Boss lesson · Unit 04
Stand on a trained model
Load weights, freeze the body, train a new head
What you practise
- Save and load a
state_dict, freeze layers, replace the head, different learning rates for old and new parts, inference witheval()andno_grad()
Try a question from this lesson
Pick one
A model trained on a million photos already detects edges, textures and shapes. You have 200 labelled X-rays and 3 diagnoses. What is the smart move?
Correct answer
Keep its layers and train a new final layer
Why
Its early layers hold general visual features that took a million images to learn. Reuse them and train only a new last layer for your 3 classes: 200 samples are enough for that, and far too few to learn to see from scratch.
The idea
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
A model is its state_dict
Everything a model has learned lives in its state_dict: an ordinary dict from parameter name to tensor.
Saving a model means saving that dict with torch.save. Loading means building the same architecture again and calling load_state_dict on it.
net = nn.Sequential(
nn.Linear(4, 8), nn.ReLU(),
nn.Linear(8, 2),
)
for name, t in net.state_dict().items():
print(name, tuple(t.shape))
# 0.weight (8, 4)
# 0.bias (8,)
# 2.weight (2, 8)
# 2.bias (2,)11 more exercises in the app
- 3× type the answer
- 3× multiple choice
- 3× fill the blanks
- 1× true or false
- 1× order the lines
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.