Bird
Raised Fist0
TensorFlowml~12 mins

Freezing and unfreezing layers in TensorFlow - Model Pipeline Trace

Choose your learning style10 modes available
Model Pipeline - Freezing and unfreezing layers

This pipeline shows how a model uses frozen layers to keep learned features fixed, then unfreezes layers to fine-tune and improve accuracy.

Data Flow - 6 Stages
1Data input
1000 rows x 32 x 32 x 3→Load images with labels→1000 rows x 32 x 32 x 3
Image of a cat with label 'cat'
↓
2Preprocessing
1000 rows x 32 x 32 x 3→Normalize pixel values to 0-1 range→1000 rows x 32 x 32 x 3
Pixel value 120 becomes 0.47
↓
3Feature extraction (frozen layers)
1000 rows x 32 x 32 x 3→Pass through frozen convolutional layers→1000 rows x 8 x 8 x 64
Feature map highlighting edges
↓
4Train classifier layers
1000 rows x 8 x 8 x 64→Train dense layers on extracted features→1000 rows x 10 (class probabilities)
[0.1, 0.7, 0.05, ..., 0.02]
↓
5Unfreeze layers
1000 rows x 8 x 8 x 64→Unfreeze some convolutional layers for fine-tuning→1000 rows x 8 x 8 x 64
Layers now trainable to adjust features
↓
6Fine-tune entire model
1000 rows x 8 x 8 x 64→Train all layers with lower learning rate→1000 rows x 10 (class probabilities)
[0.05, 0.8, 0.03, ..., 0.01]
Training Trace - Epoch by Epoch

Epoch 1: ************ (loss=1.2)
Epoch 2: ******** (loss=0.9)
Epoch 3: ******* (loss=0.7)
Epoch 4: ****** (loss=0.65)
Epoch 5: **** (loss=0.5)
Epoch 6: *** (loss=0.45)
EpochLoss ↓Accuracy ↑Observation
11.20.55Training classifier layers with frozen base
20.90.68Accuracy improves as classifier learns
30.70.75Stable improvement with frozen base
40.650.78Unfreeze some layers for fine-tuning
50.50.85Fine-tuning improves feature extraction
60.450.88Model converges with unfreezing
Prediction Trace - 5 Layers
Layer 1: Input layer
Layer 2: Frozen convolutional layers
Layer 3: Trainable dense layers
Layer 4: Unfreeze and fine-tune convolutional layers
Layer 5: Final dense layers
Model Quiz - 3 Questions
Test your understanding
Why do we freeze layers before training the classifier layers?
ATo keep learned features fixed and speed up training
BTo prevent the model from learning anything new
CTo make the model ignore the input data
DTo randomly change weights in the frozen layers
Key Insight
Freezing layers helps keep useful features stable while training new parts. Unfreezing allows the model to improve by adjusting earlier layers carefully, leading to better accuracy.

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

  1. Step 1: Understand the meaning of freezing layers

    Freezing layers means preventing them from changing their weights during training.
  2. Step 2: Identify the effect of freezing

    Since weights do not update, the layers keep their learned knowledge intact.
  3. Final Answer:

    Stops the layers from updating their weights during training -> Option A
  4. 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

  1. Step 1: Recall the attribute to control training

    TensorFlow uses the trainable attribute to allow or prevent training on layers.
  2. Step 2: Set the attribute to freeze

    Setting trainable = False freezes the layer so it won't update during training.
  3. Final Answer:

    layer.trainable = False -> Option D
  4. 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?
model.layers[0].trainable = False
model.compile(optimizer='adam', loss='mse')
print(model.layers[0].trainable)
medium
A. None
B. True
C. False
D. Raises an error

Solution

  1. Step 1: Set layer.trainable before compiling

    The code sets trainable = False before calling compile.
  2. Step 2: Check if trainable attribute changes after compile

    The trainable attribute remains as set before compile; it does not change automatically.
  3. Final Answer:

    False -> Option C
  4. Quick Check:

    trainable stays False after compile [OK]
Hint: Set trainable before compile; it stays unchanged after [OK]
Common Mistakes:
  • Assuming compile resets trainable to True
  • Expecting an error from accessing trainable
  • Confusing trainable with layer existence
4. You want to unfreeze a previously frozen layer in your TensorFlow model. Which code snippet fixes the error below?
AttributeError: 'Sequential' object has no attribute 'unfreeze'

Current code:
model.unfreeze()
model.compile(optimizer='adam', loss='mse')
medium
A. Set trainable on each layer: for layer in model.layers: layer.trainable = True
B. Set model.trainable = False instead
C. Call model.unfreeze() method
D. Rebuild the model from scratch

Solution

  1. Step 1: Understand the error cause

    The error says Sequential model has no unfreeze attribute, meaning calling model.unfreeze() is invalid.
  2. Step 2: Correct way to unfreeze layers

    Each layer has a trainable attribute; set it to True for all layers to unfreeze.
  3. Final Answer:

    Set trainable on each layer: for layer in model.layers: layer.trainable = True -> Option A
  4. Quick Check:

    Unfreeze layers by setting layer.trainable True [OK]
Hint: Set trainable on layers, not on model object [OK]
Common Mistakes:
  • Trying to call model.unfreeze()
  • Using non-existent unfreeze() method
  • Rebuilding model unnecessarily
5. You have a pretrained model and want to freeze all layers except the last two for fine-tuning. Which code correctly achieves this before compiling?
hard
A. for layer in model.layers: layer.trainable = True
B. for layer in model.layers[:-2]: layer.trainable = False for layer in model.layers[-2:]: layer.trainable = True
C. model.trainable = False model.layers[-2:].trainable = True
D. model.freeze_layers(except_last=2)

Solution

  1. Step 1: Freeze all layers except last two

    Use slicing to set trainable = False for all layers except last two.
  2. Step 2: Unfreeze last two layers

    Set trainable = True for the last two layers to allow training.
  3. Final Answer:

    for layer in model.layers[:-2]: layer.trainable = False for layer in model.layers[-2:]: layer.trainable = True -> Option B
  4. Quick Check:

    Freeze all but last two layers [OK]
Hint: Freeze with slicing; unfreeze last layers explicitly [OK]
Common Mistakes:
  • Trying to set trainable on model object
  • Using non-existent freeze_layers method
  • Setting all layers trainable True
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard