LLM Lab

Overfitting

A model that matches its training data perfectly can still be useless. What counts is how it does on data it has never seen.

Where this sits in the training loop
Data→Model makes a prediction→Loss measures the error→Gradients (backprop)→Update the weights (optimiser)

Two kinds of error

The dark points are training data; the orange ones are held out. Both come from the same smooth curve plus noise. The model is a polynomial, and its degree sets how flexible it is.

1234567held-out errortraining error
Typical curves as the polynomial degree grows (x-axis): training error keeps falling, held-out error falls and then rises. The best model sits at the bottom of the held-out curve (dashed line), not at the lowest training error.

Degree 1 is too stiff: it misses the curve on both sets (underfitting). As the degree rises, training error always drops. Past some point the curve starts bending to chase individual noisy points, and held-out error goes up (overfitting). At degree 7 the curve passes through all eight training points exactly, with zero training error, and is useless between them.

Why it matters for LLMs

Large models can memorise training text. That is why evaluations use held-out test sets, and why a benchmark leaking into training data (contamination) makes scores look better than they are.

A worked example

A degree-1 polynomial (a straight line) is too simple for curved data: both errors are high, which is underfitting. Around degree 3 both errors are low. At degree 7 the curve passes almost exactly through every training point, so training error is close to zero, but it swings wildly between them and the held-out error climbs: overfitting.

What helps

More data is the strongest cure, because a wiggly curve cannot fit many points by chance. Regularisation penalises large weights, dropout randomly switches units off during training, and early stopping halts training when the held-out error starts to rise. Large language models are trained on so much text that they rarely see the same example twice, which keeps overfitting low during pretraining; fine-tuning on a small dataset is where it returns.

Easy to confuse

Check yourself · 1 of 3

Training error keeps falling as the degree rises, but test error starts rising. What is happening?

Check yourself · 2 of 3

Which model would you pick?

Check yourself · 3 of 3

Which of these reduces overfitting?

Read more in the books

  • Deep Learning, Goodfellow, Bengio, Courville: Ch. 5, section 5.2 (capacity, overfitting and underfitting) and Ch. 7, “Regularization for Deep Learning”.
  • Pattern Recognition and Machine Learning, Christopher Bishop: Ch. 1, section 1.1 (polynomial curve fitting).
  • Dive into Deep Learning, Zhang, Lipton, Li, Smola: Ch. 3, the section on generalization.
All books
Labpolynomial degree
How to read this
  • Teal dots train the model; orange dots are held out. The curve is a polynomial of the chosen degree fitted to the teal dots.
  • Drag the degree. The two numbers compare the error on training points with the error on held-out points.
training error
held-out error