When freezing layers in a model, the main goal is to keep learned features fixed while training new parts. The key metrics to watch are validation loss and validation accuracy. These show if the model is learning new tasks well without forgetting old knowledge. If validation loss decreases and accuracy improves, freezing is helping. If not, the frozen layers might block learning.
Freezing layers in PyTorch - Model Metrics & Evaluation
Start learning this pattern below
Jump into concepts and practice - no test required
Confusion Matrix (after freezing some layers):
Predicted
Pos Neg
Actual
Pos 85 15
Neg 10 90
TP = 85, FP = 10, TN = 90, FN = 15
Precision = 85 / (85 + 10) = 0.895
Recall = 85 / (85 + 15) = 0.85
F1 Score = 2 * (0.895 * 0.85) / (0.895 + 0.85) ≈ 0.872
This shows the model still predicts well after freezing layers, balancing precision and recall.
Freezing layers can limit how much the model adapts. This might keep precision high by avoiding false alarms but reduce recall if the model misses new patterns.
For example, in a spam filter, freezing early layers keeps known spam features fixed, so precision stays high (few good emails marked spam). But recall might drop if new spam types appear and the model can't learn them well.
Choosing which layers to freeze balances this tradeoff: freeze too many and recall drops, freeze too few and training is slower or unstable.
Good: Validation accuracy improves or stays stable, validation loss decreases, and precision and recall remain balanced after freezing layers.
Bad: Validation accuracy drops, loss stays high or increases, or recall drops sharply indicating the model can't learn new patterns due to frozen layers.
- Overfitting: If only final layers train, they might overfit small new data while frozen layers don't adapt.
- Data leakage: Using test data during tuning can falsely show good metrics.
- Ignoring validation loss: Accuracy alone can hide if model is not improving properly.
- Freezing too many layers: Can block learning new features, hurting recall and overall performance.
Your model has 98% accuracy but only 12% recall on fraud detection after freezing layers. Is it good for production? Why or why not?
Answer: No, it is not good. High accuracy can be misleading if fraud cases are rare. Low recall means the model misses most frauds, which is dangerous. The frozen layers might prevent learning new fraud patterns, so you should adjust which layers to freeze or retrain more.
Practice
Solution
Step 1: Understand freezing layers meaning
Freezing layers means preventing their weights from changing during training.Step 2: Effect on training
When frozen, layers do not update weights, so they keep learned features intact.Final Answer:
Stops the layers' weights from updating -> Option CQuick Check:
Freezing = no weight updates [OK]
- Thinking freezing increases learning rate
- Believing freezing removes layers
- Assuming freezing duplicates layers
model?Solution
Step 1: Identify correct syntax to freeze parameters
Freezing requires settingrequires_grad = Falsefor each parameter.Step 2: Check each option
for param in model.parameters(): param.requires_grad = False correctly loops over parameters and setsrequires_grad = False. Others are invalid or incorrect.Final Answer:
for param in model.parameters(): param.requires_grad = False -> Option BQuick Check:
Set requires_grad False to freeze [OK]
- Using model.requires_grad instead of param.requires_grad
- Calling a non-existent freeze() method
- Setting param.grad to None does not freeze
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))
What will be printed?
Solution
Step 1: Analyze model layers and parameters
model[0] is Linear(10,5) with 2 parameters (weight and bias). model[2] is Linear(5,2) with 2 parameters.Step 2: Check which parameters are trainable
Parameters in model[0] are frozen (requires_grad=False), so only model[2]'s 2 parameters remain trainable.Final Answer:
2 -> Option AQuick Check:
Frozen layer params excluded, trainable = 2 [OK]
- Counting all parameters ignoring requires_grad
- Assuming ReLU has parameters
- Confusing layer indices
for param in model.layer1.parameters():
param.grad = FalseWhat is the problem with this code?
Solution
Step 1: Understand difference between param.grad and param.requires_grad
param.gradholds gradient values, it is a tensor or None, not a flag to enable/disable gradients.Step 2: Correct way to freeze parameters
To freeze, setparam.requires_grad = False. Settingparam.grad = Falseis invalid and does not freeze.Final Answer:
param.requires_grad should be set, not param.grad -> Option DQuick Check:
Freeze by requires_grad=False, not param.grad [OK]
- Confusing param.grad with requires_grad
- Trying to disable gradients by setting param.grad
- Assuming param.grad is a boolean
layer1, layer2, and layer3. You want to freeze layer1 and layer2 but train layer3. Which code correctly freezes only the first two layers?Solution
Step 1: Understand freezing multiple layers
Freezing means settingrequires_grad = Falseon each parameter in the layers to freeze.Step 2: Evaluate options
for layer in [model.layer1, model.layer2]: for param in layer.parameters(): param.requires_grad = False correctly loops overlayer1andlayer2parameters 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.Final Answer:
for layer in [model.layer1, model.layer2]: for param in layer.parameters(): param.requires_grad = False -> Option AQuick Check:
Freeze layers by setting requires_grad False per parameter [OK]
- Setting requires_grad on layer objects instead of parameters
- Using non-existent freeze_layers method
- Freezing all parameters
