Skip to content
AI-grafen
EUniversityDeep learning· about 60 min· fundamentals that rarely change· verified 2026-09-20· EN

Regularisation: dropout, weight decay, early stopping

Be able to apply regularisation and measure the effect on the validation loss.

Prerequisites

Intuition

Regularisation = anything that makes the model less inclined to memorise.

MethodWhat it doesWhen
Weight decay (L2)penalises large weightsnearly always, 0.01–0.1
Dropoutzeroes a random share p of the activations during trainingfully connected layers, p = 0.1–0.5
Early stoppingstop when the validation loss turnsalways — it is free
Data augmentationmore variants of the same exampleimages, audio; often the largest effect
Label smoothingthe target becomes 0.9 instead of 1.0classification, reduces overconfidence

The rule: more data beats all regularisation. If you cannot get more data, augment. If you cannot augment, regularise.

Code

import torch, torch.nn as nn

model = nn.Sequential(
    nn.Linear(784, 256), nn.ReLU(), nn.Dropout(0.3),
    nn.Linear(256, 128), nn.ReLU(), nn.Dropout(0.3),
    nn.Linear(128, 10),
)
opt = torch.optim.AdamW(model.parameters(), lr=1e-3, weight_decay=0.01)
criterion = nn.CrossEntropyLoss(label_smoothing=0.05)

best, patience, waited = float("inf"), 5, 0
for epoch in range(100):
    model.train()                      # dropout ON
    for xb, yb in train_loader:
        loss = criterion(model(xb), yb); loss.backward(); opt.step(); opt.zero_grad()
    model.eval()                       # dropout OFF — otherwise you are measuring noise
    with torch.no_grad():
        val = sum(criterion(model(xb), yb).item() for xb, yb in val_loader) / len(val_loader)
    if val < best - 1e-4:
        best, waited = val, 0; torch.save(model.state_dict(), "best.pt")
    else:
        waited += 1
        if waited >= patience:
            print(f"early stop at epoch {epoch}, best val {best:.4f}"); break
model.load_state_dict(torch.load("best.pt"))

The most common mistake: forgetting model.eval() at evaluation. Dropout is then active and the validation loss looks randomly worse — and early stopping fires on the wrong grounds.

Mastery means

  • Applies dropout, weight decay and early stopping
  • Measures the effect on the validation loss
  • Knows which method suits which problem

Sign in to do the exercises and build your mastery up.

Sources

All the sources and licences