Search

Search papers and lessons

Early Stopping

Level 3 · 11 min read

Early stopping ends training once the error on held-out validation data stops improving, and keeps the weights from the best point, which guards against overfitting for free.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Learning basics
  • Losses & regularization
  • Linear algebra
  • Optimizers
  • Sign in to see your progress

TL;DR

Early stopping is a rule for when to end training. While the model trains, you measure its error on a validation set after each pass over the data, called an epoch. When that error has not improved for a fixed number of epochs, the patience, you stop and return the weights from the best epoch, not the last one. The rule needs no change to the model and no extra training, and it works because a model that has trained for less time has had less chance to fit the noise.

Where you'll see it: in the training loop of almost every neural network, from image classifiers to Fine-Tuning of large language models, and in gradient-boosted tree libraries that stop adding trees once a validation score stalls.

Intuition

Tasting the sauce

A cook simmering a sauce tastes it as it reduces. Early on it is thin and flat. Then it gets better, reaches a point where it is as good as it will get, and then starts to become too salty and concentrated. The cook does not decide in advance how long to simmer. They taste, and stop when it tastes best.

Training a model works the same way. At the start the model is bad at everything. Its training error and its error on new data both fall. At some point the error on new data stops falling and begins to rise, while the training error keeps going down. The model has started to learn the accidents of the training set. Early stopping is the cook's tasting spoon: it measures the error on data the model is not trained on, and it stops at the moment that error is lowest.

Training time is a dial

The number of passes over the data is a dial for how complex the model effectively is. Ten passes leave the weights close to where they started, so the model is simple. A thousand passes let the weights move far, so the model can fit fine details. Nobody sets the dial by reasoning about the right complexity directly. Early stopping sets it by looking at the one signal that matters, error on data the model has not seen.

024681000.51epochloss
  • training loss
  • validation loss
Training loss keeps falling. Validation loss falls, reaches its lowest point around epoch 3 to 4, then climbs. Early stopping picks that lowest point.

Each curve here is idealised, but real curves have the same shape. The gap between the two curves after the validation minimum is the overfitting that early stopping is designed to avoid.

The math

Setting up the rule

Let θt\theta_t be the weights after epoch tt. Let Lval(θt)L_{\text{val}}(\theta_t) be the validation loss at that point, computed on a validation set that the optimiser never uses for gradient updates. Define the best epoch so far

ts∗=arg⁡min⁡t≤sLval(θt),t^{*}_{s} = \arg\min_{t \le s} L_{\text{val}}(\theta_t),

which is the index of the lowest validation loss seen up to epoch ss. Here arg⁡min⁡\arg\min means "the index at which the minimum is attained". The patience pp is a positive whole number chosen in advance.

The stopping rule is: stop at the first epoch ss with

s−ts∗≥p,s - t^{*}_{s} \ge p,

meaning that pp consecutive epochs have passed without beating the best loss. The returned model is θts∗\theta_{t^{*}_{s}}, the weights from the best epoch, not θs\theta_s.

Algorithm form:

Algorithm
Early stopping with patience p
  1. Set the best loss to infinity, the best epoch to 0, and the counter to 0.
  2. Run one epoch of Gradient Descent updates on the training set.
  3. Compute the validation loss.
  4. If it is lower than the best loss so far, save the current weights, record the epoch, and reset the counter to 0.
  5. Otherwise add 1 to the counter.
  6. If the counter has reached pp, stop and restore the saved weights. Otherwise go to step 2.

Why training time acts like a penalty

Early stopping is often described as a regularizer, a method that limits complexity the way an explicit penalty does. The cleanest way to see the link is a one-dimensional quadratic loss. Suppose the loss is

L(w)=12λ(w−w∗)2,L(w) = \tfrac{1}{2}\lambda (w - w^{*})^2,

with curvature λ>0\lambda \gt 0 and best weight w∗w^{*}. Gradient descent with learning rate η\eta updates ww by wt+1=wt−ηλ(wt−w∗)w_{t+1} = w_t - \eta \lambda (w_t - w^{*}). Write the distance to the optimum as dt=wt−w∗d_t = w_t - w^{*}. Then dt+1=(1−ηλ)dtd_{t+1} = (1 - \eta\lambda) d_t, so each step shrinks the distance by the factor 1−ηλ1 - \eta\lambda.

Start from w0=0w_0 = 0, so d0=−w∗d_0 = -w^{*}. After tt steps,

wt=w∗(1−(1−ηλ)t).w_t = w^{*}\left(1 - (1 - \eta\lambda)^{t}\right).

Now add an explicit L2 penalty 12αw2\tfrac{1}{2}\alpha w^2 to the loss. Its minimiser is

wridge=λλ+α w∗.w_{\text{ridge}} = \frac{\lambda}{\lambda + \alpha}\, w^{*}.

Both answers are the true optimum multiplied by a shrink factor that is less than 1, so both pull the weight toward 0. For small ηλ\eta\lambda, the approximation (1−ηλ)t≈e−ηλt(1 - \eta\lambda)^{t} \approx e^{-\eta\lambda t} holds, and the early-stopped weight becomes w∗(1−e−ηλt)w^{*}(1 - e^{-\eta\lambda t}). When ηλt\eta\lambda t is small, 1−e−ηλt≈ηλt1 - e^{-\eta\lambda t} \approx \eta\lambda t, and when λ≪α\lambda \ll \alpha, λ/(λ+α)≈λ/α\lambda/(\lambda+\alpha) \approx \lambda/\alpha. Matching these first-order shrinkages gives α≈1/(ηt)\alpha \approx 1/(\eta t). The number of steps plays the role of an inverse penalty strength. Training longer means less regularization. The match is only approximate, and it becomes rough when ηλt\eta\lambda t is large, but it captures the main effect.

The validation curve is noisy

The validation loss is an estimate computed on a finite set, so it jitters from epoch to epoch even when the underlying trend is clear. That is why the rule uses patience instead of stopping at the first increase. A single bad epoch is usually noise, and stopping on it would stop too early.

Worked examples

Example 1: applying the patience rule to a table

Suppose the validation losses over the first ten epochs are:

Epoch12345678910
Validation loss0.900.620.480.410.390.400.420.410.430.44

Use patience p=3p = 3.

Example
Stopping at epoch 8
  1. Epochs 1 to 5 each set a new best. The lowest loss so far is 0.39 at epoch 5, so the counter is 0.
  2. Epoch 6: loss 0.40 is higher than 0.39. The counter becomes 1.
  3. Epoch 7: loss 0.42 is higher than 0.39. The counter becomes 2.
  4. Epoch 8: loss 0.41 is higher than 0.39. The counter becomes 3.
  5. The counter has reached p=3p = 3, so training stops after epoch 8.
  6. The restored model is the one from epoch 5, with validation loss 0.39.

Interpretation: the run went three epochs past its best point, and the weights saved at epoch 5 are the ones returned. The epochs 9 and 10 are never computed. Without early stopping, the last model would have had loss 0.44 on validation, which is worse by 0.05.

Example 2: how far a quadratic gets in t steps

Take η=0.1\eta = 0.1, curvature λ=2\lambda = 2, and optimum w∗=1w^{*} = 1. Then 1−ηλ=0.81 - \eta\lambda = 0.8 and the formula gives wt=1−0.8tw_t = 1 - 0.8^{t}.

Example
The weight as training proceeds
  1. After 1 step: w1=1−0.8=0.200w_1 = 1 - 0.8 = 0.200.
  2. After 5 steps: 0.85=0.327680.8^5 = 0.32768, so w5=0.67232≈0.672w_5 = 0.67232 \approx 0.672.
  3. After 10 steps: 0.810≈0.1073740.8^{10} \approx 0.107374, so w10≈0.893w_{10} \approx 0.893.
  4. After 20 steps: 0.820≈0.0115290.8^{20} \approx 0.011529, so w20≈0.988w_{20} \approx 0.988.

Interpretation: stopping at step 5 returns a weight of 0.67, which is a third of the way short of the optimum 1. That shortfall is a shrinkage toward 0, the same kind of effect as an L2 penalty. Stopping at step 20 returns a weight close to the optimum, with almost no shrinkage. The stopping time chooses how much shrinkage there is.

Tradeoffs and failure modes

Early stopping is cheap and usually a good default, but it has failure modes. Most come from the validation signal or from the patience setting.

SituationWhat goes wrongBetter choice
Validation set is smallIts loss is noisy, so the rule fires at a random epochUse more validation data, a larger patience, or smooth the curve before testing for improvement
Loss plateaus, then drops (warmup or learning-rate decay)Patience runs out during the plateau and training stops before the dropStart counting only after warmup, or raise the patience
Validation set is also used to report the final scoreThe reported number is optimistic, because the stopping point was chosen on that setKeep a separate [[train-validation-test-split
Metric is accuracy on few examplesAccuracy moves in coarse steps, so improvement is often zeroMonitor the loss, which is smooth, and report accuracy separately
Model is very large and data is plentifulValidation error can fall, rise, and fall again as capacity grows, which the Bias-Variance Tradeoff picture does not predictEarly stopping can still help, but read the full curve before trusting one stopping point
Compute budget is fixed and knownThe rule is an extra check, not a savingA fixed epoch count with a learning-rate schedule can be just as good

The most common mistake is trusting the first stopping point without checking the curve. Plot the validation loss. If it is still falling when training stops, the patience was too small. If it is flat for a long time, the rule wastes compute.

A second mistake is forgetting to restore the best weights. Stopping at the right epoch but keeping the last weights gives back the overfitted model.

Early stopping is also an alternative to explicit regularization, not a substitute for it in every case. In the quadratic example it behaved like an L2 penalty, and in practice people often use both. L2 Regularization sets the penalty directly; early stopping sets the effective strength through training time.

History

Using a held-out set to decide when to stop training was common practice among neural-network researchers in the early 1990s, when training small networks by gradient methods was slow and overfitting was the main obstacle to using them. Practitioners stopped by eye or by a simple rule, and there was no agreed formal version.

Lutz Prechelt's 1998 review, "Early Stopping - But When?", collected the stopping criteria in use at the time and compared them on benchmark problems. It showed that the choice of criterion mattered, and that patience-style rules were a reasonable default. Later work in the early 2000s, including Caruana, Lawrence and Giles on overfitting in backpropagation networks, gave the approach a more systematic footing, and it became part of the standard training toolkit as deep learning grew.

Early stopping caught on because it is almost free. It needs one extra forward pass on the validation set per epoch and no change to the model or the loss. Modern large-scale results have complicated the simple picture, since very large models can keep improving on validation data long after the point a classical analysis would predict. Even so, the rule remains a standard default, and the question it asks is still the right one: does the model do better on data it has not seen?

Check yourself

QuestionWhat is the patience in early stopping, and what does it protect against?Show answer

Patience is the number of consecutive epochs allowed without a new best validation loss before training stops. It protects against stopping on a single noisy validation score. Without it, one bad epoch would end training, even if the loss was about to fall again.

QuestionValidation losses for epochs 1 to 6 are 0.80, 0.60, 0.55, 0.57, 0.56, 0.58. Patience is 2. At which epoch does training stop, and which weights are returned?Show answer

Epoch 3 sets the best loss of 0.55. Epoch 4 (0.57) is higher, so the counter becomes 1. Epoch 5 (0.56) is still higher than 0.55, so the counter becomes 2, which equals the patience. Training stops after epoch 5. The weights returned are those from epoch 3, the best epoch, not from epoch 5.

QuestionWhy does early stopping restore the best weights rather than keeping the final weights?Show answer

The final weights are the ones from the last epoch run, and by then the model may have started overfitting, since the rule only fires after the validation loss has already risen for several epochs. The best epoch is the point where the validation error was lowest. Returning those weights gives the model that generalised best among the ones the run produced.

QuestionYou train a model with a learning-rate warmup. Validation loss is flat for 10 epochs, then drops steadily. You set patience to 5. What happens, and what would you change?Show answer

Training stops during the flat stretch, around five epochs after the plateau begins, before the loss drops. The rule saw a plateau and mistook it for overfitting. Better choices are to start counting only after warmup ends, or to raise the patience above the length of the plateau. Plot the full curve before trusting the stop, and keep a test set for the final score.

Sources