Course map
Stage 1: Foundations
Stage 2: Machine Learning
Stage 3: Deep Learning
Stage 4: LLMs & Generative AI
Stage 5: Production & Specialization

Part 11 / Topic 5 of 6

Early stopping

Validation loss falls, bottoms out, then rises while training loss keeps falling — the moment memorisation starts. Early stopping watches validation, keeps the best weights, and quits after patience epochs without improvement:

# examples/02_early_stopping_keep_the_weights_from.py (abridged)
if val < best - 1e-4:
    best, best_state, bad = val, copy.deepcopy(model.state_dict()), 0
else:
    bad += 1
    if bad >= patience:
        break
model.load_state_dict(best_state)
# stopped at epoch 28; best epoch was 23 (val loss 0.244)

Note both numbers: training ran to 28 but the model you keep is from 23. Loading back the best state is the half people forget.

🧪 Quick quiz

  1. What does early stopping do?
Show the answer

Stops when validation loss stops improving and restores the best weights — here it stopped at epoch 28 and kept epoch 23's weights.

Done here? The practice set for Part 11 — 4 exercises with solutions — is on the part page.

Search the guide