MoonSay /Tutorials/Machine Learning Interview #4
Contents
  1. 01Introduction
  2. 02Overfitting and why we regularize
  3. 03L2 regularization
  4. 04L1 regularization
  5. 05The geometry
  6. 06Regularization paths
  7. 07A Bayesian view
  8. 08Early stopping
  9. 09Other ways to prevent overfitting
  10. 10Putting it all together
  11. 11Interview questions and model answers
  12. 12Summary
  13. References

08Early stopping: a regularizer in disguise

8.1The recipe

Train a flexible model by gradient descent and watch two curves: the training loss, which keeps falling, and the loss on held-out validation data, which falls, reaches a minimum and then rises as the model starts to memorize noise. Early stopping returns the weights from the validation minimum. In practice: evaluate the validation loss after every epoch, keep a copy of the best weights so far, and stop when the validation loss has not improved for a number of epochs called the patience; then restore the best weights. Prechelt (1998) compared formal stopping criteria; for example, the generalization loss \mathrm{GL}(t)=100\,\big(E_{\mathrm{va}}(t)/E_{\mathrm{opt}}(t)-1\big), the percentage by which the current validation error exceeds the best so far, and stopping once \mathrm{GL}(t) exceeds a threshold. In a study of 1{,}296 training runs on 12 problems with 14 stopping criteria, Prechelt (1998) found that “slower” criteria, which wait longer before stopping, improved generalization only slightly, by about 4\% on average, at the cost of about four times as much training. He also showed why patience is needed: real validation curves are bumpy, and one of his examples has 16 local minima before serious overfitting sets in, so stopping at the first increase is usually premature.

Figure 5(a) does this on the degree-15 problem of Figure 1, now with no penalty at all, trained by gradient descent from w=0 on the mean squared error \frac{1}{2n}\|y-Xw\|^2 with learning rate \eta=1/L=0.136, where L=7.34 is the largest eigenvalue of the Hessian. The validation error is lowest after 6{,}046 steps, at 0.1262; continuing to 10^6 steps lowers the training error from 0.0255 to 0.0186 but raises the validation error to 0.1453, a generalization loss of \mathrm{GL}=15.1\%. Meanwhile the largest weight grows from 1.39 at the best step to 7.03.

Early stopping. (a) Gradient descent with no penalty on the degree-15 problem of Figure 1, starting from w=0: the training error keeps falling while the validation error is lowest after 6{,}046 steps (gold); the shaded band marks a patience window, after which training stops and the best weights are restored. The dotted line is the noise level \sigma^2=0.09. (b) Why it regularizes, for a two-weight quadratic loss: gradient descent from w=0 (teal, then grey) travels close to the path of ridge solutions (dashed) as \lambda decreases from \infty to 0; stopping after four steps (gold) gives weights like ridge with a large \lambda. Data: computed for this tutorial from the same simulation as the companion video.
Figure 5. Early stopping. (a) Gradient descent with no penalty on the degree-15 problem of Figure 1, starting from w=0: the training error keeps falling while the validation error is lowest after 6{,}046 steps (gold); the shaded band marks a patience window, after which training stops and the best weights are restored. The dotted line is the noise level \sigma^2=0.09. (b) Why it regularizes, for a two-weight quadratic loss: gradient descent from w=0 (teal, then grey) travels close to the path of ridge solutions (dashed) as \lambda decreases from \infty to 0; stopping after four steps (gold) gives weights like ridge with a large \lambda. Data: computed for this tutorial from the same simulation as the companion video.

8.2Why early stopping acts like L2

Gradient descent starts from small weights and walks toward the training optimum; stopping early keeps the weights close to where they started, just as an L2 penalty keeps them close to zero (Figure 5b). For a quadratic loss the connection is exact enough to compute (Goodfellow et al., 2016, §7.8). Near its minimum w^\ast write the loss as

L(w)=L(w^\ast)+\tfrac12(w-w^\ast)^\top H(w-w^\ast),

with a symmetric positive definite Hessian H whose eigenvalues h_j measure the curvature in each eigen-direction. The gradient is H(w-w^\ast), so a gradient step gives w_{t+1}-w^\ast=(I-\eta H)(w_t-w^\ast). Starting from w_0=0 and applying this \tau times,

w_\tau=\big[I-(I-\eta H)^\tau\big]\,w^\ast,\qquad\text{so along eigenvector }j:\quad w_{\tau,j}=\big[1-(1-\eta h_j)^\tau\big]\,w^\ast_j . \tag{16}

Ridge on the same loss minimizes L(w)+\lambda\|w\|_2^2; setting the gradient H(w-w^\ast)+2\lambda w to zero gives

