简体   繁体   English

Keras 中的“model.trainable = False”是什么意思?

[英]What does “model.trainable = False” mean in Keras?

I want to freeze a pre-trained network in Keras.我想冻结 Keras 中的预训练网络。 I found base.trainable = False in the documentation.我在文档中找到base.trainable = False But I didn't understand how it works.但我不明白它是如何工作的。 With len(model.trainable_weights) I found out that I have 30 trainable weights.使用len(model.trainable_weights)我发现我有 30 个可训练的权重。 How can that be?这个怎么可能? The network shows total trainable params: 16,812,353.该网络显示总可训练参数:16,812,353。 After freezing I have 4 trainable weights.冷冻后,我有 4 个可训练的重量。 Maybe I don't understand the difference between params and weights.也许我不明白参数和重量之间的区别。 Unfortunately I am a beginner in Deep Learning.不幸的是,我是深度学习的初学者。 Maybe someone can help me.也许有人可以帮助我。

A Keras Model is trainable by default - you have two means of freezing all the weights: 默认情况下,Keras Model是可训练的 - 您有两种冻结所有权重的方法:

  1. model.trainable = False before compiling the model model.trainable = False编译 model 之前
  2. for layer in model.layers: layer.trainable = False - works before & after compiling for layer in model.layers: layer.trainable = False - 在编译之前和之后工作

(1) must be done before compilation since Keras treats model.trainable as a boolean flag at compiling, and performs (2) under the hood. (1) 必须在编译前完成,因为 Keras 在编译时将model.trainable视为 boolean 标志,并在后台执行 (2)。 After doing either of the above, you should see:完成上述任一操作后,您应该会看到:

print(model.trainable_weights)
# [] 

Regarding the docs, likely outdated - see linked source code above, up-to-date.关于文档,可能已过时 - 请参阅上面的链接源代码,最新。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM