What I mean is this: Let say there are 64 channels output from the Conv kernel, I would like only to train the channel 1 out of 64 channels.
The question comes from reading this paper https://arxiv.org/abs/1911.09659.
In this paper, it suggests we could freeze some filter and train the rest in the transfer learning.
However, I am wondering how to achieve it in Tensorflow.
If we are able to freeze some layers, it is quite obvious, just iterate the layers in the network and make their trainable boolean value to be False.
However, when it comes to kernels, it would be troublesome I than I think.
I have found this anwser Get the value of some weights in a model trained by TensorFlow.
I tried to get all the weights and put them like this:
def get_weights_bias(model, layer_name):
"""
This function aims to extract kernel and its bias from the original weight
:param model: the model we want to extract weight from
:param layer_name: the name of the layer we want to extract the weight from
:return: kernel_list and bias_list of particular layer
"""
for layer in model.layers:
if layer_name == layer.name:
weights = layer.get_weights()
print(type(weights))
print(weights[0].shape, type(weights[0])) # weights
print(weights[1].shape, type(weights[1])) # biases
kernel_list = []
bias_list = weights[1]
print(type(bias_list))
for j in range(weights[0].shape[-1]):
name_weight = layer_name + "_weight_" + str(j)
kernel = tf.Variable(initial_value=weights[0][:, :, :, j], name=name_weight, trainable=True)
kernel = tf.expand_dims(kernel, -1)
kernel_list.append(kernel)
return kernel_list, bias_list
Suggested by this answer, I faced some problems. I found it is hard to restore them back into the conv layer as layer.set_weights()
only accepts numpy array instead of tf.Variable.
Any suggestions??