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.
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.
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 oneThe 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.