AI / ML · Guide

Overfitting & Regularisation

Why a model that scores 99% on training data can be worthless, and the levers that fix it.

— min read AI / ML

Memorising Is Not Learning

A model that scores 99% on data it has seen and 62% on data it has not has learned the training set, not the task. Generalisation — performance on data drawn from the same distribution but never seen — is the only score that means anything.

The classic framing is the bias-variance trade-off. High bias is a model too simple to capture the pattern: it is wrong in the same way everywhere, and it underfits. High variance is a model flexible enough to fit the noise: it is different every time you retrain it on a new sample, and it overfits.

SymptomDiagnosisResponse
Training and validation both poorUnderfittingMore capacity, better features, train longer
Training excellent, validation poorOverfittingRegularise, or get more data
Both good, test poorYou leaked the test setRebuild the split
Validation better than trainingUsually a bug or an easy splitCheck the split and the augmentation
The most common cause of a model that fails in production is leakage, not overfitting: a feature that encodes the answer, or preprocessing fitted on the whole dataset before splitting. Fit scalers and encoders on the training fold only.

L1 & L2 Penalties

Regularisation adds a penalty on the size of the weights, so the fit has to justify complexity. Two forms dominate, and they behave differently in a way worth remembering.

L2 (ridge, weight decay)L1 (lasso)
PenaltySum of squared weightsSum of absolute weights
EffectShrinks all weights smoothlyDrives some weights to exactly zero
Gives youStability with correlated featuresFeature selection for free
Use whenAlmost always, as the defaultYou want a sparse, interpretable model
# the strength of the penalty is a hyperparameter you tune on validation
ridge = Ridge(alpha=1.0).fit(X_train, y_train)     # L2
lasso = Lasso(alpha=0.01).fit(X_train, y_train)    # L1 — many coefficients hit 0

# in deep learning the same idea is weight decay on the optimiser
optimiser = torch.optim.AdamW(model.parameters(), lr=3e-4, weight_decay=0.01)
Penalties assume comparable scales. Regularising unstandardised features punishes whichever happens to be measured in small units, so scaling is a prerequisite rather than an optional step.

Dropout, Early Stopping & Augmentation

Neural networks have enough capacity to memorise almost any dataset, so regularisation is not optional. Three techniques carry most of the load.

TechniqueMechanismWatch for
DropoutRandomly zeroes activations during training, so no unit can be relied onMust be disabled at inference
Early stoppingStops when validation loss stops improvingNeeds patience, or it stops on noise
Data augmentationLabel-preserving transforms multiply the effective datasetA transform that changes the label is poison
Batch normalisationStabilises training, mildly regularisingInteracts awkwardly with small batches
More dataThe only one with no downsideUsually the expensive option
The honest ordering: more and better data beats every technique here. Regularisation is what you do when more data is unavailable, not a substitute for having enough of it.

Validate with cross-validation where the dataset is small, and with a fixed held-out split where it is large. For time series, split by time — a random split lets the model see the future, which produces a spectacular validation score and a useless model.

Interview Questions

What is the bias-variance trade-off?

Bias is error from a model too simple to capture the pattern; variance is error from fitting noise. Reducing one usually raises the other, and generalisation is the balance.

How do you detect overfitting?

Training performance far exceeds validation performance. If both are poor the model underfits; if both are good but test is poor, the test set has leaked.

L1 or L2?

L2 shrinks all weights smoothly and is the default. L1 drives some weights exactly to zero, giving feature selection and a sparser, more interpretable model.

Why must dropout be disabled at inference?

It exists to stop the network relying on any single unit during training. At inference you want the full network and a deterministic prediction.

What is data leakage?

Information about the target reaching the model when it should not — a feature encoding the answer, or preprocessing fitted before the split. It produces excellent validation scores and a model that fails in production.

How do you split time-series data?

By time, never randomly. A random split lets the model train on the future and score brilliantly on validation while being worthless in deployment.

Quick Quiz

1. Training accuracy 99%, validation 62% indicates…
2. L1 regularisation is distinctive because it…
3. Dropout at inference time should be…
4. Fitting a scaler before splitting causes…
5. Time-series data must be split…