Transfer Learning & Fine-Tuning
Reusing features across tasks, and when it fails.
Pretrained features transfer remarkably often, and fail in ways that theory can partly predict. This lesson proves the classic bound for learning across domains and shows why making features look the same in both domains is not enough, explains why full fine-tuning can damage good features out of distribution, analyzes low-rank adaptation, and derives the gradient of meta-learning.
What you’ll learn
- State and prove the bound , and prove that invariant representations force a large joint error when the label distributions differ.
- Explain the layer-transfer experiments of Yosinski et al. and the analysis of fine-tuning versus linear probing under distribution shift.
- Count the parameters of LoRA and adapters, and explain the design choices that make them work.
- Derive the MAML meta-gradient with its second-order term, and relate multi-task and meta-learning to learning an inductive bias.
What transfers: the experimental picture
Yosinski, Clune, Bengio & Lipson (2014) designed the cleanest test of what transfers. They split ImageNet's 1,000 classes into two halves A and B of 500 classes each (about 645,000 images apiece) and trained a base network on each. Then, for each depth , they copied the first layers of a base network into a new one and trained the rest on task B, with the copied layers either frozen or fine-tuned. Copying from base B into B ("BnB") isolates optimization effects; copying from A into B ("AnB") adds the effect of the task change.
Two distinct effects emerged. Frozen BnB networks lost accuracy when cut in the middle layers: neurons there are co-adapted, and retraining the upper layers alone could not recover the joint solution, an optimization difficulty. Frozen AnB networks lost more as grew, because higher layers are specialized to their original task. With fine-tuning, transferred initializations beat training from scratch, and the generalization boost lingered even after extensive fine-tuning. On a split into man-made and natural classes, transfer was weaker, yet still better than random features.
Transfer is not uniformly positive. Kornblith, Shlens & Le (2019) compared 16 architectures on 12 image datasets and found ImageNet accuracy strongly correlated with transfer accuracy, whether the networks served as fixed feature extractors or were fine-tuned. But common regularizers that slightly improve ImageNet accuracy made the penultimate-layer features much worse for transfer, and on two small fine-grained datasets ImageNet pretraining gave minimal benefit. He, Girshick & Dollár (2019) trained object detectors on COCO from random initialization and matched ImageNet-pretrained ones, given more iterations: pretraining sped up convergence but did not necessarily improve final accuracy. When transfer actively hurts the target task, it is called negative transfer.
In Yosinski et al.'s experiments, a network trained on task B is cut after a middle layer; the lower layers are frozen and the upper layers are retrained on the same task B. Accuracy drops. Why?
- The upper layers overfit, because task B has too few examples
- Features of middle layers are specific to a different task
- Neurons on the two sides of the cut were co-adapted, and retraining one side alone cannot recover the joint solution
- Freezing layers always lowers accuracy
Show the answer
C. Neurons on the two sides of the cut were co-adapted, and retraining one side alone cannot recover the joint solution
Nothing about the task changed, so specialization cannot explain the drop. It is an optimization effect: the original solution relied on interactions across the cut that gradient descent on the upper layers alone fails to rediscover. Fine-tuning the copied layers as well removes the drop.
A theory of learning across domains
Ben-David, Blitzer, Crammer, Kulesza, Pereira & Vaughan (2010) asked when a classifier trained on a source distribution works on a target . Write for the disagreement of two binary classifiers, and for the errors against each domain's labeling function, and define for a class
The divergence asks how differently the two domains make pairs of classifiers disagree; is the joint error of the best single hypothesis, attained by some .
Disagreement satisfies the triangle inequality, so go through the ideal hypothesis :
Measure the disagreement of and on the source instead. The pair is one of those in the supremum, so switching domains costs at most half the divergence:
A second triangle inequality, now on the source, gives . Substituting both bounds, the two errors of add up to :
With source error 0.05, and , the bound guarantees target error at most ; the divergence term dominates. That suggests learning features whose source and target distributions match, while keeping the source error small. Ganin et al. (2016) do this by domain-adversarial training: a domain classifier tries to tell the domains apart from , and its gradient is reversed before it reaches the feature extractor. But the bound has a third term, and matching features can inflate it.
Binary labels, features , predictions . Let be the Jensen–Shannon distance, the square root of the Jensen–Shannon divergence in bits, a metric bounded by 1. Two facts: processing cannot increase it, ; and a classifier with error has .
The triangle inequality through the predicted labels then links the label distributions of the two domains to the feature distributions and the two errors:
If , rearrange and use :
With perfectly invariant features the right side is . If 50% of source examples but 10% of target examples are positive, that is about : no predictor on top of such features can have joint error below 7.3%. When the label proportions differ, aligning the feature distributions misaligns the classes.
Fine-tuning, probing and low-rank adaptation
Fine-tuning every layer usually wins in distribution. Kumar, Raghunathan, Jones, Ma & Liang (2022) found that it can lose out of distribution: on 10 distribution-shift benchmarks, full fine-tuning averaged about 2% higher accuracy than a linear probe in distribution but about 7% lower out of distribution. Their explanation fits in a two-layer linear model.
Let with pretrained features and a new head . Fine-tune both by gradient descent on the squared loss , over training inputs that span a subspace smaller than . The gradient with respect to is a sum of outer products with the training inputs:
Every row of every update lies in , so the features change only on : for any direction , at every step.
An out-of-distribution input has a component outside . Its features are the old ones, but they are now read by a head that was co-trained with the changed features on , so the two halves of the prediction no longer fit together. Starting from a random head makes this worse: while the head is still wrong the residuals are large, so moves a lot on . Kumar et al. prove that in this setting fine-tuning from a fixed or random head has high out-of-distribution error, while a linear probe keeps intact.
LP-FT: first fit the head with the features frozen, then fine-tune everything. The head starts near its optimum, the residuals are small, and the features barely move. Empirically LP-FT beat full fine-tuning by about 1% in distribution and 10% out of distribution on their benchmarks.
Parameter-efficient fine-tuning changes few parameters by design. Adapters (Houlsby et al., 2019) insert small bottleneck layers into each block of a frozen network, initialized near the identity so training starts from the pretrained function; on the GLUE benchmark they came within 0.4% of full fine-tuning (80.0 against 80.4) while adding 3.6% parameters per task. LoRA (Hu et al., 2022) keeps a pretrained weight matrix fixed and trains only a low-rank correction to it:
is initialized randomly and at zero, so at the first step the adapted network computes the same function as the pretrained one. Once training ends, can be merged into , leaving inference as cheap as before. Against full fine-tuning of GPT-3 (175 billion parameters) with Adam, Hu et al. cut the trainable parameters by a factor of about 10,000 and the GPU memory by a factor of 3, with quality on par or better on their benchmarks.
LoRA with rank adapts a weight matrix. What percentage of that matrix's parameter count does it train?
Show the answer
0.78 %
parameters against : about . Every output change is a combination of the 4 columns of , so the correction can push the layer's outputs only within a 4-dimensional subspace.
Why would such tiny updates suffice? Aghajanyan, Gupta & Zettlemoyer (2021) measured the intrinsic dimension of fine-tuning: optimizing only 200 parameters, randomly projected into the full parameter space, brought a pretrained RoBERTa model to 90% of its full fine-tuning performance on the MRPC paraphrase task, and larger pretrained models tended to have lower intrinsic dimension. Pretraining seems to leave models where a low-dimensional adjustment is enough.
Many tasks: multi-task learning and meta-learning
Caruana (1997) trained related tasks together with shared hidden layers, and argued that what is learned for each task acts as an inductive bias for the others. Meta-learning pushes the idea further: learn, from many tasks, a starting point from which a new task can be learned from very few examples. Model-agnostic meta-learning (MAML; Finn, Abbeel & Levine, 2017) does this by differentiating through the adaptation itself.
For each task , one inner gradient step on its training loss adapts the shared initialization with inner step size :
The meta-objective scores each adapted model on held-out data from its task, and is minimized over the initialization:
By the chain rule, the Jacobian of the inner step is , so the meta-gradient involves the Hessian of the training loss, applied to a vector:
The Hessian–vector product costs about one extra backward pass. First-order MAML drops it and uses alone, which often works almost as well.
Does MAML learn to adapt quickly, or does it learn good features? Raghu, Raghu, Bengio & Vinyals (2020) found feature reuse dominant: restricting the inner loop to the final layer (their ANIL algorithm) matched MAML on standard few-shot image classification and reinforcement-learning benchmarks, and test performance was determined by the quality of the learned features. Meta-learning, on this evidence, is largely representation learning across tasks.
Why does the exact MAML gradient contain a Hessian, and what does first-order MAML do about it?
- The meta-loss is evaluated at parameters that themselves depend on through a gradient step, whose Jacobian is ; first-order MAML replaces it by the identity
- MAML uses Newton's method in the inner loop; first-order MAML switches to gradient descent
- The Hessian comes from the validation loss; first-order MAML removes the validation set
- The Hessian regularizes the meta-objective; first-order MAML replaces it by weight decay
Show the answer
A. The meta-loss is evaluated at parameters that themselves depend on through a gradient step, whose Jacobian is ; first-order MAML replaces it by the identity
, so . Dropping the second term saves a Hessian–vector product per task and keeps the gradient evaluated at the adapted parameters.
Key takeaways
- Lower layers transfer broadly and higher layers are task-specific; cutting co-adapted middle layers hurts optimization; fine-tuned transferred weights give a lasting generalization boost, but transfer can also be neutral or negative.
- follows from two triangle inequalities. When label proportions differ, perfectly invariant features force .
- Fine-tuning changes features only on the span of the training inputs, which can distort them out of distribution; probing the head first (LP-FT) limits the damage. LoRA trains a rank- correction with parameters, initialized at zero so that training starts from the pretrained function.
- MAML's meta-gradient is ; first-order variants drop the Hessian, and much of MAML's success comes from reusable features.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
In the bound , what does measure, and why can't adaptation fix it?
- The VC dimension of ; it is fixed by the architecture
- The divergence between the unlabeled input distributions
- The source error of the trained classifier
- The joint error of the best single hypothesis on both domains; if it is large, no works on both, and unlabeled target data cannot reveal that
Show the answer
D. The joint error of the best single hypothesis on both domains; if it is large, no works on both, and unlabeled target data cannot reveal that
involves target labels, which unsupervised adaptation never sees. It is why invariant features can fail: aligning distributions can raise in feature space.
According to Kumar et al. (2022), why does training the head first (LP-FT) protect out-of-distribution accuracy better than fine-tuning all layers with a randomly initialized head?
- It uses a larger learning rate for the features
- It freezes the features permanently
- With a near-optimal head the residuals are small, so the features move little, and pretrained features of directions unseen in training stay consistent with the head
- It trains on out-of-distribution data during the probing stage
Show the answer
C. With a near-optimal head the residuals are small, so the features move little, and pretrained features of directions unseen in training stay consistent with the head
Feature updates are proportional to the residuals and confined to the span of the training inputs. A random head produces large residuals and distorts the features on that span, mismatching them with the untouched directions; a probed head avoids most of that distortion.
In LoRA, the update is with initialized to zero and initialized randomly. Why zero for ?
- So that at the start and the adapted network initially computes the pretrained function
- To make the update full-rank
- So that receives no gradient
- To keep the number of trainable parameters small
Show the answer
A. So that at the start and the adapted network initially computes the pretrained function
With the adapted layer computes exactly initially. The gradient with respect to is nonzero (it involves ), so training moves away from the pretrained function smoothly.
In a binary task, 50% of source examples are positive but only 10% of target examples. You learn features whose distribution is identical in both domains. What does Zhao et al.'s bound imply?
- Target error equals source error
- The sum of source and target errors of any classifier on those features is at least about 0.07
- Target error can be made zero by training longer
- Nothing, because the bound applies only to linear classifiers
Show the answer
B. The sum of source and target errors of any classifier on those features is at least about 0.07
With the bound gives , about for label rates of 0.5 and 0.1. Identical feature distributions cannot carry different label proportions without errors.
Further reading
- S. Ben-David, J. Blitzer, K. Crammer, A. Kulesza, F. Pereira & J. W. Vaughan, "A theory of learning from different domains," Machine Learning 79(1–2), 151–175 (2010).
- J. Yosinski, J. Clune, Y. Bengio & H. Lipson, "How transferable are features in deep neural networks?," NeurIPS (2014).
- A. Kumar, A. Raghunathan, R. Jones, T. Ma & P. Liang, "Fine-tuning can distort pretrained features and underperform out-of-distribution," ICLR (2022).
- E. J. Hu, Y. Shen, P. Wallis, Z. Allen-Zhu, Y. Li, S. Wang, L. Wang & W. Chen, "LoRA: low-rank adaptation of large language models," ICLR (2022).
- C. Finn, P. Abbeel & S. Levine, "Model-agnostic meta-learning for fast adaptation of deep networks," ICML (2017).