Model Pipeline - Freezing layers
This pipeline shows how freezing layers in a neural network helps keep some parts fixed while training others. It speeds up training and preserves learned features.
Jump into concepts and practice - no test required
This pipeline shows how freezing layers in a neural network helps keep some parts fixed while training others. It speeds up training and preserves learned features.
Loss
1.2 |****
0.9 |***
0.7 |**
0.6 |*
0.55|*
+---------
Epochs 1-5| Epoch | Loss ↓ | Accuracy ↑ | Observation |
|---|---|---|---|
| 1 | 1.2 | 0.45 | Loss starts high, accuracy low as model begins learning |
| 2 | 0.9 | 0.60 | Loss decreases, accuracy improves as trainable layers adjust |
| 3 | 0.7 | 0.72 | Continued improvement, frozen layers keep features stable |
| 4 | 0.6 | 0.78 | Model converging, trainable layers fine-tuned |
| 5 | 0.55 | 0.82 | Training stabilizes with good accuracy |
model?requires_grad = False for each parameter.requires_grad = False. Others are invalid or incorrect.import torch.nn as nn model = nn.Sequential( nn.Linear(10, 5), nn.ReLU(), nn.Linear(5, 2) ) for param in model[0].parameters(): param.requires_grad = False trainable_params = [p for p in model.parameters() if p.requires_grad] print(len(trainable_params))
for param in model.layer1.parameters():
param.grad = Falseparam.grad holds gradient values, it is a tensor or None, not a flag to enable/disable gradients.param.requires_grad = False. Setting param.grad = False is invalid and does not freeze.layer1, layer2, and layer3. You want to freeze layer1 and layer2 but train layer3. Which code correctly freezes only the first two layers?requires_grad = False on each parameter in the layers to freeze.layer1 and layer2 parameters and freezes them correctly. for param in model.parameters():
param.requires_grad = False
for param in model.layer3.parameters():
param.requires_grad = False incorrectly freezes all parameters including layer3. model.layer1.requires_grad = False
model.layer2.requires_grad = False tries to set requires_grad on layers (invalid). model.freeze_layers(['layer1', 'layer2']) calls a non-existent method.