Calling mdoel.trainable = False does not lock weights of model in tensorflow.
Example:
input = Input(batch_shape = (256, 224, 224, 3)
vggNet = VGG16(input_tensor = input, include_top=True)
vggNet.trainable = False
Compiling this model will yield all the parameters from vggNet as if vggNet.trainable = True was set.
I have a couple of ideas on how to fix this. I have posted them here and need feedback before moving forward.
https://groups.google.com/forum/#!topic/keras-users/O6H0PrJJMrU
@isaacgerg
Good question!
vggNet.trainable=False is meaningless here, because model actually dosen't have the 'trainable' attribute, this attribute is inherited from class Layer, if you want to freeze all layers in a model, you should use the following code:
for layer in model.layers:
layer.trainable=False
However, allow users to set this attribute may lead to misunderstanding just like what confused you, we should set the trainable attribute unchangeable to avoid this misunderstanding.
@MoyanZitto It turns out vggNet.trainable=False is not meaningless here and actually does freeze the weights. See #4514.
@isaacgerg
Thank you
Hi,
Still..
When setting a model to model.trainable = False and then checking for each layer in the model: "layer.trainable:" keeps being inconsistent -> the last function give me false. Not sure if it's a "display/messaging error" and all the layers in the model are actually frozen or they are not.
Thank you
@bagonzalo when did you compile the model? You have to compile after setting trainable = False not before.
Most helpful comment
@isaacgerg
Good question!
vggNet.trainable=Falseis meaningless here, because model actually dosen't have the 'trainable' attribute, this attribute is inherited from classLayer, if you want to freeze all layers in a model, you should use the following code:However, allow users to set this attribute may lead to misunderstanding just like what confused you, we should set the trainable attribute unchangeable to avoid this misunderstanding.