Frozen feature extractor
Quick answer
A frozen feature extractor maps raw inputs into representations while its learned parameters remain unchanged. A new task head learns from those features instead of updating the entire network. Examples are static Python snippets tested locally on CPU; PyTorch is required for selected examples. ByteMind execution is unverified.
Fixed projection
x=[2,3,4]w=[[1,0,1],[0,1,-1]]print([sum(a*b for a,b in zip(row,x)) for row in w])Expected output (locally tested): [6, -1]. The two output coordinates are reusable features rather than final labels.
How it works
Let f_theta(x) be an encoder and g_phi(z) a task head. The prediction is g_phi(f_theta(x)). Train phi while keeping theta fixed. This is a linear probe if the head is linear; a nonlinear head tests a different hypothesis. Transfer is useful when the source encoder captures reusable structure and the target has few labels. Random features of equal width are a useful control. In PyTorch set backbone.requires_grad_(False) and backbone.eval(); the former prevents parameter gradients, while the latter changes dropout and batch normalization behavior. The two are independent. Build the optimizer from head parameters only. When the encoder is entirely fixed and no input gradient is needed, compute features with torch.no_grad() and cache them if input transforms are fixed. Random augmentation or a changing encoder invalidates such a cache. Check all state_dict entries including normalization buffers before and after training. Keep source and target provenance and data partitions separate. A frozen encoder can discard target-relevant information, so transfer can hurt.
Frozen gradients and buffers
import torchfrom torch import nnb=nn.Sequential(nn.Linear(2,3),nn.BatchNorm1d(3))b.requires_grad_(False); b.eval()h=nn.Linear(3,1)before=b[1].running_mean.clone()h(b(torch.ones(2,2))).sum().backward()print(all(p.grad is None for p in b.parameters()),h.weight.grad is not None,torch.equal(before,b[1].running_mean))Expected output (locally tested): True True True. No encoder gradients, a head gradient, and unchanged batch-normalization running mean are distinct checks.
Common mistakes
Do not assume eval() freezes parameters or requires_grad_(False) stops running-statistic updates. Do not cache features from random augmentations and then claim fresh augmentation each epoch.
Practice and next steps
Compare a fixed projection with a random projection of the same width. Verify the feature extractor state before and after training the head; explain what information the projection loses.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Frozen feature extractor — knowledge check
Apply frozen feature extractor to small examples and debugging scenarios.
What does a frozen extractor keep unchanged?
What does eval() change?
Why compare random features?
When is caching fixed features appropriate?
Can freezing hurt target accuracy?