What does the chart show?
The points come from a smooth curve plus random noise. The model is a polynomial fitted to the ten training points with least squares. The degree sets how flexible the model is: degree 0 is a horizontal line, degree 1 a straight line, and degree 9 has ten parameters, enough to pass through all ten points.
The error is the mean squared distance between the model’s curve and the points. The training error is measured on the points the model was fitted to. The validation error is measured on the 40 new points and tells you how well the model does on data it has not seen.
Underfitting and overfitting
At a low degree the model is too rigid. It cannot follow the shape of the curve, so both the training and validation errors are high. This is called underfitting.
At a high degree the model becomes so flexible that it bends to the noise in these particular ten points. The training error approaches zero, but the curve swings wildly between the points and the validation error rises. That is overfitting: the model has learned what was random instead of the pattern.
In between is a range where the validation error is lowest. That is the model you should choose, and you find it by looking at the validation error, not the training error.
Why does the training error always fall?
A degree-5 polynomial can do everything a degree-4 polynomial can, and a little more. With least squares, the training error can therefore never rise as the degree rises. That is why a low training error on its own says almost nothing about how good a model is.
The noise sets a limit
The dashed line shows the variance of the noise. Even the true curve does not hit new points exactly, because the noise cannot be predicted. A validation error far below the line is luck with that particular sample, not a better model.
Press “New sample”
Each new sample draws new points from the same process. Notice that a high-degree model changes dramatically from sample to sample, while a straight line barely changes but is wrong in the same way every time. These are the two sources of error known as variance and bias. The best degree can also change between samples.
How to avoid overfitting
- Split your data. Use training data to find the parameters, validation data to choose the model, and a separate test set for the final assessment.
- Choose the model on validation error, not training error, and do not use the test set to choose, or it is no longer an honest test.
- Get more data. The more points there are, the harder it is for a flexible model to hit every point’s noise.
- Constrain the model. Use a simpler model, or add regularisation that penalises large parameters.
- Stop training in time. With neural networks you track the validation loss during training and stop when it starts to rise.
Try the whole workflow
In the Overfitting lab you first predict what will happen, compare your experiments in a table, choose one model based on validation error, and finally test it honestly on a test set you have not looked at. The lab is part of full access.
See the Overfitting lab Or watch a model learn with gradient descent, free
More explainers
- How self-attention works
- Backpropagation, step by step
- Byte pair encoding (BPE), step by step
- Temperature and top-p in language models
- What is a tokenizer?
- What is activation patching?
- What are logits?
- What are embeddings?
- What is prompt injection?
- How does tool calling work?
- What are steering vectors?
- What is LoRA?
- What is quantization?
- What are scaling laws?
The data on this page is synthetic: a sine curve plus normally distributed noise with a standard deviation of 0.25. It is computed in your browser.