Overfitting, bias and variance
Why a model can shine on training data and fail in production, how to tell the two failure modes apart, and what to do about each.
The idea
Two ways of being wrong. First: you consistently aim two metres to the left. That is bias, a systematic error that more practice will not fix. Second: you scatter wildly around the centre. That is variance, and practice helps a lot.
How to tell them apart
| Observation | Diagnosis | Remedy |
|---|---|---|
| Training poor, validation poor | Bias, underfitting | Bigger model, better features, train longer |
| Training good, validation poor | Variance, overfitting | More data, regularisation, smaller model |
| Both good, production poor | Distribution shift | Re-check the test set, add current data |
Reading learning curves
Two curves over the number of training examples: training error and validation error.
- Both curves meet at a high level: bias. More data will not help.
- Large gap, validation still falling: variance. More data helps immediately.
- Large gap, validation flat: variance, but data alone is no longer enough.
- Training error near zero at moderate data volume: memorisation.
from sklearn.model_selection import learning_curve
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
import numpy as np
X, y = make_classification(n_samples=4000, n_features=25,
n_informative=6, random_state=0)
sizes, tr, va = learning_curve(
RandomForestClassifier(n_estimators=200, random_state=0),
X, y, train_sizes=np.linspace(0.1, 1.0, 6), cv=5, n_jobs=-1)
for n, a, b in zip(sizes, tr.mean(1), va.mean(1)):
print(f"{int(n):5d} train {a:.3f} valid {b:.3f} gap {a-b:.3f}")The decomposition
The third term is the ceiling on any achievable quality. Estimating it, through agreement between two independent annotators, is the single most useful piece of work to do before choosing a model.
Double descent
The classical U-curve holds for models below the interpolation threshold, that
is while parameters < data points. Beyond it, test error rises sharply at
first, peaks around parameters ≈ data points, and falls again, often below the
first minimum. Modern language models operate permanently to the right of that
threshold, which is why "smaller model to fight overfitting" is the wrong reflex
there.
Measuring distribution shift
A simple and harsh test: train a classifier to distinguish whether a record came from training or from production. If it reaches ROC-AUC well above 0.5, the distributions differ measurably and every validation figure is worthless for production. See MLOps.
Related courses and sources
Dropout
Randomly switching off neurons as regularisation. Simple, effective and still in use.
Simple, effective and still in use; readable without deep background.
Machine Learning Specialization
Andrew Ng's course in its reworked form. Regression, classification, neural networks and the mistakes that actually happen in practice. The maths is included.
Free to audit; the certificate costs. If you only want to understand it, you do not need one.
MIT 6.036 Introduction to Machine Learning
More formal than most online courses, with derivations rather than recipes. A good follow-up to a hands-on course when the question of why is still open.
For anyone left with the question of why after a hands-on course.
Teachable Machine
Train an image classifier in the browser, without code, in ten minutes. Demonstrates overfitting faster than any explanation.
For a first look without code; it demonstrates overfitting in ten minutes.
The Elements of Statistical Learning
The statistical view of machine learning, the reference on bias, variance and model selection for twenty years. Demanding, free as a PDF.
For readers with a statistics background; without one the entry is hard going.