How to set parameters in keras to be non-trainable?

丶灬走出姿态 提交于 2020-06-25 09:49:05

问题


I am new to Keras and I am building a model. I want to freeze the weights of the last few layers of the model while training the previous layers. I tried to set the trainable property of the lateral model to be False, but it dosen't seem to work. Here is the code and the model summary:

opt = optimizers.Adam(1e-3)
domain_layers = self._build_domain_regressor()
domain_layers.trainble = False
feature_extrator = self._build_common()
img_inputs = Input(shape=(160, 160, 3))
conv_out = feature_extrator(img_inputs)
domain_label = domain_layers(conv_out)
self.domain_regressor = Model(img_inputs, domain_label)
self.domain_regressor.compile(optimizer = opt, loss='binary_crossentropy', metrics=['accuracy'])
self.domain_regressor.summary()

The model summary: model summary.

As you can see, model_1 is trainable. But according to the code, it is set to be non-trainable.


回答1:


You can simple assign a boolean value to the layer property trainable.

model.layers[n].trainable = False

You can visualize which layer is trainable:

for l in model.layers:
    print(l.name, l.trainable)

You can pass it by the model definition too:

frozen_layer = Dense(32, trainable=False)

From Keras documentation:

To "freeze" a layer means to exclude it from training, i.e. its weights will never be updated. This is useful in the context of fine-tuning a model, or using fixed embeddings for a text input.
You can pass a trainable argument (boolean) to a layer constructor to set a layer to be non-trainable. Additionally, you can set the trainable property of a layer to True or False after instantiation. For this to take effect, you will need to call compile() on your model after modifying the trainable property.




回答2:


There is a typo in the Word "trainble"(missing an "a"). Saddly keras doesn't warn me that the model doesn't have the property "trainble". The question could be closed.




回答3:


Despite the fact that the original question's solution is a typo fix, let me add some information on keras trainables.

Modern Keras contains the following facilities to view and manipulate trainable state:

  • tf.keras.Layer._get_trainable_state() function - prints the dictinary where keys are model components and values are booleans. Note that tf.keras.Model is also a tf.Keras.Layer.
  • tf.keras.Layer.trainable property - to manipulate trainable state of individual layers.

So the typical actions look like following:

# Print current trainable map:
print(model._get_trainable_state())

# Set every layer to be non-trainable:
for k,v in model._get_trainable_state().items():
    k.trainable = False

# Don't forget to re-compile the model
model.compile(...)



回答4:


Change the last 3 lines in your code:

last_few_layers = 20 #number of the last few layers to freeze
self.domain_regressor = Model(img_inputs, domain_label)
for layer in model.layers[:-last_few_layers]:
    layer.trainable = False
self.domain_regressor.compile(optimizer = opt, loss='binary_crossentropy', metrics=['accuracy'])


来源:https://stackoverflow.com/questions/53503389/how-to-set-parameters-in-keras-to-be-non-trainable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!