简体   繁体   English

model.trainable = 错误 - 权重冻结且无法训练?

[英]model.trainable = False - weights frozen and untrainable?

I am using EfficientNet model ( https://keras.io/api/applications/efficientnet/#efficientnetb0-function ) with weights from ImageNet and I want to use a customized top, so I stated top = False .我正在使用带有 ImageNet 权重的 EfficientNet model ( https://keras.io/api/applications/efficientnet/#efficientnetb0-function ),我想使用自定义的顶部,所以我声明top = False I am now wondering if the weights of the EfficientNet are frozen and they are not getting retrained (that is what I want) when I use the following code:我现在想知道当我使用以下代码时,EfficientNet 的权重是否被冻结并且它们没有得到重新训练(这就是我想要的):

efnB0_model = efn.EfficientNetB0(include_top=False, weights="imagenet", input_shape=(224, 224, 3))
efnB0_model.trainable = False

Or do I have to use another code?还是我必须使用其他代码?

Thanks a lot!非常感谢!

What you did works, but people generally do it layer by layer instead, because you might eventually decide to unfreeze certain layers:你所做的工作,但人们通常会一层一层地做,因为你最终可能决定解冻某些层:

for layer in model.layers:
    layer.trainable = False

model.layers returns a list, so you can also unfreeze just the last few layers: model.layers返回一个列表,因此您也可以仅解冻最后几层:

for layer in model.layers[-10:]:
    layer.trainable = False

You can verify what can be trained with您可以验证可以训练的内容

model.trainable_variables
[]

In this case, nothing.在这种情况下,什么都没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Keras 中的“model.trainable = False”是什么意思? - What does “model.trainable = False” mean in Keras? TensorFlow中可训练变量和不可训练变量的串联 - Concatenation of a trainable and an untrainable variable in TensorFlow 从冻结模型创建可训练的Tensorflow图 - Create Trainable Tensorflow graph from frozen model 有效地为 model 中的所有可训练权重添加噪声 - Efficiently add noise to all trainable weights in a model Tensorflow:更新不可训练模型层的权重 - Tensorflow: Weights of non-trainable model layers are updated (tf.)keras 加载保存的 model 权重和可训练的词嵌入 - (tf.)keras loading saved model weights with trainable word embeddings 自定义层中的可训练权重? - Trainable weights in custom layers? 对层中的固定权重使用“K.constant”或“self.add_weight(trainable=False)”有什么不同吗 - Is it different to use `K.constant` or `self.add_weight(trainable=False)` for fixed weights in layer UserWarning:可训练的权重与收集的可训练权重误差之间的差异 - UserWarning: Discrepancy between trainable weights and collected trainable weights error 如何在自定义训练循环之前/之后检查 Keras 模型的可训练权重是否发生变化 - How to Check if Trainable Weights of Keras Model Change Before/After Custom Training Loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM