Search

Search papers and lessons

Transfer Learning

Level 2 · 9 min read

Transfer learning reuses a model trained on one task as the starting point for another, so the new task needs far less data and compute.

On this page

  • Colour = topic mix
  • Calculus
  • Linear algebra
  • Probability
  • Neural network basics
  • Losses & regularization
  • Optimizers
  • Sign in to see your progress

TL;DR

Transfer learning means reusing a model trained on one task as the starting point for a different task. The early layers have already learned general patterns such as edges, textures and word shapes, so the new task only has to teach the model what is specific to it. This works with far less labelled data and compute than training from scratch.

Where you'll see it: almost every practical vision and language system. A network pretrained on ImageNet is adapted to spot defects on a factory line, and a language model pretrained on web text is adapted to answer customer emails.

Intuition

Think of someone who already plays the piano and then learns the organ. They do not relearn what a note is, how rhythm works or how to read music. They only learn the new instrument's keys and pedals. Most of the knowledge carries over, and only the instrument-specific part is new.

A neural network has the same structure. Its early layers respond to simple, general things, and later layers combine those into task-specific things. Training on a large dataset teaches the network a set of features, called a representation, that stay useful well beyond the original task. Transfer learning keeps those features and retrains only the part that is specific to the new task.

It exists because labelled data is expensive. A medical imaging team might have 2,000 labelled scans. A vision model started from random weights usually needs hundreds of thousands of examples to learn good features from nothing. Starting from weights someone else already paid for turns a data problem into a small adaptation problem.

flowchart LR
  A[Large source dataset] --> B[Pretrain: learn general features]
  B --> C[Keep the feature layers]
  C --> D[Small target dataset]
  D --> E[Train a new head, optionally fine-tune all layers]
  E --> F[Model for the target task]

The curves below are illustrative, not measured data. They plot test accuracy against the number of labelled target examples, in thousands. The pretrained model starts near its ceiling and needs only a few thousand labels to get close to it.

010203040500.40.50.60.70.80.91labelled examples (thousands)test accuracy
  • trained from scratch
  • pretrained, then adapted
Illustrative curves. The pretrained model starts near its ceiling and needs only a few thousand labels to get close to it.

The math

Write a model as a composition of two parts. The feature extractor gg has parameters θg\theta_g and maps an input u\mathbf{u} to a feature vector. The head hh has parameters θh\theta_h and maps features to a prediction: f(u)=hθh(gθg(u))f(\mathbf{u}) = h_{\theta_h}(g_{\theta_g}(\mathbf{u})).

Definition: Transfer learning

Let DS\mathcal{D}_S be a large source dataset and DT\mathcal{D}_T a much smaller target dataset. Transfer learning first finds feature parameters θg∗\theta_g^{*} by minimising a loss on DS\mathcal{D}_S. It then trains on DT\mathcal{D}_T starting from θg∗\theta_g^{*} rather than from random values.

Two ways to adapt

  • Feature extraction. Freeze θg\theta_g at its pretrained value θg∗\theta_g^{*} and train only the head:
min⁡θh∑(u,y)∈DTℓ(hθh(gθg∗(u)),y).\min_{\theta_h} \sum_{(\mathbf{u}, y) \in \mathcal{D}_T} \ell\big(h_{\theta_h}(g_{\theta_g^{*}}(\mathbf{u})), y\big).
  • Fine-tuning. Update all parameters on DT\mathcal{D}_T, usually with a smaller learning rate so the pretrained features are not destroyed.

For classification the loss ℓ\ell is usually cross-entropy. Both options use the same update rule as Gradient Descent, θ←θ−η∇θL\theta \leftarrow \theta - \eta \nabla_\theta \mathcal{L}, where η\eta is the learning rate. Training usually runs in mini-batches, as in Stochastic Gradient Descent.

Why starting point matters

Optimisation is a search, and where it starts decides where it can end up. Pretrained weights already sit in a region where the features are useful, so the search for the target task only has to make small moves. Yosinski and colleagues measured this in image networks. They found that the first layers learn generic filters, similar across tasks, while the last layers become specific to the source task. That is why the usual recipe keeps the early layers and replaces the top.

Worked examples

Example
One head update by hand

