Freezing a layer in Keras

To freeze a layer in Keras, use:

model.layers[0].trainable = False

Notes:

  • Typically, the freezing of layers will be done so that weights which are learned in prior stages are not forgotten in later layers of the model.
  • For example, if you have BERT as one part of a Keras TensorFlow model, that layer might need to be frozen so that large changes in gradient that occur during fine tuning do not distrupt the weights that have been learned in BERT.