Fine-tuning task heads
Quick answer
A task head converts a representation into outputs for a new task. Training a new head on frozen features is head-only adaptation; fine-tuning the backbone additionally updates selected pretrained layers. Examples are static Python snippets tested locally on CPU; PyTorch is required for selected examples. ByteMind execution is unverified.
Head parameter budget
Expected output (locally tested): 68. A biased linear head has one weight per input-output pair and one bias per output.
How it works
For z with width d and C mutually exclusive classes, a biased linear head has d*C+C parameters and returns C logits per row. CrossEntropyLoss expects raw logits and integer class IDs. Start with a head-only baseline; consider unfreezing later layers if validation performance justifies the added capacity. Set requires_grad_(True) on selected layers and ensure the optimizer includes them, typically with a smaller encoder learning rate. Rebuild or explicitly extend optimizer groups, and document changes to optimizer state. Choose train/eval mode separately for each submodule; model.train() recursively changes child modes, so reapply frozen backbone eval() afterward. Preserve a validation checkpoint, not the final training state automatically. Inspect state changes and gradient norms; a nonzero gradient is not proof of a useful update. Use small target data and compare equal target update budgets, reporting extra source pretraining compute. Catastrophic forgetting means source capability can degrade after adaptation; evaluate source retention only on independently held-out source data. Never use test labels to decide which layers to unfreeze.
Update a head only
import torchfrom torch import nnb=nn.Linear(2,2,bias=False); b.requires_grad_(False); b.eval()h=nn.Linear(2,1,bias=False)with torch.no_grad(): b.weight.copy_(torch.eye(2)); h.weight.zero_()before=b.weight.clone()o=torch.optim.SGD(h.parameters(),lr=.1)o.zero_grad(); loss=(h(b(torch.tensor([[1.,2.]])))-1).square().mean();loss.backward();o.step()print(torch.equal(before,b.weight),[round(v,2) for v in h.weight.flatten().tolist()])Expected output (locally tested): True [0.2, 0.4]. The loss derivative at zero yields the head update; the backbone is unchanged.
Common mistakes
Do not put softmax before CrossEntropyLoss, silently omit newly unfrozen parameters from the optimizer, or call model.train() and accidentally reactivate frozen dropout.
Practice and next steps
Compute the parameter count of a 16-feature, 4-class head. Then compare head-only and partial unfreezing on validation data; inspect which parameters actually changed.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Fine-tuning task heads — knowledge check
Apply fine-tuning task heads to small examples and debugging scenarios.
What does a biased d-to-C head contain?
What should CrossEntropyLoss receive?
After unfreezing a layer, what must be checked?
What can happen after model.train()?
What is catastrophic forgetting?