Transfer Learning
A representation learned once can be reused everywhere. We cover the main mechanisms of reuse: feature extraction versus fine-tuning, the generic-to-specific gradient of features that sets the freeze boundary, the learning-rate discipline that keeps borrowed weights from being erased, domain adaptation when only the input distribution shifts, and the modern arc from supervised transfer to self-supervised foundation models.
╌╌╌╌
This builds on Representation Learning, which established what makes a representation good — disentangled factors, a distributed code, and respect for the data manifold — and closed on greedy layer-wise pretraining, the first method to reuse a learned representation. Here we take the reuse idea seriously as engineering: how to transfer a trained encoder to a new task, when it helps and when it hurts, and how the modern self-supervised recipe turns one representation into a foundation for everything downstream.
Transfer learning
A representation learned to solve one task is rarely specific to that task. The early layers of an ImageNet classifier compute oriented edges, color blobs, and textures, features useful for any vision problem. Transfer learning reuses that learned on a new task, paying the large pretraining cost once and amortizing it over many downstream problems.1
There are two regimes for the transfer, distinguished by whether the borrowed weights are held fixed or adapted.
The choice between the two is governed by target-set size and source–target similarity: small data favors freezing (fewer parameters to overfit), large and dissimilar data favors fine-tuning.2
| Scenario | Source–target similarity | Target data size | Recommended strategy |
|---|---|---|---|
| same domain, few labels | high | small | feature extraction (freeze all, train head) |
| same domain, many labels | high | large | fine-tune whole network at small rate |
| related domain, few labels | medium | small | freeze low layers, fine-tune top blocks |
| related domain, many labels | medium | large | fine-tune all; warm-start beats random |
| unrelated domain | low | any | transfer rarely helps; train from scratch |
| target unlabeled, source labeled | varies | none labeled | domain adaptation (align features) |
A worked example: feature extraction with shapes
Take a convolutional base pretrained on ImageNet (a VGG16-style stack) and reuse it on a target set of labeled cat/dog images. Feature extraction runs each target image through the frozen base once and caches the output.
The convolutional base holds roughly million weights; every one is frozen. The head is a single dense layer with trainable parameters, about of the network. Because the base never receives a gradient, its outputs are constant across epochs: precompute the feature maps once, store the matrix, and train the head on that cached matrix. This is why feature extraction is cheap.3 It is also why it resists overfitting on small data: a -parameter head fit to examples is a favorable ratio, whereas turning the full million weights loose on those same images would memorize them.
Caching only works while the base is frozen. The moment any base layer is unfrozen its outputs change every step, so fine-tuning cannot precompute and must run the full forward and backward pass over the base each iteration, which is one reason fine-tuning costs far more per epoch.
The generic-to-specific gradient of features
The mechanism behind transfer is the layer hierarchy: low layers learn generic features (edges, color blobs, phones, character n-grams) that recur in nearly every task in a modality; high layers learn task-specific features (an ImageNet dog-snout detector, a sentiment cue) that are useful only for the source task. Transferability falls monotonically as you climb the stack, and this gradient is what dictates the freeze boundary: freeze the layers whose features are generic enough to reuse verbatim, retrain the layers whose features have specialized to the source.
Two forces set the boundary. Freezing more layers means fewer trainable parameters, which helps when target labels are scarce (less to overfit) and hurts when the target differs from the source (the frozen features are wrong and cannot adapt). Unfreezing more layers adds capacity to adapt but demands enough target data to fit it. A practical default: freeze the bottom two-thirds, fine-tune the top third and the head, and move the boundary down only if the target is both large and unlike the source.
The freeze boundary has been measured directly. Yosinski et al. (2014, NeurIPS) split ImageNet into two disjoint halves, trained a network on one, and transferred the first layers to the other for each . Transferring the first one or two layers cost almost nothing; transferring the top layers hurt, and the damage grew with exactly because those layers had co-adapted to the source. That experiment is the empirical curve the diagram above draws: generic at the bottom, specific at the top, with a measurable crossover in between.
The learning-rate discipline for fine-tuning
Fine-tuning is not ordinary training. The borrowed weights already sit in a good basin, and the task is to nudge them, not to relearn them. Three rules follow.
Train the head first, then unfreeze. A freshly initialized head produces large, random gradients. If those flow back into the pretrained base before the head has converged, they overwrite the features being transferred. The standard sequence is: (1) freeze the base and train the head to convergence as feature extraction, then (2) unfreeze the top blocks and continue at a small rate. Skipping step (1) lets a random head's gradient destroy the pretrained representation on the first minibatch.4
Use a small learning rate. Fine-tuning uses a rate roughly one to two orders of magnitude below from-scratch training, so that each update moves the weights a short distance and the borrowed features are preserved. If pretraining used , fine-tuning typically uses to . Too large a rate erases the pretrained structure and the run reverts to training from scratch on too little data.
Discriminative (layer-wise) learning rates. Because transferability decays with depth, the layers should learn at different speeds: lower layers, being more generic, should barely move; upper layers, being more source-specific, need more adjustment. Assign each layer its own rate that grows with depth,
so the top layer trains at the base rate and each
layer below it is discounted by a factor (a common choice is
, giving a base layer rate hundreds of times smaller than the
head). This is discriminative fine-tuning: a continuous version of the freeze
boundary, where frozen
is the limit and every layer in between
gets a rate matched to how much its features should change.
Domain adaptation
A special case keeps the task fixed but changes the input distribution: a sentiment classifier trained on product reviews applied to movie reviews, or a detector trained on daytime images run at night. Here labels exist for the source but not the target.
The standard approach makes the representation domain-invariant by penalizing any statistic that separates source from target codes: minimizing a divergence jointly with the source classification loss, so that keeps what predicts and discards what reveals the domain. Geometrically, the encoder is pushed to collapse the two clouds of embeddings on top of each other while the source decision boundary stays put, so the boundary now cuts the target cloud in the same place.
The divergence-minimizing approach has an adversarial form. Domain-adversarial training (Ganin et al., 2016, JMLR) attaches a small domain classifier on top of that tries to tell source from target, and trains the encoder to fool it through a gradient-reversal layer: the encoder is pushed to make domain-agnostic at the same time as the label head is pushed to make predictive. At the saddle point the domain classifier is at chance — — and the label head transfers. It is the same align-the-clouds picture, with a learned adversary supplying the divergence instead of a fixed statistic.
When transfer helps, and when it hurts
Transfer is not free. Reusing imports whatever inductive bias the source task built into the features, and that bias helps only when source and target share the relevant structure. Three regimes:
- Positive transfer. Source and target share low-level structure and the target is small. The pretrained features are close to a good target encoder, so the head (or top blocks) adapts from a warm start with far fewer labels than training from scratch would need. This is the common case within a modality: any ImageNet base transfers to most vision tasks; any large language-model encoder transfers to most text tasks.
- Negligible transfer. The target is large and the source is unrelated. The warm start neither helps nor hurts much: with enough target data the network relearns whatever it needs, and pretraining is wasted effort but not damaging.
- Negative transfer. The source features encode structure that is actively wrong for the target, and the target is too small to correct it. Freezing then locks in the wrong features; even fine-tuning can leave the network stuck in the source basin. A detector pretrained to ignore color transferred to a task where color is the signal is worse than random initialization.
The deciding variable is the mismatch between on the two tasks, not merely the input shift. Domain adaptation handles the case where only shifts and is shared; when itself differs, the borrowed representation is optimizing for the wrong invariances and transfer degrades.
The modern arc: self-supervision and foundation models
Greedy pretraining used unlabeled data through a clumsy layer-wise proxy. Transfer learning reused supervised features. The synthesis of the two is self-supervised pretraining: train one enormous encoder end-to-end on a task whose labels are manufactured from the input itself (predict a masked-out token, the next token, or whether two augmentations came from the same image), then transfer that single representation to every downstream task.
The design of the pretext task is the central decision: it must be solvable only by learning features that also serve the real downstream tasks, so that the encoder is forced to model rather than exploit a shortcut. The families that work share that property.5
- Masked prediction. Hide part of the input and predict it from the rest, optimizing . Masking a token in a sentence forces the encoder to model syntax and semantics; masking a patch in an image forces it to model shape and texture. The label is the hidden piece itself, so no annotation is needed.
- Next-element prediction. Predict the next token in a sequence, . Modeling the conditional over a large corpus requires a representation of everything that predicts what comes next, which is most of language.
- Contrastive agreement. Produce two augmented views of the same input and train their codes to agree while disagreeing with views of other inputs. The encoder must discard the nuisance factors the augmentation varies (crop, color jitter) and keep the identity-preserving content — disentanglement pursued directly. For a similarity and temperature , the per-pair loss over a batch is
where is the other view of input and the are the other batch members. Minimizing it pulls matched views together and pushes mismatched ones apart, carving the code space into one cluster per underlying instance.
Each of these manufactures a supervised-looking objective out of raw data, so the encoder trains end-to-end at scale with the same backprop machinery as a labeled task, and the resulting transfers by feature extraction, fine-tuning, or prompting.
This collapses the pretrain–transfer pipeline into its modern form: one representation, learned once on web-scale unlabeled data, serves many tasks.
The architecture that made this scale to language and beyond is the Transformer: an attention-based encoder whose self-supervised pretraining objective (predict the next token over a trillion-word corpus) yields a representation general enough that a frozen model plus a prompt solves tasks it never saw at training time. Every property from the companion lesson recurs there at scale: the hidden states are distributed, the learned embedding space disentangles syntactic and semantic factors, and tokens that play similar roles cluster on a shared manifold.
| Era | Pretraining signal | How features are reused | Limitation |
|---|---|---|---|
| greedy layer-wise (2006) | unsupervised, layer-by-layer | init for supervised fine-tune | clumsy; superseded by better optimization |
| supervised transfer (2014) | labeled source task | feature-extract or fine-tune | needs a large labeled source |
| self-supervised (2018–) | pretext task from raw | fine-tune, head, or prompt | compute-hungry; data quality matters |
| foundation models (now) | web-scale self-supervision | one model, many tasks | cost, opacity, alignment |
Parameter-efficient adaptation
Fine-tuning as the standard references describe it updates the borrowed weights. Once a foundation model has billions of parameters, updating and storing a full copy per downstream task is untenable, and a family of parameter-efficient fine-tuning (PEFT) methods answers it by adapting the model while touching only a tiny fraction of its weights.
The dominant method is LoRA (low-rank adaptation; Hu et al., 2021, ICLR). Freeze the pretrained weight matrix entirely and learn a low-rank correction with , , and , so the adapted layer computes
Only and are trained — parameters instead of . For a layer and rank , that is trainable weights against million, a reduction, and the correction folds back into at inference so there is no added latency. The underlying assumption is the same manifold intuition from the companion lesson: the update a downstream task needs lies in a low-dimensional subspace, so a low-rank suffices.
Two neighbors round out the family. Adapters (Houlsby et al., 2019, ICML) insert
small bottleneck layers between the frozen blocks and train only those; prompt /
prefix tuning (Lester et al., 2021, EMNLP) freezes the entire model and learns a
handful of continuous soft prompt
vectors prepended to the input — the logical end of
the freeze boundary, where no model weight moves and adaptation lives entirely in the
input. All three share LoRA's economics: one frozen backbone, a few megabytes of
task-specific parameters each, swappable at serving time.
Takeaways
- Transfer reuses a learned encoder on a new task, paying the pretraining cost once. Feature extraction freezes it and trains only a head (cheap, overfits little, cacheable); fine-tuning unfreezes some layers at a small rate (adapts more, costs more). Choose by target size and source–target similarity.
- Features run generic-to-specific with depth. Low layers (edges, textures) transfer; high layers (source-specific detectors) do not. The freeze boundary sits at the crossover — measured by Yosinski et al. — and discriminative learning rates are its continuous form.
- Fine-tune by moving little: converge the head first so a random gradient cannot wreck the base, keep one to two orders below from-scratch, and scale it down with depth.
- Domain adaptation aligns when the task is shared but the input distribution shifts and the target is unlabeled — via a divergence penalty or, adversarially, a domain classifier the encoder learns to fool.
- Transfer can hurt. Negative transfer occurs when source features are wrong for the target and the target is too small to correct them; the deciding variable is the mismatch in , not merely the input shift.
- Self-supervised pretraining manufactures labels from raw data (masked prediction, next-token, contrastive agreement), yielding foundation models: one representation, learned once on web-scale data, adapted to many tasks — now most cheaply through parameter-efficient methods like LoRA.
Footnotes
- Goodfellow, Deep Learning, §15.2 — Transfer Learning and Domain Adaptation: reusing a source-task representation on a target task, and aligning marginals when only the input distribution shifts. ↩
- Chollet, Deep Learning with Python, §5.3 — Using a Pretrained Convnet: feature extraction (freeze) versus fine-tuning (unfreeze top blocks at a small learning rate), chosen by target-set size and similarity. ↩
- Chollet, Deep Learning with Python, §5.3 — Feature extraction without data augmentation: run the frozen convolutional base over the data once, cache the extracted feature maps, and train a small densely connected classifier on the cached features. ↩
- Chollet, Deep Learning with Python, §5.3 — Fine-tuning: the pretrained base is unfrozen only after the newly added head has been trained, because otherwise the large gradient from a randomly initialized head propagates through the network and destroys the learned representations; fine-tuning then proceeds at a low learning rate. ↩
- Goodfellow, Deep Learning, Ch. 15 / §15.1 — Representation Learning: pretraining a representation on an unsupervised objective that models so the learned features transfer to supervised tasks; the value of a pretext objective is that solving it requires capturing the structure of the data distribution. ↩
╌╌ END ╌╌