Search

Search papers and lessons

Fine-Tuning

Level 3 · 12 min read

Fine-tuning continues training a pretrained model on a new task with a small learning rate, adapting its general features instead of learning everything from scratch.

On this page

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

TL;DR

Fine-tuning takes a model that has already been trained on a large dataset and keeps training it on a smaller, task-specific dataset. The new training uses a small learning rate, so the model adjusts what it learned earlier instead of forgetting it and starting over. The new task then needs far less data and compute than training from scratch.

The risk is that aggressive updates on a small dataset overwrite useful general knowledge, or overfit the new data. Controlling how far the weights move is most of the craft.

Where you'll see it: adapting a language model to follow instructions or to answer in a company's style, training a medical image classifier from a general vision backbone, and the parameter-efficient methods such as Low-Rank Adaptation (LoRA) that fine-tune only a small add-on.

Intuition

A pianist learning a new piece

A pianist who has spent years on scales, rhythm and reading music does not start from zero when given a new piece. Their hands and ears already know most of what the piece requires. What they still need is a short, focused practice on the specific passages. Practise too hard, with huge changes to technique, and the old skills degrade.

A pretrained network is in the same position. Early layers of an image model learn edges and textures that are useful for almost any picture task. Later layers learn parts and objects. When you move to a new task with a few thousand labelled examples, most of that structure is reusable. Fine-tuning is the focused practice: keep the existing weights as the starting point and nudge them toward the new target.

Why it works with little data

Training from random weights needs enough examples to learn every feature from the ground up. With a few hundred labelled images that is hopeless, because the model has far more parameters than the data can constrain. Starting from pretrained weights gives the model a head start. It only has to move a short distance in weight space to fit the new task, and a short move is something a small dataset can determine. This is the same logic as Transfer Learning, with fine-tuning as the step where the weights are actually updated.

−101234502468weight wloss
  • new-task loss L(w)
Starting at the pretrained value w = 1, a step of size 0.1 moves to 1.4 and lowers the loss. A step of size 1 overshoots to 5 and the loss is no better than before.

The math

Setup

Let θ0\theta_0 be the pretrained weights and let D={(xi,yi)}i=1n\mathcal{D} = \{(x_i, y_i)\}_{i=1}^{n} be the new labelled dataset. Fine-tuning looks for weights that minimise the average loss on D\mathcal{D}, starting from θ0\theta_0:

L(θ)=1n∑i=1nℓ(fθ(xi), yi),θ←θ0 initially.\mathcal{L}(\theta) = \frac{1}{n} \sum_{i=1}^{n} \ell\big(f_\theta(x_i),\, y_i\big), \qquad \theta \leftarrow \theta_0 \text{ initially.}

Here fθf_\theta is the network, ℓ\ell is the per-example Loss Function (for classification this is usually Cross-Entropy Loss), and θ\theta denotes all the weights. The optimiser is ordinary Gradient Descent or a variant of it:

θt+1=θt−η ∇θL(θt).\theta_{t+1} = \theta_t - \eta\,\nabla_\theta \mathcal{L}(\theta_t).

The step size η\eta is the learning rate. The only thing that distinguishes fine-tuning from training from scratch is the starting point θ0\theta_0 and, usually, a smaller η\eta.

Why the learning rate is small

Take a one-dimensional toy loss L(w)=(w−3)2\mathcal{L}(w) = (w-3)^2, whose minimum is at w=3w = 3. Its gradient is 2(w−3)2(w-3). Starting from the pretrained value w0=1w_0 = 1, the gradient is 2(1−3)=−42(1-3) = -4, and one step gives

w1=w0−η⋅(−4)=1+4η.w_1 = w_0 - \eta \cdot (-4) = 1 + 4\eta.

The step size controls how far the weight travels. A small η\eta keeps the weight near θ0\theta_0, where the pretrained knowledge lives. A large η\eta can throw it far away. Fine-tuning therefore uses learning rates often ten or a hundred times smaller than pretraining.

Anchoring to the pretrained weights

A second way to protect the pretrained knowledge is to penalise distance from it. The objective becomes

Lanchored(θ)=L(θ)+λ∥θ−θ0∥2.\mathcal{L}_{\text{anchored}}(\theta) = \mathcal{L}(\theta) + \lambda \lVert \theta - \theta_0 \rVert^2.

This is an L2 Regularization term centred on θ0\theta_0 instead of on zero. The larger λ\lambda is, the more the model is pulled back toward its starting point. Letting λ\lambda grow very large pins θ\theta at θ0\theta_0, which is the frozen case described below.

Which layers to update

Two standard choices control how much changes:

  • Feature extraction. Freeze every pretrained layer and train only a new output head. Only the head's parameters receive gradients.
  • Full fine-tuning. Update every layer. Often the early layers get a smaller learning rate than the later ones, since the early features are the most general.

Partial freezing sits between these: freeze the first kk layers and fine-tune the rest.

Worked examples

Example
One gradient step with two learning rates

The toy loss is L(w)=(w−3)2\mathcal{L}(w) = (w-3)^2, the pretrained value is w0=1w_0 = 1, and the gradient there is −4-4.

  1. With η=0.1\eta = 0.1: w1=1−0.1×(−4)=1.4w_1 = 1 - 0.1 \times (-4) = 1.4. The loss falls from (1−3)2=4(1-3)^2 = 4 to (1.4−3)2=2.56(1.4-3)^2 = 2.56. Progress, and the weight stays near its pretrained value.
  2. With η=1\eta = 1: w1=1−1×(−4)=5w_1 = 1 - 1 \times (-4) = 5. The loss is (5−3)2=4(5-3)^2 = 4, exactly what it was before the step.
  3. The large step overshot the minimum at 3 by the same distance it started below it. A pretrained network with many parameters can be pushed into a bad region this way, and the pretrained features are lost along with the error.
  4. Fine-tuning uses the first kind of step, repeated over many small updates.
