Regularisation
Everything that keeps a model from memorising its training data: weight decay, dropout, early stopping, and more data.
The idea
A student who memorises every past exam question aces the repeat and fails the new paper. Regularisation is everything that forces them to learn the principle instead of the answers.
The four levers
| Lever | What it does |
|---|---|
| More data | Memorising simply becomes too much work |
| Weight decay | Large weights are penalised, the model stays smooth |
| Dropout | Randomly disabled neurons force redundancy |
| Early stopping | Training ends before validation gets worse |
What helps when
- 01
First check whether there is overfitting at all
Only if validation loss rises while training loss falls. Otherwise regularisation is the wrong tool.
- 02
Data augmentation before algorithms
Flips, crops and colour jitter for images; back-translation and paraphrase for text. Nearly always stronger than a hyperparameter.
- 03
Tune weight decay
Over a logarithmic search across three orders of magnitude, judged on validation.
- 04
Early stopping with patience
Do not stop at the first worse value but after a fixed number of epochs without improvement.
# Early stopping, as it actually looks
best, patience, waited = float("inf"), 5, 0
for epoch in range(200):
val = train_one_epoch() # placeholder
if val < best - 1e-4:
best, waited = val, 0
save_best_model() # not the last one, the best one
else:
waited += 1
if waited >= patience:
breakThe formulas
L1 induces sparsity because the gradient of the absolute value is a constant
±λ, so small weights get pushed all the way to zero. For L2 the gradient is
2λθ and shrinks with the weight itself, so nothing reaches exactly zero.
Weight decay is not L2
Under Adam the two are not equivalent. L2 adds the penalty into the gradient, so
it is scaled by √v̂ and acts more weakly on parameters with large gradients.
AdamW instead subtracts the decay from the weight directly:
That difference is why AdamW consistently outperforms Adam with L2 in the loss on transformers.
Dropout, scaled correctly
At training time each activation is zeroed with probability p and the rest
scaled by 1/(1−p). The expectation stays the same, so nothing has to be
adjusted at inference. Forget the scaling and at p = 0.5 you get a model whose
outputs in production are systematically twice as large as during training.
Related courses and sources
Deep Learning
The standard work by Goodfellow, Bengio and Courville, free to read. Mathematically dense, complete, and in its foundational parts timeless.
For the systematic route. Strong as a reference, too dense as a first read.
Dropout
Randomly switching off neurons as regularisation. Simple, effective and still in use.
Simple, effective and still in use; readable without deep background.
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.
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.