如何将路缘石中的参数设置为不可训练的?

2024-04-19 13:42:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我对凯拉斯很陌生,我正在做一个模型。我想冻结模型最后几层的权重,同时训练前几层。我试图将横向模型的可训练属性设置为False,但它似乎不起作用。下面是代码和模型摘要:

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 summary

如您所见,model_1是可训练的。但根据规定,它是不可训练的。


Tags: 模型buildselffalseimgdomainlayersout
3条回答

“trainble”一词有误(缺少“a”)。可悲的是,凯拉斯没有警告我,模型没有“可训练”的属性。这个问题可以结束了。

您可以简单地为层属性trainable分配一个布尔值。

model.layers[n].trainable = False

你可以想象哪一层是可训练的:

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

也可以通过模型定义传递:

frozen_layer = Dense(32, trainable=False)

从路缘石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.

更改代码中的最后3行:

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'])

相关问题 更多 >