Visualized ML
Topics

B · Neural networks

Regularization & Overfitting

A model can memorise its training data and fail on new data; held-out data catches it, and a penalty on large weights prevents it.

Try it: B7 · Regularization & Overfitting

Intuition

A model's job is not to fit the training data; it is to do well on data it has never seen. A flexible model, with many features or many weights, can fit the training points perfectly by bending through every one of them, noise included. That is overfitting: training error near zero, test error high. A model that is too stiff misses real structure instead: underfitting.

Regularization is any technique that pulls a flexible model toward simpler solutions, without taking its flexibility away. The classic form adds a penalty on the size of the weights to the loss. Fitting a wiggle through noisy points needs large, finely balanced weights; with the penalty they cost more than the small gain in training error, so the model settles for a smoother curve.

A degree-9 polynomial fitted to 12 noisy points (filled) as the penalty strength λ sweeps from tiny to large. Hollow points are test data the fit never sees. Left: with tiny λ the curve wiggles through every training point; with large λ it flattens. Right: training error only rises with λ, test error is lowest in between.

The math

Add a penalty \(\Omega(\mathbf{w})\), weighted by \(\lambda \ge 0\), to the data loss:

\[ J(\mathbf{w}) = L(\mathbf{w}) + \lambda\,\Omega(\mathbf{w}) \]

L2 (ridge, weight decay)

\[ \Omega(\mathbf{w}) = \tfrac12\lVert\mathbf{w}\rVert_2^2 = \tfrac12\sum_i w_i^2, \qquad \nabla J = \nabla L + \lambda\mathbf{w} \]

A gradient descent step becomes \(\mathbf{w} \leftarrow (1 - \eta\lambda)\,\mathbf{w} - \eta\nabla L\): every step first shrinks all weights by a constant factor, hence the name weight decay. For linear regression the minimum has a closed form, \(\mathbf{w} = (X^\top X + \lambda I)^{-1}X^\top\mathbf{y}\); the \(\lambda I\) also makes the matrix invertible when features are redundant.

L1 (lasso)

\[ \Omega(\mathbf{w}) = \lVert\mathbf{w}\rVert_1 = \sum_i |w_i| \]

The pull toward 0 has the same strength however small a weight gets, so L1 drives many weights to exactly 0. It selects features. L2's pull weakens as a weight shrinks, so it makes weights small but rarely zero.

The Bayesian view

Minimizing \(J\) is finding the most probable weights given the data, under a prior belief that weights are small: L2 corresponds to a Gaussian prior, L1 to a Laplace prior. \(\lambda\) sets how strong that belief is compared with the evidence.

Worked example. One weight, loss \(L(w) = (w - 3)^2\), so without regularization \(w = 3\).

With L2 and \(\lambda = 2\): \(J = (w-3)^2 + w^2\), and \(J' = 2(w-3) + 2w = 0\) gives \(w = 1.5\). It shrinks, but stays nonzero for any \(\lambda\): \(w = 6/(2+\lambda)\).

With L1 and \(\lambda = 2\): \(J = (w-3)^2 + 2|w|\). For \(w > 0\), \(2(w-3) + 2 = 0\) gives \(w = 2\). With \(\lambda \ge 6\) the minimum is exactly \(w = 0\): the weight is switched off.

Other regularizers

  • Early stopping. Watch the error on held-out validation data during training and keep the weights from the epoch where it was lowest. For gradient descent on a linear model it is closely related to L2.
  • Dropout. During training, set each neuron's output to 0 with probability \(p\) (often 0.1 to 0.5) and scale the rest by \(1/(1-p)\). No neuron can rely on any particular other one, which acts like training a huge ensemble of thinned networks.
  • Data augmentation. Train on flipped, cropped, recoloured or otherwise altered copies of the data. It encodes what the model should ignore, and is often the strongest regularizer for images.
  • More data. Not a technique, but the most reliable cure: overfitting is the model knowing the training set better than the world.
  • Smaller model. Fewer features or weights limit flexibility directly, at the risk of underfitting.
  • Label smoothing and noise injection are further examples: anything that makes memorizing the training set harder.

Pitfalls

  • Choosing λ on the test set. λ is a hyperparameter: choose it on validation data or by cross-validation, then report the test error once. Otherwise the test error is optimistic.
  • Unscaled features. The penalty treats all weights alike, so a feature measured in large units (needing a small weight) is penalized less than one in small units. Standardize features first.
  • Penalizing the bias. The intercept only sets the output's level; shrinking it toward 0 adds error without reducing flexibility. Most libraries leave it out.
  • L2 with Adam. Adding \(\lambda\mathbf{w}\) to the gradient is not weight decay under Adam, whose per-weight scaling distorts it. Use AdamW (see Optimizers).
  • Huge networks. Very large networks can fit pure noise, yet often generalize well anyway; the classic bias-variance picture above is not the whole story ("double descent"). Regularization still helps them.

History

Andrey Tikhonov introduced L2 regularization for ill-posed problems in 1943; Arthur Hoerl and Robert Kennard brought it to statistics as ridge regression in 1970. Robert Tibshirani introduced the lasso in 1996. Dropout came from Geoffrey Hinton's group (Srivastava and others, 2012–2014) and was a key ingredient of AlexNet.

Connections

Builds on

Further reading