如何将 keras 中的参数设置为不可训练? [英] How to set parameters in keras to be non-trainable?
问题描述
我是 Keras 的新手,我正在构建模型.我想在训练前几层的同时冻结模型最后几层的权重.我试图将横向模型的可训练属性设置为 False,但它似乎不起作用.这是代码和模型摘要:
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()
模型摘要:模型摘要.
如您所见,model_1
是可训练的.但是根据代码,它被设置为不可训练.
As you can see, model_1
is trainable. But according to the code, it is set to be non-trainable.
推荐答案
单词trainble"中有一个拼写错误(缺少一个a").可悲的是,keras 没有警告我该模型没有trainble"属性.问题可以结束了.
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.
这篇关于如何将 keras 中的参数设置为不可训练?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!