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:

VariantValidation (known templates)Test (new templates)Category (test)
Reference (seed 42)1.000.650.72
Seed 11.000.650.72
Seed 21.000.740.88
Learning rate 2e-50.970.490.78
Learning rate 1e-31.000.420.50
1 epoch0.990.600.89
Rank 41.000.580.79
Rank 641.000.610.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:

SignWhere to see it
A widening gap between validation and testEvaluation on a set split by source
Training loss close to zeroTraining log: the model recites
Better results with fewer epochs or a lower rateSeries of runs
Correct answers only on phrasings close to trainingReading 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:

SymptomProbable causeRemedy
Loss that explodes or becomes nanRate too high, numerical overflow in fp16Lower the rate, bf16 rather than fp16, gradient clipping
grad_norm spikesAberrant examples, batch too smallInspect the data, gradient accumulation
Loss that stalls from the startRate too low, wrong masking (nothing to learn)Check the labels (lesson 2.3), raise the rate
Training loss → 0, validation going back upOverfittingFewer 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

  1. 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.
  2. Change only one setting at a time, starting with the learning rate (a factor of 3 to 10 on either side).
  3. Decide on the source-split validation set, never on the test set (kept for the final decision).
  4. Treat a gap as real only if it exceeds the gap between seeds and passes a paired test (lesson 5.1).
  5. 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_norm and 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

  1. Run examples/03_sft_lora.py lora --seed=3 and --seed=4: what is the total spread between your seeds?
  2. Try --lr=5e-4 and --lr=1e-4: where is the optimum for your data?
  3. Plot the training loss from run.log_history for rates 2e-5, 2e-4 and 1e-3, and compare with their test scores.

Resources