Transfer Learning and Fine-tuning

2 min

Objective: adapt a pretrained model with little data without destroying its knowledge.

Transfer starts from a model trained on a large source and adapts it to a target task. The first visual or linguistic representations are often reusable.

Two regimes

  1. Frozen extractor: replace the head and train only that.
  2. Fine-tuning: unfreeze part or all of the network with a generally lower learning rate.
python
from torchvision.models import resnet18, ResNet18_Weights
from torch import nn

model = resnet18(weights=ResNet18_Weights.DEFAULT)
for p in model.parameters():
    p.requires_grad = False
model.fc = nn.Linear(model.fc.in_features, 5)

The optimizer must only receive the trainable parameters. After unfreezing, recreate or update the optimizer so it sees the new parameters.

Preview — the rest of the lesson is for enrolled readers.

Already enrolled with a code?

Your access is tied to your account, not to this link. Sign in with the same email you used in class: your course is waiting, no need to enter the code again.

Sign inNo account yet? Create one
This lesson is part of the “Specialized Architectures” module

The first modules of the course are open to everyone. For the rest you have three options: buy this course once and for all, subscribe, or enter the code handed out in class.

Are you a student on this course?

The code is tied to your account: sign in or create an account and it will be applied automatically when you come back.

No account yet? Create one