Step 1 Understand it
The problem it solves. have so many that they can memorise their exactly, even with random , and then fail on new data. makes memorising harder or less attractive, so the network learns patterns that generalise. On small it is often the difference between a useful and a useless one.
New words in this topic (tap one to see what it means)
Watch it happen
Read the animation as text
- A network with two hidden layers.
- Training step 1, dropout rate 0.5: a random half of the hidden neurons are switched off (red). The rest are scaled up × 2 so the total signal stays the same size.
- Step 2: a different random half. No neuron can rely on any particular partner being there, so each must learn something useful on its own.
- Step 3: another random team. In effect, you train a huge number of thinned networks that share weights.
- At test time dropout is off: every neuron is used, with no scaling (that is why it was scaled during training). Forgetting model.eval() leaves dropout on, and predictions become random.
The idea
Tricks to stop a network memorising. turns off random during . shrinks weights. makes 'new' data, such as flipped or cropped images. stops when the validation starts to rise.
How it works, step by step
- Dropout: during training, zero each hidden with probability p (often 0.1 to 0.5) and scale the survivors by 1/(1 − p). Every step trains a different thinned network, so no neuron can rely on a particular partner.
- At , dropout is switched off (), and thanks to the scaling no change is needed.
- Weight decay: shrink every weight a little each step (AdamW's weight_decay), pulling towards simpler functions.
- Data augmentation: create new, label-preserving versions of each example — flips, crops, small rotations, colour changes for images; or for audio and text.
- Early stopping: track validation loss during training and keep the checkpoint where it was lowest.
- Other levers: a smaller model, more real data, label .
Picture it
xychart-beta title "Train vs validation loss (illustration)" x-axis "epoch" [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] y-axis "loss" 0 --> 2.5 line [2.2, 1.5, 1.1, 0.85, 0.65, 0.5, 0.38, 0.28, 0.2, 0.14] line [2.3, 1.6, 1.25, 1.05, 0.95, 0.92, 0.95, 1.02, 1.12, 1.25]
Analogy
Dropout is a football team that trains with random players missing, so nobody relies on one star.
Where the analogy breaks
Missing players happens only in training. At test time all neurons are used, so the output size must match. 'Inverted dropout' fixes this by scaling during training.
Worked example
With dropout p = 0.5 and four hidden activations [2, 4, 6, 8], one training step might keep [2, _, 6, _] and scale them by 2, giving [4, 0, 12, 0]. On each activation keeps its value. At test time all four pass through unchanged.
mask = (np.random.rand(*h.shape) > p) / (1 - p) # inverted dropout, drop rate p
h = h * mask # training onlyMust-know points
- Dropout is only active in training mode; the 1/(1 − p) scaling keeps the expected activation the same.
- Augmentations must keep the label correct: flipping a "6" makes a "9".
- Weight decay = regularisation for neural networks; use AdamW.
- Early stopping needs a validation set and saved checkpoints.
- More data is the most reliable regulariser of all.
- Large often use little or no dropout; small models on small data need more.
Common mistakes
- Leaving dropout on at evaluation time (forgetting model.eval()).
- Augmenting the validation or test sets.
- Augmentations that change the meaning: flipping text, rotating digits too far.
- Stopping on training loss instead of validation loss.
Step 2 Go further (optional)
Optional. The lesson above is written to be enough on its own. If you want a second explanation, start with the one marked Worth it.
-
Read
Understanding Deep Learning, Ch 9 (regularisation)
Simon Prince · free
- Read
Step 3 Practise
Try each exercise yourself first: on paper, out loud or in code. Then open the answer and compare it with yours.
-
ExplainSay it in plain words, with no notes.
Explain dropout with the football-team picture.
Show answer
A football team that trains with random players missing in every session. Nobody can rely on one star striker to score every goal, because the striker might not be there. So every player learns to be useful, and the team learns several ways to win. On match day, the whole team plays, and it is stronger and more flexible for it.
Dropout does the same to a network: during training it switches off a random set of neurons at each step, so no single neuron can carry the answer on its own, and the network can't memorise the training set through a few fragile pathways. At test time every neuron is switched on.
-
DeriveWrite the maths, or the steps, from memory.
With dropout rate 0.5, why must you divide the kept activations by 0.5 during training? Show what happens to the expected value.
Show answer
Dropout with rate 0.5 keeps each activation with probability 0.5 and sets it to 0 otherwise.
Without rescaling, the expected value of a kept-or-dropped activation a is 0.5·a + 0.5·0 = 0.5a. So during training, the next sees inputs that are half as big on average as at test time, when nothing is dropped. The network would be trained for one scale and used at another.
Dividing the kept values by 0.5 (the keep probability) fixes this: E = 0.5·(a / 0.5) + 0.5·0 = a. The expected value during training now matches test time, so at test time you can simply switch dropout off with no other change. This is "inverted dropout", what does.
-
BuildCode it from scratch in NumPy, with no ML library.
Train an overfitting on a small part of . Add dropout, then weight decay, then augmentation, then early stopping, and record validation after each.
Show answer
Train a large MLP (784 → 512 → 512 → 10) on only 1,000 MNIST images, so it overfits: training accuracy hits 100% while validation accuracy stalls. Then add one regulariser at a time and keep the ones before it.
Typical results on this run: the plain model is a little under 90% on validation; dropout and weight decay each add under a point; augmentation (small random shifts of each image, so the model never sees the exact same image twice) helps most; early stopping keeps the best epoch instead of the last. Exact numbers vary from run to run by a point, so the trend matters more than any single value.
pythonimport torch import torch.nn as nn from torchvision import datasets mnist = datasets.MNIST("data", train=True, download=True) X = mnist.data.float().unsqueeze(1) / 255.0 y = mnist.targets Xtr, ytr, Xva, yva = X[:1000], y[:1000], X[50_000:55_000], y[50_000:55_000] def shift(images): # augmentation: random shifts of up to 2 pixels dx, dy = torch.randint(-2, 3, (2,)).tolist() return torch.roll(images, shifts=(dy, dx), dims=(2, 3)) def run(dropout=0.0, weight_decay=0.0, augment=False, early_stop=False, epochs=60): torch.manual_seed(0) model = nn.Sequential(nn.Flatten(), nn.Linear(784, 512), nn.ReLU(), nn.Dropout(dropout), nn.Linear(512, 512), nn.ReLU(), nn.Dropout(dropout), nn.Linear(512, 10)) opt = torch.optim.AdamW(model.parameters(), lr=1e-3, weight_decay=weight_decay) history = [] for _ in range(epochs): model.train() for idx in torch.randperm(1000).split(64): xb = shift(Xtr[idx]) if augment else Xtr[idx] opt.zero_grad(); nn.functional.cross_entropy(model(xb), ytr[idx]).backward(); opt.step() model.eval() with torch.no_grad(): history.append((model(Xva).argmax(1) == yva).float().mean().item()) return max(history) if early_stop else history[-1] print(f"plain: {run():.1%}") print(f"+ dropout: {run(dropout=0.5):.1%}") print(f"+ weight decay: {run(dropout=0.5, weight_decay=0.1):.1%}") print(f"+ augmentation: {run(dropout=0.5, weight_decay=0.1, augment=True):.1%}") print(f"+ early stop: {run(dropout=0.5, weight_decay=0.1, augment=True, early_stop=True):.1%}") -
BreakShow when it fails, and why.
Leave dropout switched on at test time. What happens to the and to accuracy? Why?
Show answer
With dropout left on at test time, every prediction drops a random half of the units. Run the same image twice and you can get two different answers, and accuracy falls, because each prediction comes from a crippled, random sub-network instead of the full one. In the run below, the same trained model scores about 1.5 points lower with dropout on (about 90.6% against 92.3%), and the score changes every time you evaluate.
The fix: call model.eval() before evaluating or , and model.train() before training again. (Turning dropout on deliberately at test time and averaging many predictions, "Monte Carlo dropout", is a real technique for estimating uncertainty, but it is a choice, not an accident.)
pythonimport torch import torch.nn as nn from torchvision import datasets mnist = datasets.MNIST("data", train=True, download=True) X, y = mnist.data.reshape(-1, 784).float() / 255.0, mnist.targets torch.manual_seed(0) model = nn.Sequential(nn.Linear(784, 256), nn.ReLU(), nn.Dropout(0.5), nn.Linear(256, 10)) opt = torch.optim.Adam(model.parameters(), lr=1e-3) for idx in torch.randperm(20_000).split(64): opt.zero_grad(); nn.functional.cross_entropy(model(X[idx]), y[idx]).backward(); opt.step() Xva, yva = X[50_000:], y[50_000:] with torch.no_grad(): model.eval() print("eval mode:", round((model(Xva).argmax(1) == yva).float().mean().item(), 4)) model.train() # the mistake for _ in range(3): print("dropout on:", round((model(Xva).argmax(1) == yva).float().mean().item(), 4))
Step 4 Prove it
Check you understood
With dropout p = 0.2, what is a kept activation multiplied by during training?
1 / (1 − 0.2) = 1.25, so the expected value stays the same.
Why does dropout reduce overfitting?
No neuron can rely on any particular other neuron being present, so the network learns redundant, more general . It is like averaging many thinned networks.
Give two augmentations for photos of cats and one that would be wrong for handwritten digits.
Flips and random crops (or colour jitter) for cats. A horizontal flip or a large rotation is wrong for digits: it can turn one digit into another.
How does early stopping work?
Watch validation loss during training and keep the model from the epoch where it was lowest, even if training continues.
Tick each check when you can do it without notes
A topic is finished when you pass all four checks, not when you finish reading. Be strict: if you needed your notes, it is not ticked yet.
- Explain Say it in plain words, with no notes.
- Derive Write the maths, or the steps, from memory.
- Build Code it from scratch in NumPy, with no ML library.
- Break Show when it fails, and why.
0 of 4 checks ticked.
Saved in this browser. Back it up or move it to another device.