Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
Overfitting explained: why models memorise training data, how to spot it with validation curves, and fixes like more data, regularisation and early stopping.
Quick answer: Overfitting happens when a machine learning model learns its training data too closely, including the noise, quirks and coincidences, instead of the general pattern underneath. It scores brilliantly on the examples it was trained on and poorly on new ones. It is like a student who memorises last year's exam answers: perfect on those exact questions, lost when the questions change. The tell-tale sign is a growing gap between performance on training data and on held-out data. The remedies are more data, a simpler model, regularisation, and stopping training before memorisation sets in.
A model is useful only if it works on data it has not seen. Doing well on the training set is a means, not the goal. The ability to perform on new data is called generalisation. For how a model is fitted to its training data in the first place, see how neural networks learn.
Every dataset is a mix of:
A model with enough capacity can fit both. Fitting the noise is overfitting.
Imagine fitting a curve through scattered points that roughly follow a gentle bend.
| Underfitting | Good fit | Overfitting | |
|---|---|---|---|
| The curve | A straight line that misses the bend | A smooth curve through the trend | A wild squiggle through every point |
| Training error | High | Low | Very low, near zero |
| Error on new data | High | Low | High |
| Diagnosis | Too simple | Balanced | Too flexible for the data available |
This is often described as the bias-variance trade-off:
Underfitting is high bias. Overfitting is high variance.
You cannot see overfitting by looking at training results alone. You need data the model has never trained on.
| Set | Used for |
|---|---|
| Training set | Fitting the model |
| Validation set | Choosing settings and deciding when to stop |
| Test set | A final, one-time estimate of real-world performance |
During training, plot the loss on the training set and on the validation set.
The widening gap between the two lines is overfitting. Google's Machine Learning Crash Course shows these curves with interactive examples.
For small datasets, cross-validation gives a steadier estimate: split the data into several parts, train several times with a different part held out each time, and average the results.
The most reliable cure. With more examples, coincidences average out and real patterns stand out.
Create extra training examples by modifying existing ones in ways that do not change the answer: flipping, cropping or recolouring images; adding background noise to audio; rephrasing text.
Fewer layers, fewer parameters, fewer input features, or a shallower decision tree.
Regularisation discourages complexity during training.
| Technique | How it works |
|---|---|
| L2 regularisation (weight decay) | Adds a penalty for large weights, keeping the model smooth |
| L1 regularisation | Pushes many weights to exactly zero, effectively selecting features |
| Dropout | Randomly switches off a fraction of neurons at each training step, so the network cannot rely on any single path |
| Early stopping | Halt training when validation performance stops improving, and keep the best version |
Early stopping is the simplest and most widely used. It follows directly from the curves described above. The training loop itself is covered in gradient descent explained.
Ensembles average the predictions of several models. Each overfits in a different way, and their errors partly cancel. Random forests are built on this idea.
Transfer learning takes a model already trained on a huge general dataset and adapts it to your small one. It needs far less data than training from scratch.
Sometimes the validation score looks excellent because the model has, in effect, seen the answers.
The model looks superb in testing and fails in production. Whenever results seem too good, suspect leakage. Split data before any preprocessing, and split time series by time.
A related trap is overfitting to the test set. If you repeatedly adjust your model after checking the test score, you gradually tune it to that particular test set. Keep the final test set untouched until the end.
Classical theory says that making a model bigger eventually makes overfitting worse. Modern deep learning produced a surprise: very large networks, with far more parameters than training examples, often generalise better. Test error can fall, rise, and then fall again as models grow, a pattern known as double descent.
The reasons are still being studied. The practical rules remain unchanged: hold data out, watch the gap, and regularise.
Large language models show a version of this problem too. They can reproduce passages that appeared many times in their training data, and scores on public benchmarks become unreliable if the benchmark questions leaked into the training set. How memorisation relates to made-up answers is discussed in why LLMs hallucinate.
When a model memorises its training examples, including their random quirks, and so performs badly on new data.
An overfitted model is too closely tailored to the training data. An underfitted model is too simple to capture the pattern at all. Both perform poorly on new data.
Compare training and validation performance. A large gap, or validation loss rising while training loss falls, indicates overfitting.
It usually helps, provided the new data is relevant and varied. More copies of the same biased or mislabelled data will not.
Overfitting is the central risk in machine learning: a model that looks excellent on the data you have and fails on the data you care about. The defence is discipline more than cleverness. Keep honest held-out data, watch the gap between training and validation, prefer simpler models and more data, and stop training when the validation score stops improving.