擅长:python、mysql、java
<p>您可以简单地为层属性<code>trainable</code>分配一个布尔值。</p>
<pre><code>model.layers[n].trainable = False
</code></pre>
<p>你可以想象哪一层是可训练的:</p>
<pre><code>for l in model.layers:
print(l.name, l.trainable)
</code></pre>
<p>也可以通过模型定义传递:</p>
<pre><code>frozen_layer = Dense(32, trainable=False)
</code></pre>
<p>从路缘石<a href="https://keras.io/getting-started/faq/" rel="noreferrer">documentation</a>:</p>
<blockquote>
<p>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.<br/>
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.</p>
</blockquote>