Example
Freezing most of a realistic classifier

Suppose a pretrained image backbone has 25 million parameters, and you add a new linear head for 2 classes on 2048 features. The head has 2048×2+2=4,0982048 \times 2 + 2 = 4{,}098 parameters. This is an illustrative setup, not a measured result.

  1. Feature extraction trains only the head: 4,0984{,}098 parameters.
  2. The fraction of the model being trained is 4,098/25,000,000≈0.016%4{,}098 / 25{,}000{,}000 \approx 0.016\%.
  3. With 1,000 labelled images, 4,098 parameters is a problem a small dataset can constrain. Twenty-five million parameters is not.
  4. Full fine-tuning updates all 25 million weights from the same 1,000 images. That is the regime where overfitting is most likely. The practical approach is to try feature extraction first, then unfreeze the last block and retrain with a smaller learning rate if validation accuracy is still improving.
Example
Watching the training curves

You fine-tune for 10 epochs and record training and validation loss.

  1. Epoch 1: training loss 0.80, validation loss 0.82.
  2. Epoch 5: training loss 0.31, validation loss 0.40.
  3. Epoch 10: training loss 0.09, validation loss 0.52.
  4. The validation loss bottoms out between epochs 5 and 10 and then rises while the training loss keeps falling. The gap, 0.52−0.09=0.430.52 - 0.09 = 0.43, is the signature of Overfitting. Stop around epoch 5, or use Early Stopping to do it automatically, and check the result on a held-out test split as described in Train, Validation and Test Sets.

Tradeoffs and failure modes

SituationProblemBetter choice
Few labelled examples, similar domainFull fine-tuning overfitsFeature extraction, then light unfreezing
Many labelled examples, very different domainFrozen features are a poor fitFull fine-tuning with a small learning rate
The model is very large and compute is limitedUpdating every weight is too costly in memory and timeParameter-efficient methods such as Low-Rank Adaptation (LoRA)
The target task drifts away from the pretraining dataCatastrophic forgetting: the model loses the general skills it started withAnchored objectives, lower learning rate, or mixing in data from the original task
The pretrained features do not suit the task at allNegative transfer: starting from them is worse than starting from random weightsTrain from scratch, or pick a different pretrained model

Practical points. Fine-tuning changes the model's behaviour on everything, not just the new task, so evaluate on the original capabilities too. Keep the same preprocessing and normalisation the pretrained model used, or its early features will be fed inputs it never saw. Finally, check for leakage: if the fine-tuning set overlaps with the data the base model was pretrained on, your validation numbers can be too optimistic.

History

Fine-tuning grew out of the early deep learning work on pretraining. Geoffrey Hinton and Ruslan Salakhutdinov's 2006 paper on deep autoencoders trained layers one at a time as a way to find good starting weights, and the weights were then refined with backpropagation. That recipe made deep networks trainable at a time when random initialisation often failed.

Yosinski and colleagues measured in 2014 how transferable features are across the layers of a convolutional network. They found that early layers learn general features and later layers grow more specific, which is the empirical basis for freezing some layers and fine-tuning others.

In language, ULMFiT and BERT in 2018 showed that pretraining a large model on unlabelled text and then fine-tuning it beat task-specific models on many benchmarks. Fine-tuning became the default way to use a large pretrained model. In 2021, LoRA showed that adding small low-rank matrices and training only those could match full fine-tuning on many tasks at a fraction of the cost. Later, instruction-following models were built by fine-tuning on examples of good responses, and then on human preference data.

Check yourself

QuestionWhat is the difference between fine-tuning and training from scratch, apart from the starting weights?Show answer

Fine-tuning usually uses a smaller learning rate and often trains for fewer steps, so the weights move only a short distance from the pretrained values. Training from scratch uses a larger learning rate and needs enough data to learn every feature.

QuestionFor the toy loss L(w)=(w−3)2\mathcal{L}(w) = (w-3)^2 starting at w0=2w_0 = 2, what is the gradient, and what does one step with η=0.25\eta = 0.25 give?Show answer

The gradient is 2(2−3)=−22(2-3) = -2. One step gives w1=2−0.25×(−2)=2.5w_1 = 2 - 0.25 \times (-2) = 2.5. The loss falls from 1 to 0.25.

QuestionYou have 500 labelled X-ray images and a large pretrained image backbone. Why might you freeze the backbone at first?Show answer

Freezing leaves only the small new head to train, which has few parameters the 500 images can constrain. Training or fully fine-tuning the backbone would have far more parameters than the data supports and would likely overfit. Unfreezing later, with a small learning rate, is a sensible second step if validation accuracy keeps improving.

QuestionA model fine-tuned on customer-support chats now answers general knowledge questions badly. What happened, and how would you reduce it?Show answer

This is catastrophic forgetting: the updates on the narrow data overwrote general skills. Reduce it with a smaller learning rate or fewer steps, an anchoring penalty toward the pretrained weights, or by mixing some general data into each batch during fine-tuning.

Sources