Module 3 · Efficient fine-tuning
Hyperparameters, stability and overfitting
5 min read
Learning rate, number of epochs, rank, batch size: every setting changes the result… and so does chance. This lesson relies on a series of ten Forge training runs to answer three practical questions: which settings really matter, how to recognize overfitting, and how much confidence to place in a gap between two runs.
The series of experiments
All variants start from the same configuration — Qwen2.5-0.5B-Instruct, LoRA r = 16, α = 32, learning rate 2e-4, 2 epochs, batch of 8, cosine schedule with 5% warmup — and change only one setting:
| Variant | Validation (known templates) | Test (new templates) | Category (test) |
|---|---|---|---|
| Reference (seed 42) | 1.00 | 0.65 | 0.72 |
| Seed 1 | 1.00 | 0.65 | 0.72 |
| Seed 2 | 1.00 | 0.74 | 0.88 |
| Learning rate 2e-5 | 0.97 | 0.49 | 0.78 |
| Learning rate 1e-3 | 1.00 | 0.42 | 0.50 |
| 1 epoch | 0.99 | 0.60 | 0.89 |
| Rank 4 | 1.00 | 0.58 | 0.79 |
| Rank 64 | 1.00 | 0.61 | 0.75 |
The reading that stands out: the random seed moves the test score as much as any setting — from 0.65 to 0.74 for the same configuration. Without this information, one would have concluded that "seed 2", or any setting tested with it, was a major improvement.
The learning rate, setting #1
The learning rate is the only setting whose effect clearly exceeds the noise:
- Too low (2e-5): the model learns the format (100% valid JSON) but stays approximate on substance — urgency drops to 0.73. That is underfitting: more epochs would be needed.
- Too high (1e-3): 100% on validation, 0.42 on the test set, and only 0.50 on the category. The model clung even harder to the training templates' keywords: it memorizes instead of generalizing.
- In between (2e-4): the best measured compromise, in LoRA's usual range (1e-4 to 2e-4).
For full fine-tuning, the range is ten to twenty times lower (about 1e-5, lesson 3.1).
Overfitting, invisible on a bad validation set
The reference run's curves look perfect: validation loss goes from 0.0016 after the first epoch to 0.0001 after the second, "token accuracy" reaches 100%. Everything points to an excellent model… on validation, which shares the training templates (lesson 2.4). The test score plateaus at two thirds.
The signs of overfitting:
| Sign | Where to see it |
|---|---|
| A widening gap between validation and test | Evaluation on a set split by source |
| Training loss close to zero | Training log: the model recites |
| Better results with fewer epochs or a lower rate | Series of runs |
| Correct answers only on phrasings close to training | Reading errors, broken down by template |
Here, one epoch gives 0.60 and two epochs 0.65: the second epoch still helps a little, without the difference exceeding the noise. With a smaller or more repetitive training set, overfitting arrives earlier; the main remedy remains data diversity (lesson 2.2), not fine-tuning the number of epochs.
Training stability
Beyond final quality, a training run must be stable. TRL's logs show at each step the loss, the gradient norm (grad_norm) and the learning rate. The symptoms to watch:
| Symptom | Probable cause | Remedy |
|---|---|---|
Loss that explodes or becomes nan | Rate too high, numerical overflow in fp16 | Lower the rate, bf16 rather than fp16, gradient clipping |
grad_norm spikes | Aberrant examples, batch too small | Inspect the data, gradient accumulation |
| Loss that stalls from the start | Rate too low, wrong masking (nothing to learn) | Check the labels (lesson 2.3), raise the rate |
| Training loss → 0, validation going back up | Overfitting | Fewer epochs, more data, more diversity |
The progressive ramp-up of the rate (warmup, 5% of steps in Forge) and the cosine schedule avoid jolts at the start and end; gradient clipping (active by default at 1.0 in Transformers) limits aberrant steps.
A method for tuning without fooling yourself
- Set a reasonable reference (Forge's values are a good start for LoRA) and run it with three seeds: the gap between seeds is the noise threshold.
- Change only one setting at a time, starting with the learning rate (a factor of 3 to 10 on either side).
- Decide on the source-split validation set, never on the test set (kept for the final decision).
- Treat a gap as real only if it exceeds the gap between seeds and passes a paired test (lesson 5.1).
- When no setting exceeds the noise any more, stop tuning: the next lever is the data.
On Forge, this discipline leads to a clear conclusion: no hyperparameter demonstrably beats the reference. The generalization problem comes from the data, and that is where to act.
Key takeaways
- The random seed moves the test score from 0.65 to 0.74: always measure several seeds before concluding.
- The learning rate is the setting that matters: 2e-5 underfits, 1e-3 memorizes, 2e-4 is the right compromise for LoRA here.
- A contaminated validation set shows perfect curves while the test score plateaus.
- Watch loss,
grad_normand learning rate; warmup, cosine and clipping stabilize training.- One setting at a time, several seeds, a paired test — then, when nothing moves any more, work on the data.
Practice
- Run
examples/03_sft_lora.py lora --seed=3and--seed=4: what is the total spread between your seeds? - Try
--lr=5e-4and--lr=1e-4: where is the optimum for your data? - Plot the training loss from
run.log_historyfor rates 2e-5, 2e-4 and 1e-3, and compare with their test scores.