Keras
machine learning
neural networks
freezing weights
deep learning

How to freeze weights in certain layer with Keras?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

In deep learning, particularly when using Keras, there are scenarios where you might want to "freeze" the weights of specific layers within a neural network model. Freezing a layer means that during the training process, the weights of that layer do not update. This technique is useful in various situations, such as transfer learning, where you use a pre-trained model and only update the last few layers for a new task.

Why Freeze Layers in a Neural Network?

  1. Transfer Learning: Often, models like VGG, ResNet, or Inception are already trained on vast datasets, such as ImageNet. Freezing the initial layers can preserve learnt features and prevent catastrophic forgetting.
  2. Reduces Overfitting: By training fewer layers, we mitigate the risk of overfitting, particularly when you have a relatively small dataset.
  3. Speed Up Training: With fewer trainable parameters, training becomes more efficient, requiring less computation.

How to Freeze Layers in Keras

With Keras, freezing layers is relatively straightforward. Here's a step-by-step technical guide to achieving this:

  1. Load a Pre-trained Model: Start by loading a model with pre-trained weights.
  2. Set Layers to Non-trainable: Mark specific layers as non-trainable before compiling the model.
  3. Compile the Model: After updating layers, always compile the model again.
  4. Train the Model: Finally, fit the model to your data.

Detailed Example

Let's go through a detailed example where we freeze layers in a pre-trained model using Keras.

  • Load the Base Model: In the example above, we load VGG16 without the top fully connected layers (include_top=False).
  • Freezing Layers: The for loop iterates through each layer in base_model, setting layer.trainable to False, which stops these layers from updating during training.
  • Add Custom Layers: We add new layers using the Keras functional API. These layers introduce task-specific layers to adapt the model to new data.
  • Compile: It's crucial to recompile the model after changing the trainable status of any layer.
  • Training: Finally, the model can be trained on new data. Only the weights of the newly added layers will be updated.
  • Initial Layers: In convolutional neural networks (CNNs), early layers capture general features like edges and textures. These layers are often frozen in transfer learning.
  • Transfer Learning Stages: You might freeze, train, and then unfreeze more layers incrementally in a process typically called "fine-tuning."

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.