w_\lambda=(H+2\lambda I)^{-1}H\,w^\ast,\qquad\text{so along eigenvector }j:\quad w_{\lambda,j}=\frac{h_j}{h_j+2\lambda}\,w^\ast_j . \tag{17}

Both methods keep a fraction of w^\ast in each direction, a filter factor, and both keep the high-curvature directions and suppress the flat ones. They agree when (1-\eta h_j)^\tau\approx 2\lambda/(h_j+2\lambda). For the small-curvature directions, which are the ones regularization is about (\eta h_j\ll1 and h_j\ll2\lambda), (1-\eta h_j)^\tau\approx1-\tau\eta h_j and 2\lambda/(h_j+2\lambda)=1/(1+h_j/2\lambda)\approx1-h_j/(2\lambda). Equating the two:

\lambda\;\approx\;\frac{1}{2\,\eta\,\tau}. \tag{18}

The number of steps plays the role of 1/\lambda: stopping sooner means stronger regularization. (Goodfellow et al. (2016) use the penalty \frac\alpha2\|w\|^2 and therefore write \alpha\approx1/(\eta\tau); the factor 2 is only the convention.) The idea that gradient-based training of neural networks regularizes like a penalty goes back to Sjöberg and Ljung (1995); Yao et al. (2007) analyse early stopping of gradient descent in general function spaces.

Early stopping versus ridge. (a) Validation error of ridge as a function of \lambda (blue, as in Figure 1c) and of gradient descent as a function of the effective penalty \lambda_{\mathrm{eff}}=n/(\eta\tau) after \tau steps (teal): the two curves nearly coincide, and their minima (gold) are at \lambda=10^{-1.81} and \lambda_{\mathrm{eff}}=10^{-1.61}. (b) The two filter factors (16) and (17) as functions of the curvature h, for \eta=0.1, \lambda=0.5 and \tau=1/(2\eta\lambda)=10 steps: both pass the high-curvature directions and suppress the flat ones. Data: computed for this tutorial.
Figure 6. Early stopping versus ridge. (a) Validation error of ridge as a function of \lambda (blue, as in Figure 1c) and of gradient descent as a function of the effective penalty \lambda_{\mathrm{eff}}=n/(\eta\tau) after \tau steps (teal): the two curves nearly coincide, and their minima (gold) are at \lambda=10^{-1.81} and \lambda_{\mathrm{eff}}=10^{-1.61}. (b) The two filter factors (16) and (17) as functions of the curvature h, for \eta=0.1, \lambda=0.5 and \tau=1/(2\eta\lambda)=10 steps: both pass the high-curvature directions and suppress the flat ones. Data: computed for this tutorial.
Try itEarly stopping is ridge in disguise

Gradient descent from w=0 on the problem of Figure 5, with no penalty. Stop after \tau steps and compare with ridge at the matching penalty \lambda_{\mathrm{eff}}=n/(\eta\tau) (Worked example 8): the two fits stay close at every stopping time.

Worked example 8. In Figure 5(a) the loss is \frac{1}{2n}\|y-Xw\|^2, so by (18) stopping after \tau steps is like adding \frac{1}{2\eta\tau}\|w\|^2 to it; multiplied by 2n to match the convention (1), that is ridge with \lambda_{\mathrm{eff}}=n/(\eta\tau). At the best step \tau=6{,}046 with n=20 and \eta=0.136: \lambda_{\mathrm{eff}}=20/(0.136\cdot6{,}046)=0.0243=10^{-1.61}. Cross-validated ridge chose \lambda=0.0157=10^{-1.81}, the same order of magnitude, and ridge with \lambda=0.0243 has validation error 0.1281, against 0.1262 for early stopping and 0.1277 for the best ridge. Figure 6(a) shows the whole curves.

In practice. Early stopping is cheap (it saves training time instead of costing it), needs no change to the loss, and is used almost everywhere: neural networks, and boosted trees, where libraries such as XGBoost and LightGBM stop adding trees when a validation metric stops improving. Library defaults matter: Keras’s EarlyStopping callback stops after patience epochs without improvement but keeps the weights of the last epoch unless restore_best_weights=True is set (Keras team, 2026), and XGBoost’s train function with early_stopping_rounds returns the model from the last iteration and records the best one in best_iteration (XGBoost developers, 2026). Its cost is a validation set, and the result depends on the learning rate and on where training starts.

Pause and think. You halve the learning rate and keep everything else fixed. Roughly how should the best stopping step change?

Show the answer

By (18) the regularization depends on the product \eta\tau, so the best step should roughly double: the same amount of “distance travelled” from the starting point takes twice as many smaller steps.