Deep learning and neural networksLoading progress…

Fine-tuning

Compare learned embeddings, transfer learning, fine-tuning and contrastive training objectives.

Adapting everything, and what it can cost

Fine-tuning continues training the pretrained parameters on the new task. It usually fits the new task better than a frozen encoder, but the parameters move away from where the old skills lived, and those old skills can degrade. This is often called forgetting.

One weight w. Task A (the pretrained skill) is best at w = 1. Task B (the new task) is best at w = 3. Fine-tuning minimises task B only.

fine-tuning steps3
stepswtask B losstask A loss
014
0
11.42.56
0.16
21.721.638
0.518
31.9761.049
0.953
42.1810.671
1.394
52.3450.429
1.808
62.4760.275
2.178
72.5810.176
2.498
82.6640.113
2.77
92.7320.072
2.998
102.7850.046
3.187
112.8280.03
3.342
122.8630.019
3.469
w ← w − 0.1·2·(w − 3) at 3 steps: B loss 1.049 (want ≤ 1.1), A loss 0.953 (want ≤ 1) ✓ balanced
One weight. The pretrained value is best for task A; fine-tuning drags it toward task B. Slide the step count.

Predict first. Is more fine-tuning always a quality improvement?

Chapter context

A representation is useful when its structure supports a downstream task. Learning a representation changes which examples appear similar, but those similarities inherit assumptions from training data and objectives.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

One contrastive loss for an anchor a and positive p is

L=−log⁡exp⁡(s(a,p)/τ)∑j∈Cexp⁡(s(a,j)/τ).L=-\log\frac{\exp(s(a,p)/\tau)}{\sum_{j\in\mathcal C}\exp(s(a,j)/\tau)}.

The candidate set includes the designated positive; other candidate definitions depend on the method. Temperature tau is positive and controls concentration.

Worked example

For a candidate set with positive logit ln 2 and one alternative logit 0, exponentiated weights are 2 and 1. The positive probability is 2/3 and loss is −ln(2/3)≈0.405465. This demonstrates the normalization; actual contrastive batches usually contain many more candidates.

Reference code

The following example uses the Python standard library and does not call a remote service.

from math import log, exp, isclose
logits=[log(2),0]
prob=exp(logits[0])/sum(exp(v) for v in logits)
assert isclose(prob,2/3)
assert isclose(-log(prob),.40546510810816444)

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

Embedding proximity is task-dependent. Frozen parameters can coexist with a trainable head. Parameter-efficient tuning still requires evaluation for forgetting and task failures.