[英]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.