skip to content

How do you replace a pretrained model's 1000-class head for a 7-class task?

level: juniorimportance: must knowfreq 66%

answer

  1. old rows point at old labels
  2. nothing above the features transfers
  3. new layer, correct output width
  4. make sure it is actually being optimised
  5. prove the wiring on 32 examples

basics

~20 s

Discard the old output layer and attach a new, randomly initialised one with seven outputs, keeping the layers beneath it. The old head maps to the wrong label set, so none of its weights are reusable.

solid answer

~50 s

The head is the final layer mapping the pooled feature vector to class scores, and its rows are tied one-to-one to the pretraining label set, so with a different label set it carries nothing usable and is thrown away whole. Attach a fresh layer with seven outputs and the same input width as the pooled representation, initialised small and random, biases at zero or at the log of the class priors when the classes are heavily imbalanced. Everything below is the backbone and loads as-is; a strict checkpoint load will fail on the head's shape, which is expected. Two things to get right: the new layer's parameters must actually be in the set you optimise, and the name-to-index label mapping must be recorded and identical at training and serving. Then prove the wiring by overfitting a 32-example subset before any real run.

go deeper

for a junior

Be ready to say the old output layer is discarded and a fresh one with the new output count replaces it, and that the layers below are what actually transfers. Naming the head's role — features in, class scores out — is expected.

for a middle

Explain the head's shape, why a strict checkpoint load fails on it, and how you confirm the new layer is in the set being optimised. Be able to say what changes for multi-label or regression targets.

for a senior

Show that you pin the class mapping, match the pretraining preprocessing exactly, and prove the rebuilt pipeline on a tiny subset before spending a full run — and that you read a plateau there as a plumbing fault.

for a principal

Own the contract between training and serving: one recorded class mapping, one preprocessing spec, both versioned with the model artefact, and a check that fails the build when they diverge. A permuted label mapping in production is silent and total, so it deserves a structural guard.

## Anatomy A classification network is a **backbone** that turns an input into a pooled feature vector — a few hundred to a few thousand numbers — followed by a **head**: a linear layer with a weight matrix of shape (number of classes) x (feature dimension), plus a bias per class. The head produces logits, which a softmax and a cross-entropy loss turn into a training signal. Each row of that weight matrix is, in effect, a prototype for one source class. Row 386 means whatever class 386 meant in the pretraining label set. Your seven classes have no correspondence to those rows, and even where a name coincidentally overlaps, the ordering and the calibration are different. There is nothing to salvage, so the head is replaced entirely rather than adapted. ## Building the replacement Construct a new linear layer with output width 7 and the same input width as the pooled representation. Initialise the weights small and random. Biases at zero is the standard default. When the classes are heavily imbalanced, initialising each bias to the log of that class's prior probability is a well-known improvement: with weights near zero, the model's first predictions are the base rates rather than a uniform guess, which avoids spending the first epochs merely learning the intercept and removes a large, uninformative initial loss spike. If the task is not single-label classification, the head changes in more than width. Multi-label means one independent output per label with a per-label binary cross-entropy rather than a softmax over classes; regression means a single linear output with a squared-error or absolute-error loss. Occasionally a small two-layer head helps, especially with a fully frozen backbone on a distant domain, at the cost of more parameters to overfit with a small dataset. ## Loading, shapes and mappings Loading a pretrained checkpoint strictly — every stored tensor must find a matching one — fails at the head, because the stored matrix has 1000 rows and yours has 7. That failure is expected: load the backbone's tensors and construct the head separately. The label mapping deserves more care than it usually gets. Your seven class names must map to indices 0-6 by a mapping that is written down, versioned, and used identically when training and when serving. A mapping that is derived from, say, directory listing order in one place and a hand-written list in another is a silent catastrophe: the model trains perfectly, the metrics look fine, and production predictions are systematically permuted. One more input-side check: the preprocessing must match what the backbone was pretrained with — the same resize convention and the same input scaling and channel normalization. Feed it differently scaled inputs and the pretrained features are being computed on out-of-distribution data, which quietly costs accuracy in a way no error message reveals. ## Prove the wiring on 32 examples Before any real run, take a fixed subset of 32 training examples, turn off augmentation and any regularisation, and train on that subset repeatedly. The training loss should fall to near zero and accuracy to 100% within a short run. This is not a model-quality result — it is a wiring check on a freshly rebuilt pipeline, and it is the cheapest one available. If it plateaus instead, look at the plumbing, not the data: - The new head's parameters are not in the set being optimised — an easy mistake when you froze the backbone by excluding parameters and swept up the head too. - The label mapping used in the loader disagrees with the label file. - The loss is receiving values in a form it does not expect — for example already-normalised probabilities where raw logits are required, or the wrong argument order. - Augmentation or dropout is still on, so the 32 examples are never actually seen twice. - Everything, head included, is frozen, so nothing is being learned at all. ## What comes next A randomly initialised head produces large, essentially arbitrary gradients in its first steps, and those gradients flow back into whatever part of the backbone you unfroze. How to keep that from disturbing good pretrained features is a step-size question with its own answer, but it is the reason the head swap and the training schedule are usually thought about together. The main thing to carry away: the backbone is what transfers, the head is a per-task attachment, and the boring parts around it — the label mapping, the preprocessing convention, and a 32-example proof that the pipeline learns at all — are where the real failures live.

  • Why not keep the pretrained head and just retrain its weights?
    With 1000 outputs and 7 classes the shape is simply wrong, so it cannot be reused at all. Even at a matching width it would be a poor starting point: its rows encode the source classes and their ordering, which biases early predictions toward concepts you no longer have. A small random initialisation offers no such bias and costs nothing.
  • What do you set the new head's biases to when the seven classes are heavily imbalanced?
    Set each bias to the log of that class's prior probability. With weights initialised near zero the logits then equal the log priors, so the model's very first predictions are the base rates. That avoids burning early epochs learning the intercept and removes a large initial loss that tells you nothing about the features.
  • Your 32-example run plateaus at 40% training accuracy — what do you check first?
    Wiring, not data. Confirm the new head's parameters are in the optimised set, that the loader's label mapping matches the label file, that augmentation and dropout are off for the check, and that the loss is receiving values in the form it expects. A pipeline that cannot memorise 32 examples has a plumbing fault, not a hard task.

Swapping the destination sign on a bus. The engine and the driver's knowledge of the roads are what you are keeping; the sign listing the old route's stops is useless for the new route and gets replaced whole, not edited.

saying these in an interview costs you the question

  • Fine-tune the pretrained 1000-class head down to 7 classes
  • The pretrained head's weights carry useful knowledge for any task
  • Only the output count matters; preprocessing can differ from pretraining
  • A plateau on 32 examples means the task is genuinely hard
  • Label indices can be regenerated independently at training and serving

context