Freezing layers means stopping some parts of a model from learning. Unfreezing lets those parts learn again. This helps save time and keep good knowledge when training.
When using a pre-trained model and you want to keep some learned features fixed.
When training a big model step-by-step to avoid losing earlier learned skills.
When you want to speed up training by not updating all layers.
When fine-tuning a model on new data but keeping some layers stable.
Freezing means weights won't change during training.
Examples
This freezes all layers in the model so none will update.
TensorFlow
for layer in model.layers:
layer.trainable = False
This freezes only the first layer of the model.
TensorFlow
model.layers[0].trainable = False
This unfreezes the last three layers to allow training.
TensorFlow
for layer in model.layers[-3:]:
layer.trainable = True
Sample Model
This code builds a small model, freezes the first layer, trains on random data, and shows which layers trained and the final loss.
TensorFlow
import tensorflow as tf
from tensorflow.keras import layers, models
# Create a simple model
model = models.Sequential([
layers.Dense(10, activation='relu', input_shape=(5,)),
layers.Dense(1)
])
# Freeze the first layer
model.layers[0].trainable = False# Compile the model
model.compile(optimizer='adam', loss='mse')
# Create dummy dataimport numpy as np
x = np.random.random((10, 5))
y = np.random.random((10, 1))
# Train the model
history = model.fit(x, y, epochs=2, verbose=0)
# Check which layers are trainable
trainable_status = [layer.trainable for layer in model.layers]
print('Trainable layers:', trainable_status)
print('Loss after training:', history.history['loss'][-1])
OutputSuccess
Important Notes
Always set trainable before calling compile().
Freezing layers helps keep learned features and reduces training time.
You can selectively freeze or unfreeze layers depending on your task.
Summary
Freezing stops layers from learning to keep their knowledge.
Unfreezing allows layers to update weights again.
Set trainable before compiling the model.
Practice
(1/5)
1. What does freezing layers in a TensorFlow model do?
easy
A. Stops the layers from updating their weights during training
B. Allows the layers to learn new features
C. Deletes the layers from the model
D. Increases the learning rate for those layers
Solution
Step 1: Understand the meaning of freezing layers
Freezing layers means preventing them from changing their weights during training.
Step 2: Identify the effect of freezing
Since weights do not update, the layers keep their learned knowledge intact.
Final Answer:
Stops the layers from updating their weights during training -> Option A
Quick Check:
Freezing = stop weight updates [OK]
Hint: Freezing means no weight updates during training [OK]
Common Mistakes:
Thinking freezing deletes layers
Believing freezing increases learning rate
Confusing freezing with unfreezing
2. Which is the correct way to freeze a layer named layer in TensorFlow before compiling the model?
easy
A. layer.frozen = True
B. layer.freeze = True
C. layer.trainable = True
D. layer.trainable = False
Solution
Step 1: Recall the attribute to control training
TensorFlow uses the trainable attribute to allow or prevent training on layers.
Step 2: Set the attribute to freeze
Setting trainable = False freezes the layer so it won't update during training.
Final Answer:
layer.trainable = False -> Option D
Quick Check:
Freeze layer = trainable False [OK]
Hint: Use trainable = False to freeze layers before compile [OK]
Common Mistakes:
Using non-existent attributes like freeze or frozen
Setting trainable = True to freeze
Changing trainable after compiling
3. Given the code below, what will be the output of model.layers[0].trainable after compiling?