Transfer learning and fine-tuning: start from a model that already learned
Say you want an image classifier but you only have a few hundred photos. Training from scratch will not go well. Instead, you take a model that already learned from a huge number of photos and adapt only its last part to your task. Change the amount of data and compare fitting 3 numbers on top of features the model already knows with fitting all 9 numbers from scratch.
Short answer. Transfer learning means using a model trained on another task as your starting point. The early layers of a neural network learn general features that are useful for many tasks, and the later layers learn features specific to the original task. So you keep the early layers as they are (freeze them) and adapt only the later ones. With little data that looks like the original, train only the head. With lots of data, fine-tune everything with a small learning rate.
This is a scaled-down toy. A real pretrained model has millions of numbers learned from a big dataset. Here the "pretrained body" is two directions (20° and 110°) set by hand, and we pretend they were learned on an original task. In the new task, the right answer is A inside the corner the two directions make. Both models are the same network (2 inputs → 2 features → 1 answer) and use the same gradient descent (1500 steps, learning rate 0.5). Only the starting point differs. The left one freezes the body and learns just the 3 head numbers. The right one starts from random numbers and learns all 9. The points are made up with a fixed seed, and n training points means the first n of the same 100.
Try this. ① At the start (10 points), head only gets 96% and from scratch gets 80.5%. In the right panel, the purple dashed lines (the two directions it found by itself) do not match the corner. ② With 20 points it is 99% and 86.3%. ③ Go all the way to 100 and it is 100% and 98.5%, a gap of only 1.5 percentage points. With lots of data, learning from scratch nearly catches up. ④ The from-scratch curve does not climb in a straight line. It is 98% at 40 points, then 95% at 45 and 95.3% at 60. It has to find the body as well, so the result swings with the data. ⑤ Drop to 5 points and both do poorly: 75.8% and 74%. Four of the 5 points are A, so almost nothing shows where B begins.
Definitions
The text below uses a small backbone and head. These are the textbook definitions. The ideas on this page are ways of training, so they are written in words instead of formulas.
- transfer learning
- A method that starts from the weights of a model trained on one task (the source task) and trains it on another task (the target task).
- pre-training, pretrained model
- Pre-training is training a model on a large dataset before it learns the target task. A model trained this way is called a pretrained model.
- backbone, head
- The head is the last layer that gives the output of the task (for classification, a score for each class). The backbone is the part in front of the head, which turns the input into features. In transfer learning the head is replaced with a new head that fits the number of classes in the target task.
- freezing, unfreezing
- Freezing takes the parameters of a layer out of training so they do not change. Unfreezing puts a frozen layer back into training. The trainable parameters are the parameters of the layers that are not frozen.
- feature extraction, fine-tuning
- Fine-tuning starts from the pretrained weights and trains further on data for the target task. When every layer is trained, it is called full fine-tuning. Feature extraction freezes the whole backbone and trains only the new head.
- catastrophic forgetting
- The effect where, when a neural network keeps training on a new task, the weights that did the original task move toward the new task and performance on the original task drops sharply.
1. What transfer learning is: general at the front, specific at the back
A neural network turns its input into new numbers (features) at every layer. Open up a network trained on images and the first layer learns simple patterns (stripes, color blobs) that look much the same whatever image dataset it was trained on. The deeper the layer, the more its features are tuned to that data and task. So the early layers of "a model that tells cats, dogs and cars apart" are still useful for "telling bad parts from good ones", and only the last layer (the head) is tied to the original classes.
Transfer learning uses this. You take a model already trained on a big dataset (a pretrained model), swap the head for one that matches the number of classes in your task, and train a bit more on your data. Everything before the head is called the body. Stopping the body from changing is called freezing it, and letting it train again is unfreezing.
In the lab above, the body already knows two directions. When the answer to the new task is easy to write with those two directions (when it looks like the original task), the head only has to decide "how much of each feature to add". A model learning from scratch must also find the two directions from the data, so it needs more data.
2. Three ways to do it
| Way | Frozen | Trained | Trainable in the section 3 example | Traits |
|---|---|---|---|---|
| Feature extraction (train the head only) | The whole body | The new head | 645 (1.1%) | Cheapest and fastest. Overfits less with little data. You can compute the body's output once, save it, and train the head many times. If the body's features do not fit the new task, it will not get better |
| Unfreeze some layers | The early block | The later block + head | 33,669 (57.6%) | Adapts only the later layers, which are specific to the original task. You have to choose how far to unfreeze |
| Full fine-tuning | Nothing | Every layer | 58,501 (100%) | Can push performance the furthest. Needs the most memory and compute, overfits with little data, and forgets what it learned more easily (section 7) |
All three start from the pretrained weights. Training from scratch means starting the same network from random numbers. That is the right panel of the lab.
3. How the formula is built: counting what you train
Let us count what freezing saves. The example is a small network: 64 inputs → layers 1 to 4 (128 outputs each) → a new head (5 classes). Each rectangle in the picture is one layer's grid of weights (width = number of inputs, height = number of outputs).
- One layer:
inputs × outputs + outputs. There is one weight for every line from an input to an output, and one bias per output. Layer 2 has128 × 128 + 128 = 16,512, layer 1 has64 × 128 + 128 = 8,320, and the head has128 × 5 + 5 = 645. - Full fine-tuning:
Σ(…). Add every layer:8,320 + 16,512 × 3 + 645 = 58,501. That is both the trainable count and the size of the whole model. - Freeze the early block:
Σ over unfrozen layers (…). Freeze layers 1 and 2 and they drop out of the Σ:16,512 × 2 + 645 = 33,669, 57.6% of the total. Why: frozen layers are never updated by the gradient, so there is nothing to count. - Head only:
trainable = Σ over unfrozen layers (inputs × outputs + outputs). Freeze layers 1 to 4 and you train 645 numbers, 1.1% of the total.
A smaller trainable count saves three things. There are fewer gradients to keep, fewer values that an optimizer like Adam stores for each trainable number (Adam keeps two), and if everything before a point is frozen, backpropagation can stop there. Frozen layers still do the forward pass (the prediction) as usual.
Try this. Press "Next" three times. The number at the bottom falls 58,501 (100%) → 33,669 (57.6%) → 645 (1.1%), and the gray rectangles spread in from the left. The head is the thinnest strip in the picture. Lab ① counts the same way: the body (4 weights, 2 biases) plus the head (3) makes 9, and freezing the body leaves 3.
4. By hand: one training step for the head
Take the first training point of lab ①, x = (0.07, 0.18), answer A, and train the head one step on it. The body is frozen and the head starts at (a₁, a₂, b) = (0, 0, 0). The head does the same job as logistic regression.
- Feature 1. The 20° direction is
(cos 20°, sin 20°) ≈ (0.94, 0.34).0.94 × 0.07 + 0.34 × 0.18 + 0.5 ≈ 0.627, times 1.5, givestanh(0.94) ≈ 0.735. - Feature 2. The 110° direction is
≈ (−0.34, 0.94).−0.34 × 0.07 + 0.94 × 0.18 + 0.5 ≈ 0.645, andtanh(1.5 × 0.645) ≈ 0.748. - Predict.
z = 0 × 0.735 + 0 × 0.748 + 0 = 0, sop = sigmoid(0) = 0.5. It knows nothing yet. - Gradient. The derivative of cross-entropy with respect to z is
p − answer = 0.5 − 1 = −0.5. Multiply it by whatever each head number multiplies:(−0.5 × 0.735, −0.5 × 0.748, −0.5) ≈ (−0.368, −0.374, −0.5). - One step. Move against the gradient with learning rate 0.5:
(a₁, a₂, b) = (0.184, 0.187, 0.25). Measure again:z ≈ 0.184 × 0.735 + 0.187 × 0.748 + 0.25 ≈ 0.525,p ≈ 0.628. It moved toward A.
The lab takes this step 1500 times, using the average gradient over the n training points. The body is frozen, so the features 0.735 and 0.748 never change during training. That is why, when you train the head only, you can compute the body's output once and save it.
5. A different learning rate for each layer
Even after unfreezing the body, you do not move every layer by the same amount. The new head starts from random numbers and has a long way to go. The pretrained layers already work and should move only a little. So pretrained layers get a smaller learning rate than the head (CS231n). Earlier layers hold more general features, so they change even less.
The ULMFiT paper turned this into a rule called discriminative fine-tuning. First pick the last layer's learning rate by training only that layer, then divide by 2.6 for each layer further down. If the head uses 0.001, the layer below uses 0.001 ÷ 2.6 ≈ 0.000385, and the one below that ≈ 0.000148. The paper found 2.6 by experiment. For another model you would pick again.
# PyTorch: freeze the body and swap the head for the new number of classes (5)
for p in model.parameters():
p.requires_grad = False # freeze
model.fc = torch.nn.Linear(model.fc.in_features, 5) # the new head is trained
# If you also unfroze the later block (layer4): one learning rate per group
for p in model.layer4.parameters():
p.requires_grad = True
optimizer = torch.optim.SGD([
{"params": model.layer4.parameters(), "lr": 1e-4}, # pretrained layer: small
{"params": model.fc.parameters(), "lr": 1e-3}, # new head: larger
], momentum=0.9)
A common order is to train the head first until it settles, then unfreeze the body. That way the large early gradients of a randomly started head do not flow back and shake the pretrained features. ULMFiT goes one step further and unfreezes one layer at a time, starting from the last (gradual unfreezing).
Put the two together, and the learning rate of the layer k layers below the head is:
ηk = η0 ÷ 2.6k
- k: how many layers below the head. The head is 0, layer 4 is 1, layer 1 is 4.
- ηk: the learning rate of that layer (η is read "eta").
- η0: the learning rate of the head. 0.001 in the lab below.
- 2.6: the number you divide by for each layer down (the value ULMFiT picked).
Green bar = learning rate of an unfrozen layer (length is proportional to the value), closed grey padlock = frozen layer (rate 0), orange = the layer unlocked in this step. The orange arrow shows the rate of the layer above being divided by 2.6.
6. When to use what
| Option | Good | Bad | Use it when |
|---|---|---|---|
| Train the head only (feature extraction) | Cheap and fast. Works with little data. You can save and reuse the body's output | Stuck if the body's features do not fit the new task | Little data that looks like the original task. Also the first baseline to try in any case |
| Unfreeze some layers | Adapts only the specific later layers. Medium cost | You must pick how far to unfreeze. More unfrozen means more risk of overfitting | A medium amount of data, or a task a bit different from the original |
| Full fine-tuning | Can raise performance the most | Lots of memory and compute. A full copy of the model per task. Overfits with little data and forgets more easily | Lots of data. Use a small learning rate |
| Train from scratch | Not tied to a pretrained model's design | Needs the most data and compute | No suitable pretrained model exists, or you must change the design a lot |
| LoRA | Freezes the original weights and trains only small matrices added on the side. Little to train and little to store | May not change the model as much as full fine-tuning | Adapting a big model (an LLM) to your data |
| Prompting only | No training. You can change it right away | Every call gets a longer input. There is a limit to how many examples fit | A few instructions and examples are enough. The first thing to try with an LLM |
Picking by amount of data and by how much the task looks like the original (the four cases from CS231n):
| Similar to the original task | Different from the original task | |
|---|---|---|
| Little data | Train the head only. Fine-tuning overfits easily. Even the later features are useful for the new task | Hard. Train only a head, but attach it to an earlier layer's output. The later layers are specific to the original task and fit worse |
| Lots of data | Fine-tune everything (or several later layers). With lots of data, overfitting is less of a worry | Training from scratch is possible, but starting from pretrained weights and fine-tuning everything is very often better |
7. Forgetting: learning the new and losing the old
If you keep training a network on a new task, the weights that handled the original task move toward the new task, and the network can no longer do the original one. This is called catastrophic forgetting. It is not a slow fading like in people. It happens because the same numbers get overwritten. The ULMFiT paper notes that language models suffered from it when fine-tuned with a classifier, and offers per-layer learning rates and gradual unfreezing as a fix.
- Freeze. Frozen layers do not change. In lab ①, the head-only model's two body directions are exactly the same as at the start. Keep the original head too and it still does the original task.
- Small learning rates, gradual unfreezing. Move less, forget less (section 5).
- Move the important weights less. EWC selectively slows down learning on the weights that matter for the original task (Kirkpatrick et al.).
- Mix in old data. Train on a little of the original task's data along with the new data.
- Leave the original weights alone. LoRA freezes the original weights and trains only a small add-on, so removing the add-on gives you the original model back.
8. In LLMs: pre-training → fine-tuning → LoRA → prompting only
The same idea forms a ladder for large language models. Each rung down changes fewer numbers and costs less.
- Pre-training. Train on a huge amount of text to predict the next word and build the body. It is by far the most expensive step, so it is usually done by whoever makes the model. It is the starting point we borrow.
- Fine-tuning. Train all the weights further on your own data (pairs of questions and good answers, for example). As in section 3, the trainable count is the whole model, so it takes lots of memory and you store a full copy per task.
- LoRA. Freeze the weights, add two small matrices beside a layer, and train only those. Like "head only" in section 3, the trainable count drops a lot, and you store only those small matrices.
- Prompting only. Change no weights at all. Put instructions and a few examples in the input to get the behavior you want (in-context learning). There is no training, but that text goes along with every call.
You usually try from the bottom up. If prompting is not enough, try LoRA. If that still is not enough and you have plenty of data and compute, do full fine-tuning. Looking up documents and adding them to the input (embedding search) is a different way to add knowledge without changing any weights.
Where it is used
- Image classification. Take a convolutional network trained on a big image dataset and swap its head for your classes (defective or fine, for example).
- Text classification and adapting LLMs. Adapt a pretrained language model to review sentiment or to answers in your company's voice (transformer, LoRA).
- Embeddings as features. Putting a small classifier on top of a pretrained model's embeddings is also "train the head only".
- Fields where labels are expensive. When it is hard to collect lots of labeled data, it lets you start with a little.
Frequently asked questions
Are transfer learning and fine-tuning the same thing?
Transfer learning is the wider term. Any time you use what was learned on one task for another task, that is transfer learning. Fine-tuning is one way to do it: you start from the pretrained weights and keep training on your new data. Freezing the body and training only the head (feature extraction) is also transfer learning, but it does not change the body's weights.
What learning rate should I use for fine-tuning?
There is no fixed number, so pick it with validation data. The direction is clear, though. Pretrained layers get a smaller learning rate than the new head, so the weights that already work are not shaken too much. The ULMFiT paper first picked the learning rate of the last layer, then divided it by 2.6 for each layer further down.
With lots of data, isn't training from scratch better?
It can be, but usually starting from pretrained weights is better or about the same. The CS231n notes say that even with a large dataset that is very different from the original, it is very often still helpful in practice to start from pretrained weights and fine-tune the whole network. In the lab above, training from scratch with 100 points nearly catches up at 98.5%, but it does not beat the head-only model (100%).
Words
| Plain words used here | Textbook term |
|---|---|
| Using a model that already learned as a starting point | transfer learning |
| Training on a big dataset first, and the model you get | pre-training, pretrained model |
| Starting from pretrained weights and training more | fine-tuning |
| Body, head | backbone, head (the head is also called the classifier) |
| Freezing the body and training only the head | feature extraction, linear probing |
| Freeze, unfreeze | freezing, unfreezing |
| A different learning rate for each layer | discriminative fine-tuning, layer-wise learning rates |
| Unfreezing one layer at a time from the last | gradual unfreezing |
| Numbers you train | trainable parameters |
| Losing the old task while learning a new one | catastrophic forgetting |
| Moving the important weights less | EWC (elastic weight consolidation) |
| Prompting only | in-context learning, few-shot prompting |
| The original task and the new task | source task, target task |
Sources
- Jason Yosinski, Jeff Clune, Yoshua Bengio, Hod Lipson, How transferable are features in deep neural networks?, arXiv:1411.1792. First-layer features are general, and features become specific toward the last layer.
- Stanford CS231n course notes, Transfer Learning. Fixed feature extractor and fine-tuning, the four cases of data size × similarity, a smaller learning rate for fine-tuned layers.
- Jeremy Howard, Sebastian Ruder, Universal Language Model Fine-tuning for Text Classification, arXiv:1801.06146. Per-layer learning rates (ηl−1 = ηl ÷ 2.6), gradual unfreezing, forgetting during fine-tuning.
- James Kirkpatrick et al., Overcoming catastrophic forgetting in neural networks, arXiv:1612.00796. Selectively slowing down learning on the weights important for the original task (EWC).
← Data augmentationSelf-supervised learning: representations without labels →