如何将keras中的参数设置为不可训练? [英] How to set parameters in keras to be non-trainable?

查看:658
本文介绍了如何将keras中的参数设置为不可训练?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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并没有警告我该模型不具有可训练属性。该问题可能已经结束。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