Frozen features f=(0.5,−1.0)\mathbf{f} = (0.5, -1.0), head weights w=(0.2,0.4)\mathbf{w} = (0.2, 0.4), bias b=0b = 0, label y=1y = 1, learning rate η=0.5\eta = 0.5. The output is p=1/(1+e−z)p = 1/(1+e^{-z}) with z=w⋅f+bz = \mathbf{w} \cdot \mathbf{f} + b, and the loss is L=−ln⁡pL = -\ln p.

  1. Forward pass: z=0.2(0.5)+0.4(−1.0)+0=−0.3z = 0.2(0.5) + 0.4(-1.0) + 0 = -0.3.
  2. Probability: p=1/(1+e0.3)≈0.4256p = 1/(1+e^{0.3}) \approx 0.4256. Loss: L≈0.854L \approx 0.854.
  3. Gradient: for sigmoid with cross-entropy, ∂L/∂z=p−y=−0.5744\partial L/\partial z = p - y = -0.5744. So ∂L/∂w=(p−y)f=(−0.2872, 0.5744)\partial L/\partial \mathbf{w} = (p-y)\mathbf{f} = (-0.2872,\ 0.5744) and ∂L/∂b=−0.5744\partial L/\partial b = -0.5744.
  4. Update with η=0.5\eta = 0.5: w=(0.2+0.1436, 0.4−0.2872)=(0.3436, 0.1128)\mathbf{w} = (0.2 + 0.1436,\ 0.4 - 0.2872) = (0.3436,\ 0.1128) and b=0+0.2872=0.2872b = 0 + 0.2872 = 0.2872.
  5. New output: z=0.3436(0.5)+0.1128(−1.0)+0.2872=0.3462z = 0.3436(0.5) + 0.1128(-1.0) + 0.2872 = 0.3462, so p≈0.5857p \approx 0.5857 and L≈0.535L \approx 0.535.

Interpretation: one step on the head alone cut the loss from 0.854 to 0.535. The feature extractor never changed. Only the mapping from features to output moved.

Example
How many parameters does the new head need?

Suppose the pretrained extractor outputs 512 numbers per image, and the new task has 10 classes. The backbone has 25 million parameters.

  1. Weights: one weight for each pair of feature and class, so 512 × 10 = 5120.
  2. Biases: one per class, so 10.
  3. Trainable total with feature extraction: 5120 + 10 = 5130.
  4. Share of the model: 5130 / 25,000,000 ≈ 0.02%.

Interpretation: feature extraction fits about five thousand numbers, which is why it can work with a few hundred labels per class. Fine-tuning all 25 million parameters on the same small set is far more likely to overfit.

Tradeoffs and failure modes

Transfer learning pays off when the target data is small and the source task is related. It helps least when the target data is large and the compute budget is generous, since training from scratch then has enough data to find good features on its own.

The main failure mode is negative transfer: the pretrained features are a poor fit, and starting from them is worse than starting from random weights. A second is catastrophic forgetting, where aggressive fine-tuning erases the general features. A third is overfitting the small target set, which the usual guard, early stopping and regularisation, addresses. Finally, a model adapted on one set of inputs can still fail in production when the inputs shift, which is the problem of Covariate Shift.

SituationProblemBetter choice
Small target set, similar domainFull fine-tuning overfitsFreeze the backbone and train the head
Medium target set, similar domainFrozen features miss target detailsFine-tune the top layers with a small learning rate
Very different domain, such as X-rays vs photosFeatures partly mismatched, negative transferFine-tune more layers, or pretrain on in-domain data first
Large target set and large computePretraining adds littleTrain from scratch, or pretrain on a larger in-domain set
Very large model, little computeFull fine-tuning is too costlyParameter-efficient methods such as Low-Rank Adaptation (LoRA)

If there are no labels for the target task at all, Zero-Shot Learning and pretraining with Self-Supervised Learning are the alternatives to try.

History

Transfer ideas predate deep learning. Rich Caruana's 1997 work on multitask learning argued that training one network on related tasks at once helps each of them, which is the same intuition in another form. Pan and Yang's 2010 survey gave the field its vocabulary of source and target domains. The practical turn came in computer vision around 2014, when networks pretrained on ImageNet, a large labelled image collection, were reused as feature extractors for other tasks. Yosinski and colleagues then measured which layers transfer, and found the early ones generic and the later ones specific.

In language, ULMFiT and BERT, both published in 2018, showed that pretraining a large model on unlabelled text and then fine-tuning it beat task-specific models on many benchmarks. That recipe, pretrain on a large corpus and then adapt, is now the default for language models and for much of the work on transformers.

Check yourself

QuestionName the two standard ways to adapt a pretrained model, and say what each one changes.Show answer

Feature extraction freezes the pretrained feature extractor and trains only a new head, so the general features are kept exactly. Fine-tuning updates all parameters, usually with a smaller learning rate, so the features adapt to the new task. Feature extraction suits small target sets; fine-tuning suits larger ones.

QuestionA head maps 256 features to 4 classes and includes a bias for each class. How many trainable parameters does it have?Show answer

Weights: 256 × 4 = 1024. Biases: one per class, so 4. Total: 1028.

QuestionIn the worked example, why did the loss fall after one update when the features never changed?Show answer

The features are fixed inputs, but the head weights are not. The gradient pointed toward a larger weight on the positive feature and a larger bias, which pushes the output toward the label 1. Moving the head in that direction lowers the loss for this example.

QuestionYour model was pretrained on natural photos and performs poorly on medical X-rays. Name a likely cause and a fix.Show answer

A likely cause is a domain gap, with early features that do not match the new images, possibly causing negative transfer. A fix is to fine-tune more layers, or to pretrain on unlabelled in-domain X-rays before adapting. Check the test inputs against the training inputs as well, since Covariate Shift can lower accuracy even after adaptation.

Sources