27 / 28 · Adapting Pretrained Models
Transfer learning in NLP
Use a pretrained representation for a new task, identify information that a head over frozen features cannot recover, and trace the tradeoff when the representation itself changes.
BERT and masked language modeling · Encoder, decoder and encoder–decoder models · Corpora and evaluation
On this page 7
A representation can preserve exactly the distinction that mattered during pretraining and discard the distinction needed for a new task. Adding a more elaborate classifier cannot reconstruct information that its input no longer contains. The first adaptation decision is therefore which parts of the pretrained system remain fixed.
Starting with the inherited interface#
Write the representation as , where is an input and contains the backbone parameters. A task head maps to predictions. Pretraining supplies and sometimes an existing output head. It also supplies a tokenizer and input conventions. Those components form an interface: token IDs, special tokens, position handling and output semantics have to agree.
The target task supplies labels and a loss. A language-model output is a distribution over tokens; a document classifier needs a distribution over classes. These sets do not become interchangeable because both are represented by vectors. We must specify how the existing output is interpreted or replace it with a head having the required meaning.
Peters, Ruder and Smith distinguish frozen feature extraction from directly fine-tuning the pretrained model (2019, §§1–3.2). This is a choice of trainable parameters, not a ranking that holds for every target distribution. The usefulness of the representation and the reliability of target evaluation determine which choice succeeds.
Mapping a vocabulary output to labels#
For a masked-language-model head, one possible hand-designed interface assigns one vocabulary token to each class and reads their probabilities at a selected masked position. The template, position and label words are part of that classifier. They need no gradient updates, but they can still be chosen using labeled development data. Calling a procedure zero-shot does not establish that those choices were independent of the test set.
Consider an explicitly constructed vocabulary distribution: a first label token has probability 0.3, a second has probability 0.1, and all remaining tokens have total probability 0.6. Renormalizing over the two chosen tokens gives
This is a conditional probability under the vocabulary distribution, given that the output belongs to the chosen pair. It is not automatically a calibrated probability of the real-world class. The excluded mass remains relevant to how well the prompt matches the model’s objective. These values are specified inputs, not an evaluation of a pretrained model.
A class name split into several tokens introduces another decision. A masked model’s conditional probabilities for separate positions do not automatically define one joint phrase probability. A causal model can score a specified continuation by its conditional token factors, but length and tokenization then affect the score. Define the scoring rule before comparing verbalizers. If a small target training set is available, a learned task head offers a direct alternative.
Frozen features and a linear model#
Freeze , compute and optimize the head on target training examples. For classes and representation width , a linear softmax head has matrix and bias :
Here is the number of target training examples, and is the correct class index. The head has parameters. The training objective averages over the examples used for fitting; a target validation estimate is computed on a separate set with the same label meaning.
For 30 constructed examples, width 7 and three classes, cached four-byte features occupy bytes, while the head contains 24 parameters. This counts only the feature array. The encoder weights, temporary inference memory, labels and classifier optimizer require additional storage. Feature caching is valid only while preprocessing and the encoder computation remain fixed; changing either invalidates the cache.
Worked example: a lost coordinate. Specify four inputs and two labeling rules:
| Input | Original label | Target label |
|---|---|---|
| 0 | 0 | |
| 0 | 1 | |
| 1 | 0 | |
| 1 | 1 |
The original label records the sign of the first coordinate; the target label records the sign of the second. These four vectors are mathematical objects, not extracted text embeddings. Suppose the inherited scalar representation is . At each represented value, one example belongs to each target class. Every deterministic classifier receiving only this scalar must give the pair the same prediction. Its target accuracy on these four equally weighted points cannot exceed one half, regardless of classifier complexity.
For a probabilistic prediction on either such pair, the mean binary loss is
For , the derivative is negative below and positive above it. The minimum is therefore at , with loss . A zero-weight, zero-bias logistic head attains it. The obstruction comes from identical representations with conflicting labels; more head parameters cannot remove it.
Identical representations hide a label distinction
Label 0Label 1
Allowing the representation to change#
Fine-tuning optimizes both the chosen backbone parameters and the task head from a pretrained initialization. The loss derivative must now pass through the representation:
The Jacobian describes how representation coordinates change with the parameters. Freezing the backbone stops this update. Freezing the final task head alone does not stop gradients from passing through its computation to an earlier trainable component.
For the four-point construction, use scalar representation , logit and probability . A single example’s binary-logistic derivative with respect to the logit is . Applying the chain rule gives
Start with , and . Average the derivatives over all four points and update all four scalar parameters simultaneously with step size 0.2. Initially the mean gradient of is , the head-weight gradient is 0.761594 and the bias gradient is zero. The first update therefore gives and . The second coordinate begins to enter the representation because the target labels produce a nonzero derivative along it.
| Updates | Target NLL | Target accuracy | Agreement with original labels |
|---|---|---|---|
| 0 | 1.126928 | 1/2 | 1 |
| 5 | 0.640821 | 1/2 | 1 |
| 20 | 0.221297 | 1 | 1/2 |
| 60 | 0.019927 | 1 | 1/2 |
| 240 | 0.002325 | 1 | 1/2 |
At 240 updates, and . The learned classifier mostly uses the second coordinate. These are executed training-set calculations for a four-parameter surrogate; they do not measure generalization, language understanding or the advantage of pretraining over training from scratch. A direct classifier on the original two coordinates could also solve this target problem.
Changing the representation removes the loss restriction
Train representation and head
Best loss with h=x₁
This is a mechanism check on specified points. There is no held-out set.
We can verify the representation issue without an optimization run. Choosing , and gives logits −2 or 2 with the target sign. Every target example then has loss . The target distinction was available in the inputs and inaccessible only after the original projection.
What is being transferred#
Transferred parameters provide an initial function and a starting point for optimization. A frozen backbone additionally restricts every target predictor to depend on the same representation. A tokenizer transfers a particular decomposition of input strings. An inherited output head transfers particular prediction semantics. These are separable commitments, and a new task may benefit from some while requiring others to change.
The four-point example also exposes a competing objective. The adapted classifier agrees with the original labels on only half the points. It does not prove that the entire earlier task is irrecoverable from all internal states of a real network; it shows that optimizing the target objective need not preserve the original predictor. If retention matters, evaluate the old objective separately rather than infer it from the target score.
Continuing language-model training on unlabeled target-domain text and learning a target classifier are distinct stages. ULMFiT explicitly separates general language-model pretraining, target-domain language-model adaptation and classifier fine-tuning (Howard and Ruder, 2018, §3). This provides one example of separating a domain shift from the supervised task. It does not guarantee that an extra adaptation stage helps every dataset.
Negative transfer is an empirical comparison: under a specified target protocol and resource budget, transferring a particular source system performs worse than a defined alternative, often training without that source initialization. To establish it, run the comparator. The frozen-feature obstruction above explains one possible failure mechanism; it is not an experiment establishing negative transfer for a pretrained NLP model.
One backbone, many tasks#
Several heads can share a fixed encoder, allowing features to be reused when the input and pooling rule agree. Separate fine-tuned copies can instead change their representations independently. Their model weights, optimizer state and versioning costs differ. Parameter-efficient adaptation provides another boundary between shared and task-specific weights; its mechanisms belong to the later PEFT topic.
The output structure also matters. A sentence classifier consumes one pooled vector, a tagger needs token-level states and a span model must map positions back to input tokens. A decoder scores continuations. Pooling and token alignment can remove or misassign information even when the encoder is appropriate. A successful head for one output structure is not evidence that the same head fits another.
Limitations#
Training and test sets must use the same task definition, while still being separated so that labels, template choices, preprocessing statistics and duplicates do not leak across the boundary. Choose a split reflecting the intended deployment: documents from the same source or near-duplicates can make a random split overly easy. Keep all adaptation decisions on training and validation data; reserve the test set for the final defined comparison.
Compare a prompt interface, frozen features and fine-tuning on the same examples and metrics, including their preprocessing and any tuning budget. Report whether the backbone or data changed. A training-loss decrease establishes successful optimization of that objective. It does not establish calibration, robustness to new domains or improved held-out performance.
Gradient freezing and inference mode solve different problems. Disabling parameter updates does not by itself disable dropout or changes to mutable layer state. Cached features require a reproducible encoder evaluation mode. Conversely, a frozen prefix can save its own backward activations, but trainable layers after that prefix still need their ordinary forward and backward computations. Resource savings depend on where the boundary lies.
What you should remember#
Name the transferred representation and its input contract. Define the target head and loss separately. A head receiving a frozen representation cannot recover an erased distinction. Target updates can change old behavior. Compare methods on one held-out protocol before calling a transfer useful or harmful.
Reproduce the construction#
The standard-library calculation supplies all four vectors and both labeling rules, verifies the frozen optimum, executes 240 simultaneous gradient updates and saves their loss/accuracy trace. It also computes the vocabulary renormalization and feature-cache counts. No text corpus, tokenizer, pretrained weights or model download is required.
Next#
Once the trainable boundary is chosen, update size and validation determine whether adaptation is useful. The next topic, fine-tuning, develops that optimization problem.
References#
- Matthew E. Peters, Sebastian Ruder and Noah A. Smith. To Tune or Not to Tune? Adapting Pretrained Representations to Diverse Tasks. RepL4NLP, 2019. §§1–3.2: frozen feature extraction, task-specific models and fine-tuning.
- Jeremy Howard and Sebastian Ruder. Universal Language Model Fine-tuning for Text Classification. ACL, 2018. §3: separating source pretraining, target-domain language modeling and supervised adaptation.