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
Machine Learning Interview #4 · Tutorial

L1 vs L2 Regularization and Early StoppingHow to Prevent Overfitting

Ridge, lasso, elastic net and early stopping: the math, the geometry and how they connect

A tutorial companion to the video Machine Learning Interview #4: L1 vs L2 Regularization and Early Stopping PDF

01Introduction

1.1The question

“What is the difference between L1 and L2 regularization?” is one of the most common questions in machine learning interviews. The one-line answer is well known: both fight overfitting by penalizing large weights, L2 shrinks every weight a little, and L1 drives many weights to exactly zero, so it also selects features. The follow-ups are where candidates stumble. Why does L1 give exact zeros and L2 does not? Why is the L1 constraint drawn as a diamond? Why is early stopping called a regularizer? Is weight decay the same thing as L2? This tutorial answers all of them from one picture: a penalty is a force on each weight. L2 is a spring, whose pull is proportional to the weight and fades near zero. L1 is a flat tax, the same push whatever the weight’s size, so a weak feature cannot afford any weight at all.

1.2Prerequisites and notation

Prerequisites. Derivatives, gradients and the minimum of a quadratic function; vectors, matrices, eigenvalues and eigenvectors of a symmetric matrix; mean and variance; the normal distribution.

Notation. The training data are n examples with p features, collected in a matrix X\in\mathbb{R}^{n\times p} (row i is the input x_i, column j is feature x_{\cdot j}) and a target vector y\in\mathbb{R}^n. A linear model predicts \hat y=Xw with weights w\in\mathbb{R}^p. Unless said otherwise, features are standardized (mean 0, variance 1) and y is centred, so no intercept is needed. \|w\|_2^2=\sum_jw_j^2 and \|w\|_1=\sum_j|w_j|. We follow the video and write every regularized objective without a factor \tfrac12:

J(w)=\underbrace{\|y-Xw\|_2^2}_{\text{training loss (RSS)}}+\underbrace{\lambda\,\Omega(w)}_{\text{penalty}},\qquad \lambda\ge0 . \tag{1}

Books and libraries scale these terms differently. Hastie et al. (2009) put a \tfrac12 in front of the loss for the lasso, and scikit-learn’s Lasso minimizes \frac{1}{2n}\|y-Xw\|_2^2+\alpha\|w\|_1, which is (1) with \lambda=2n\alpha (Pedregosa et al., 2011). The value of \lambda therefore depends on the convention, the behaviour does not.

1.3A short history

Regularization began with ill-posed inverse problems, such as inferring the structure of the Earth’s crust from gravitational or electric fields measured at the surface: Tikhonov (1943); Tikhonov (1963) stabilized the solution of such problems, whose answer is extremely sensitive to noise in the measurements, by adding a penalty on the size of the solution, which is why L2 regularization is also called Tikhonov regularization. In statistics, Hoerl and Kennard (1970a) introduced ridge regression for nearly collinear predictors and proved that a small penalty always lowers the mean squared error of least squares. Neural-network researchers rediscovered the idea as weight decay (Hanson and Pratt, 1989; Krogh and Hertz, 1992) and noticed that stopping training early has a similar effect (Morgan and Bourlard, 1990; Prechelt, 1998). Tibshirani (1996) proposed the lasso, the L1 penalty that both shrinks and selects; Efron et al. (2004) found a fast algorithm for its whole solution path, and Zou and Hastie (2005) combined L1 and L2 in the elastic net. Good textbook treatments are Hastie et al. (2009, §3.4), Hastie et al. (2015), Bishop (2006, §3.1) and Goodfellow et al. (2016, Ch. 7).