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

03L2 regularization: ridge regression and weight decay

3.1The penalty and its gradient: a spring on every weight

L2 regularization adds \lambda\|w\|_2^2=\lambda\sum_jw_j^2. Its gradient is

\nabla_w\big(\lambda\|w\|_2^2\big)=2\lambda w , \tag{3}

so the penalty pulls each weight toward zero with a force proportional to the weight itself, like a spring attached to every weight. Big weights are pulled hard; small weights are pulled gently; and as a weight approaches zero the pull fades away, so it never quite gets there. A gradient-descent step on L(w)+\lambda\|w\|_2^2 with learning rate \eta reads

w\;\leftarrow\;w-\eta\big(\nabla L(w)+2\lambda w\big)\;=\;(1-2\eta\lambda)\,w-\eta\,\nabla L(w) : \tag{4}

first shrink every weight by the same factor 1-2\eta\lambda, then take the usual step. That is why L2 regularization in neural networks is called weight decay (Hanson and Pratt, 1989; Krogh and Hertz, 1992). For example, with w=2, a loss gradient 0.5, \eta=0.1 and \lambda=0.05, the weight is first multiplied by 0.99 and then moved by -0.05, giving 1.93.

3.2The closed form

For linear regression the minimizer has a formula. Expand the objective:

J(w)=\|y-Xw\|_2^2+\lambda\|w\|_2^2=y^\top y-2w^\top X^\top y+w^\top X^\top Xw+\lambda w^\top w .

Its gradient is \nabla J(w)=-2X^\top y+2X^\top Xw+2\lambda w. Setting it to zero gives (X^\top X+\lambda I)w=X^\top y, so

\hat w_{\mathrm{ridge}}=\big(X^\top X+\lambda I\big)^{-1}X^\top y . \tag{5}

The Hessian 2(X^\top X+\lambda I) is positive definite for every \lambda>0, so this is the unique minimum, and the matrix is invertible even when X^\top X is singular, for example when there are more features than examples (p>n) or two identical columns. “Add \lambda to the diagonal” is exactly how Hoerl and Kennard (1970a) motivated ridge regression: it makes a nearly singular problem stable.

3.3What ridge does to each direction of the data

The eigenvalue picture of Theorem 1 explains what ridge does. With X^\top X=V\operatorname{diag}(d_j)V^\top, the least-squares and ridge solutions in eigen-coordinates are

v_j^\top\hat w_{\mathrm{ridge}}=\frac{d_j}{d_j+\lambda}\;v_j^\top\hat w_{\mathrm{LS}} . \tag{6}

Ridge multiplies the least-squares solution along each eigenvector v_j by the shrinkage factor d_j/(d_j+\lambda)\in(0,1). Directions in which the data vary a lot (d_j\gg\lambda) are well measured and kept almost intact; directions in which the data barely vary (d_j\ll\lambda) are poorly measured, dominated by noise, and shrunk almost to zero. This is ridge’s intuition in one sentence: trust the directions the data measure well, and distrust the rest. Hastie et al. (2009, §3.4.1) write the same result for the fitted values, X\hat w_{\mathrm{ridge}}=\sum_ju_j\frac{d_j}{d_j+\lambda}u_j^\top y, where u_j are the left singular vectors of X (the principal component directions).

Effective degrees of freedom. Least squares with p weights uses p degrees of freedom. Ridge uses fewer, and Hastie et al. (2009, §3.4.1) define the effective degrees of freedom as the sum of the shrinkage factors,

\mathrm{df}(\lambda)=\tr\!\big[X(X^\top X+\lambda I)^{-1}X^\top\big]=\sum_{j=1}^{p}\frac{d_j}{d_j+\lambda}, \tag{7}

which falls smoothly from p at \lambda=0 to 0 as \lambda\to\infty. In Figure 1, the 15-weight polynomial has \mathrm{df}=14.38 at \lambda=10^{-9}, 7.33 at the best \lambda and 1.36 at \lambda=10^2: the penalty turns a model with 15 knobs into one that behaves as if it had about seven.

Worked example 2. Use the two correlated features of Worked example 1, now with data X^\top y=(1,\;0.98). Least squares solves X^\top Xw=X^\top y; the determinant is 1-0.99^2=0.0199, so

\hat w_{\mathrm{LS}}=\frac{1}{0.0199}\begin{pmatrix}1&-0.99\\-0.99&1\end{pmatrix}\begin{pmatrix}1\\0.98\end{pmatrix}=\begin{pmatrix}1.497\\-0.503\end{pmatrix}.

Two nearly identical features get weights of opposite sign: a large positive weight on one and a negative weight on the other, which nearly cancel. With \lambda=0.1 the diagonal becomes 1.1, the determinant 0.2299, and \hat w_{\mathrm{ridge}}=(0.565,\;0.383): two moderate positive weights. In eigen-coordinates the least-squares solution has component 0.7036 along v_1=(1,1)/\sqrt2 and 1.4142 along v_2=(1,-1)/\sqrt2. Ridge multiplies them by 1.99/2.09=0.952 and by 0.01/0.11=0.091, giving 0.6699 and 0.1286. The well-measured “sum” direction is kept; the noisy “difference” direction is cut to a tenth.

Pause and think. Why does ridge never set a weight exactly to zero (unless the least-squares weight is already zero)?

Show the answer

Near zero the spring force 2\lambda w vanishes, while the loss gradient pulling the weight toward its preferred value stays finite. At w_j=0 the derivative of the penalty is 0, so any nonzero loss gradient moves the weight away from zero. In the orthonormal case the answer is explicit: \hat w_j/(1+\lambda) is zero only if \hat w_j is.