Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Teaching a trained model a new job
A pretrained model already knows a lot. Fine-tuning gives it a little extra practice on your task so it gets good at exactly that. Full fine-tuning lets every part of the model change. That works well, but with very little practice data it can memorise instead of learning, and it can forget its old skills. Gentle, short training and mixing in some old material help. The demo compares training from scratch, tuning only the last layer, and full fine-tuning as data grows.
Fine-tuning turns a general model into a maintenance-report classifier, a procedure assistant or a code helper for flight software, usually from a few hundred to a few thousand examples.
- 1. Full fine-tuning updates…
- 2. With very little data, full fine-tuning risks…
- 3. Forgetting old skills after fine-tuning is called…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Procedure: start from pretrained weights, add or reuse an output head, train on task data with a small learning rate (about 1e-5 to 1e-4 for LLMs), warm-up and decay, for a few epochs, and select the checkpoint on validation loss.
- Memory: about 16 bytes per parameter for mixed-precision Adam (112 GB for 7B) plus activations; each task needs its own full checkpoint (14 GB in bf16 for 7B).
- Overfitting: many epochs over a tiny set (200 steps × 32 on 30 examples is over 200 epochs) memorises it. Early stopping, weight decay, dropout and data augmentation help.
- Forgetting: the shared weights move to suit the new task, degrading old abilities. Remedies: lower learning rate, fewer steps, layer-wise learning-rate decay (lower layers change less), replaying pretraining or general data, or parameter-efficient methods that freeze most weights (F6-3).
- Head-only tuning (linear probe) is cheapest and forgets nothing in the frozen body, but here the output layer was shared with task A, so changing it forgot A.
- 1. Full fine-tuning states for a 7B model:
- 2. Layer-wise learning-rate decay makes lower layers…
- 3. Replay mixes into fine-tuning…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Full fine-tuning in code
Exercise 1.
- Count trainable parameters for different freezing choices.
- Compute layer-wise learning rates.
- Measure forgetting as the increase in loss on the original task.
Practice.
- Hugging Face
Traineror TRLSFTTrainerwithlearning_ratearound 1e-5–5e-5 for LLMs, cosine schedule, warm-up ratio 0.03–0.1, 1–3 epochs. - Use FSDP or DeepSpeed ZeRO for models that do not fit on one GPU; gradient checkpointing for long sequences.
- Hold out validation data and evaluate general benchmarks too, to detect forgetting.
- Prefer LoRA (F6-3) when data is small, many tasks need separate adapters, or memory is limited.
from transformers import AutoModelForCausalLM, TrainingArguments, Trainer
model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.2-1B", torch_dtype="bfloat16")
args = TrainingArguments("ft-out", learning_rate=2e-5, lr_scheduler_type="cosine", warmup_ratio=0.05,
num_train_epochs=2, per_device_train_batch_size=8, gradient_checkpointing=True,
eval_strategy="steps", eval_steps=200, load_best_model_at_end=True, bf16=True)
Trainer(model=model, args=args, train_dataset=train_ds, eval_dataset=val_ds).train()- 1. A typical full fine-tuning learning rate for LLMs:
- 2. Forgetting is detected by evaluating…
- 3. When data is small or memory limited, consider…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Why pretraining transfers. Task B needs the same attention skill as task A (find the token 8 back) plus a new output mapping. The pretrained model already has the skill, so 30 examples suffice to learn the mapping. From scratch, 30 examples cannot teach the attention pattern and the model memorises them.
Why a high learning rate hurts. Large updates on a tiny dataset move the weights far from the pretrained solution, overwriting the transferable skill while fitting the 30 examples. A small learning rate keeps the model near the pretrained basin, as an implicit regulariser.
Forgetting and interference. Parameters shared between tasks receive gradients only from the new task. Unless old data is replayed (or changes are constrained, as in EWC, E19-4), the old task’s solution drifts. Replay is a direct trade: with half of each batch spent on task A, task B learns more slowly in the same number of steps.
- 1. Pretraining helped task B because B reuses…
- 2. A small learning rate acts as…
- 3. Replay trades…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.How should a pretrained model be fine-tuned on very little data?
History.
- J. Howard and S. Ruder, “Universal language model fine-tuning for text classification” (ACL 2018).
- J. Devlin et al., “BERT” (NAACL 2019), fine-tuning for every task.
- A. Kumar et al., “Fine-tuning can distort pretrained features and underperform out-of-distribution” (ICLR 2022).
Exercise 2.
- Model: one Transformer block (causal attention plus a ReLU MLP, d = 32) with a task token at the start of each sequence.
- Task A (pretraining, 3000 sequences): each token copies the token 8 back with probability 0.9.
- Task B (30 sequences): each token copies the token 8 back and adds 1 (mod 10).
- Fine-tuning options (200 Adam steps each):
- full fine-tuning at learning rate 0.01 or 0.001;
- output layer only (0.01);
- full fine-tuning (0.01) with half of each batch replayed from task A;
- training from scratch on the 30 examples.
- Metric: held-out loss on both tasks (floor 0.50).
| model | loss on A | loss on B |
|---|---|---|
| pretrained base | 0.545 | 4.629 |
| full fine-tuning, lr 0.01 | 21.938 | 3.268 |
| full fine-tuning, lr 0.001 | 5.722 | 0.802 |
| output layer only | 6.179 | 0.670 |
| full fine-tuning + replay | 1.158 | 2.135 |
| from scratch on B | 16.565 | 3.968 |
- Pretraining makes 30 examples enough: fine-tuned models reach 0.67–0.80 on B, while training from scratch memorises the examples (3.97).
- The learning rate matters most: at 0.01, full fine-tuning overfits and destroys the transferable skill (3.27 on B); at 0.001 it works.
- Tuning only the output layer is best here, because B differs from A only in the output mapping.
- Forgetting is severe without replay: every non-replay option loses task A (5.4 to 21.9).
- Replay preserves A but slows B: 1.16 on A, 2.14 on B in the same 200 steps.
- Seeds: seeds 1 and 2 gave B losses 0.768/0.880 (lr 0.001), 0.666/0.740 (head only) and 3.167/3.265 (scratch); replay kept A at 1.126/1.138.
- Two designs I discarded:
- task B = copy 3 back needed a different skill, so pretraining gave no advantage and the two tasks contradicted each other;
- an attention-only model could not make the output mapping depend on the task token, so I added the MLP.
Full fine-tuning in AI, robotics and aerospace
- Domain assistants: fully fine-tuning mid-sized models on curated procedures and reports when data and compute allow.
- Safety: checking that general abilities (and safety behaviour) survive fine-tuning before deployment.
- Design lesson: small learning rates, early stopping and some replay; and try the cheapest adaptation first.
References
- J. Howard and S. Ruder (2018). “Universal language model fine-tuning for text classification.” Proc. ACL.
- A. Kumar et al. (2022). “Fine-tuning can distort pretrained features and underperform out-of-distribution.” Proc. ICLR.
- Y. Luo et al. (2023). “An empirical study of catastrophic forgetting in large language models during continual fine-tuning.” arXiv:2308.08747.