Topic 5 of 5 · 5 min read
Dropout, weight decay and early stopping
Goal: Choose a regularization method for an overfitting network and predict its effect on the training and validation curves.
Regularization limits how closely a large network can fit its training data, so that it keeps performing on new data; you pick the method by reading the training and validation curves.
Check yourself · 1 of 3
Training loss keeps falling while validation loss starts rising. What does this indicate?
Why it matters
A network with enough weights can memorize its training set, noise included. The sign is a training loss that keeps falling while validation loss flattens and then rises. Regularization is the toolbox for this gap.
The main methods
- Dropout. During training, each hidden unit is zeroed with probability p (for example 0.1 to 0.5) on every step, and the rest are scaled up. The network cannot rely on any single unit, so it learns redundant features. At test time dropout is off.
- Weight decay (L2). Adds a penalty on large weights, pulling them toward zero. With Adam, use the decoupled form,
AdamW, because plain L2 interacts badly with Adam's scaling. - Data augmentation. Random flips, crops, noise or paraphrases create new training examples. Often the most effective method when data is scarce.
- Early stopping. Track validation loss each epoch and keep the checkpoint with the lowest value. Stop after it has not improved for a set number of epochs (the patience).
- A smaller model or more data. Less capacity or more examples attacks the cause directly.
Reading the curves
Take three runs of the same network:
| Run | Train loss | Validation loss | Reading |
|---|---|---|---|
| Baseline | 0.05 | 0.90 | Memorizing: big gap |
| With dropout 0.3 | 0.25 | 0.55 | Smaller gap, better validation |
| Dropout 0.9 | 1.40 | 1.45 | Underfitting: both high |
Regularization usually raises training loss a little and lowers validation loss. If both are high, you regularized too much, or the model is too small or undertrained, and the fix is the opposite.
In code
model = nn.Sequential(nn.Linear(100, 256), nn.ReLU(), nn.Dropout(0.3), nn.Linear(256, 10))
opt = torch.optim.AdamW(model.parameters(), lr=1e-3, weight_decay=0.01)
model.train() # dropout on
# ... training loop ...
model.eval() # dropout off for validation and inference
Before and after
Weak: "The model overfits, so add every regularizer we know."
Better: "Validation loss rises after epoch 8 while train loss falls. Add dropout 0.3 and augmentation, and keep the epoch with the lowest validation loss."
What goes wrong
- Forgetting model.eval(). Dropout stays active at test time and predictions become random.
- Tuning on the test set. Stopping or choosing p by test score leaks it. Use a separate validation set.
- Regularizing an underfit model. If training loss is already high, regularization makes it worse.
Examples
Choosing from curves
A text classifier reaches 99% training accuracy and 71% validation accuracy, with 4,000 labeled examples. The gap is large and data is small. First steps: add dropout, add weight decay through AdamW and augment text with paraphrases. Then stop at the best validation epoch. Getting more labeled data would help most but costs the most.
Dropout left on
preds = model(x_val) # model still in train mode
Two runs on the same input give different predictions because dropout is sampling units. Call model.eval() and wrap evaluation in torch.no_grad().
Key points
- Overfitting shows as falling training loss with flat or rising validation loss.
- Dropout randomly zeroes hidden units during training only, so call model.eval() for validation and inference.
- Weight decay penalizes large weights; with Adam, use the decoupled AdamW.
- Early stopping keeps the checkpoint with the lowest validation loss, using a patience window.
- If both training and validation loss are high, the model is underfitting and more regularization makes it worse.
Common mistakes
- Adding dropout before checking whether the model overfits at all.
- Leaving the model in train mode during evaluation, so dropout stays active.
- Picking the stopping epoch or dropout rate using the test set.
- Using plain L2 inside Adam and expecting the effect of true weight decay.
Check yourself · 2 of 3
When should dropout be active?
Check yourself · 3 of 3
What does early stopping do?
Practice items, not counted towards your mastery.
Start here
- Neural networks series (3Blue1Brown) (opens in a new tab)VideoFree
Animated walk through neurons, layers, gradient descent and backpropagation with the chain rule drawn out.
- Neural Networks and Deep Learning (Michael Nielsen) (opens in a new tab)BookFree
Free online book that derives backpropagation step by step and builds a digit classifier in Python.
- Deep Learning (Goodfellow, Bengio, Courville) (opens in a new tab)BookFree
The standard textbook; chapters on feedforward networks, optimization and regularization.
More for this topic
- Dropout: A Simple Way to Prevent Neural Networks from Overfitting (Srivastava et al.) (opens in a new tab)PaperFree
The original dropout paper: the method, the test-time scaling and results against other regularizers.