Hyperparameter Optimization
Intermediate 14 min read Hyperparameter Optimization

Early Stopping and Pruning

Stop wasting compute on runs that can't win

Early stopping halts training when validation stops improving; pruning cuts entire search candidates that can't win. Together they turn tuning budgets from 'number of full runs' into 'total compute spent', which is what actually matters.

early stoppingpruningHyperbandracinglearning curvebudget
Before we start

What you will learn

  • Apply early stopping with a validation patience
  • Compare pruning mid-run candidates vs finishing all
  • Use Hyperband-style racing to spend budget on the best
Introduction

What is this?

Early stopping watches validation during a single run and halts when improvement stalls for n epochs — preventing overfitting and saving compute. Pruning generalizes that idea across a whole search: candidates whose early performance can't plausibly beat the best so far are canceled early (racing), and multi-fidelity methods like Hyperband allocate budget in growing bands so promising runs get more and doomed ones get killed. Tuning then optimizes compute, not just trial count.

Motivation

Why do you need it?

  • Full training runs dominate tuning budgets; most never win.
  • Validation curves stabilize early — the ranking of candidates barely changes mid-run.
  • Early stopping is also a regularizer: halting before overfit gives better validation performance.
  • Hyperband can solve tuning 5–10x faster for iterative learners (GBDT, NN).
Think about it

Mental model

Imagine

A hiring funnel that fast-tracks superstars and cuts stragglers

Think of

Pruning + early stopping

You don't give every candidate a full year on probation. Early promising work buys more runway; clear mismatches are cut in week one. The strongest finalist still gets the complete process — some runners just never go the full distance.

Mechanics

How it works

  • Early stopping: monitor validation loss each epoch; if no improvement in P epochs (patience), roll back to the best checkpoint and stop.
  • Restore-the-best is important — stopping at a low point is the point.
  • Pruning: search candidates report scores at fixed milestones; a candidate ≥ a distance below the incumbent best is terminated.
  • Hyperband: try R candidates with 1 unit of budget each, keep the top 1/η, double budget, repeat bands — batting averages dominate finishing.
  • Report best config + the compute saved (a KPI for tuning efficiency).

A band of Hyperband: budget grows, candidates shrink.

Band 1
Filter
Band 2
Filter
Winner
Real world

In production

  • An XGBoost trainer early-stops on `validation_evaluations` with early_stopping_rounds=50, restoring the best iteration.
  • A torch training loop checks `best_val < val - eps` each epoch and saves the best weights, halting at patience.
  • Optuna's MedianPruner + HyperbandSampler cut an 8-hour search to 90 minutes on a deep-learning job.
Guidelines

Best practices

  • Always restore the best epoch/checkpoint, not the last weight vector.
  • Use a validation split/curve that doesn't leak into the search's decisions (a racing budget is still model selection).
  • Pick patience proportional to loss noise: noisier losses need bigger patience to avoid premature stops.
  • Combine pruning with Bayesian sampling — the sampler learns which regions deserve the surviving budget.
Watch out

Common mistakes

  • Early stopping without restoring the best state — dropping to the tail of the curve.
  • Pruning on noisy per-epoch metric where one lucky batch kills a good config.
  • Using the tuned-validation data for early stopping AND for the final report — it gets reused, so it's a guide, not gospel.
  • Hyperband with too-frequent pruning, so no candidate ever reaches full budget and all underfit.
Root cause analysis

Common issues

Error
Early stopping halts but validation then improves after restoration
Root cause

Patience too short vs loss noise, or the halting rule saw a lucky low

Fix

Increase patience, smooth the monitored metric, and confirm the final eval uses restored weights.

Error
Pruning prematurely kills the eventual best config
Root cause

Per-milestone scores noisy or too-aggressive threshold

Fix

Filter less aggressively (keep more candidates), use a lower percentile pruner, and evaluate more points before cutting.

When things break

Debugging tips

  • Plot validation curves (metric vs epoch) — if the 'best epoch' is the last, patience was too short.
  • When pruning kills everything, lower the pruning threshold or reduce frequency of checks.
  • Log stopped-epoch + reason for every candidate; audits love that trace.
  • Reproduce the best config with a full-unpruned run once to confirm the pruned winner wasn't budget-starved.
Interviews

Interview checkpoint

Practice

Hands-on lab

Early Stop and Prune a Training Loop by Hand
30 min · intermediate
In progress
Objectives
  • Simulate learning curves and implement early stopping from scratch
  • Implement a median-pruner race over candidate hyperparameters
  • Compare compute spent with and without pruning

Early Stop and Prune a Training Loop by Hand

30 min · intermediate

0/4 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/2
1

After early stopping triggers, the deployed model should use:

2

Pruning spends compute only on candidates that could plausibly win.

Go deeper

Official documentation