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

04L1 regularization: the lasso

4.1The penalty and its gradient: a flat tax

The lasso (Tibshirani, 1996) adds \lambda\|w\|_1=\lambda\sum_j|w_j|. Away from zero its derivative is

\frac{\partial}{\partial w_j}\,\lambda|w_j|=\lambda\operatorname{sign}(w_j)\qquad(w_j\neq0), \tag{8}

a push of constant size \lambda toward zero, whatever the size of the weight. Figure 2(b) contrasts the two forces. The L2 spring weakens as the weight shrinks; the L1 push does not. It is a flat tax: a strong feature pays it and keeps most of its weight, but a weak feature, whose contribution to the fit is worth less than the tax, cannot afford any weight at all and is pushed all the way to zero, where it stays.

At w_j=0 the absolute value has a corner and no derivative. Convex analysis replaces the derivative by the subdifferential, the set of slopes of all lines that touch the graph at that point from below:

\partial|w_j|=\{\operatorname{sign}(w_j)\}\quad(w_j\neq0),\qquad \partial|w_j|=[-1,\,1]\quad(w_j=0).

A point minimizes a convex function exactly when 0 belongs to its subdifferential. The interval at zero is the whole secret of sparsity: at w_j=0 the penalty can supply any counter-force between -\lambda and \lambda, so it can absorb a weak pull of the loss completely.

The two penalties for one weight. (a) The penalties w^2 and |w|. (b) Their gradients, the force pulling the weight toward zero: proportional to w for L2 (a spring), constant for L1 (a flat tax); at w=0 the L1 force can take any value in [-1,1]. (c) The solutions for orthonormal features as functions of the least-squares weight \hat w_j, for \lambda=1.4: ridge divides by 1+\lambda, the lasso subtracts \lambda/2 and sets every |\hat w_j|\le\lambda/2 to zero (shaded). Data: formulas (9).
Figure 2. The two penalties for one weight. (a) The penalties w^2 and |w|. (b) Their gradients, the force pulling the weight toward zero: proportional to w for L2 (a spring), constant for L1 (a flat tax); at w=0 the L1 force can take any value in [-1,1]. (c) The solutions for orthonormal features as functions of the least-squares weight \hat w_j, for \lambda=1.4: ridge divides by 1+\lambda, the lasso subtracts \lambda/2 and sets every |\hat w_j|\le\lambda/2 to zero (shaded). Data: formulas (9).

4.2Orthonormal features: soft thresholding

The lasso has no closed form in general, but when the features are orthonormal (X^\top X=I) everything can be solved by hand, and the solution shows exactly what the penalty does. Let \hat w=X^\top y be the least-squares solution. Then

\|y-Xw\|_2^2=y^\top y-2w^\top X^\top y+w^\top X^\top Xw=y^\top y+\sum_{j}\big(w_j^2-2\hat w_jw_j\big),

and the objective splits into p separate one-dimensional problems, one per weight: minimize f(w_j)=w_j^2-2\hat w_jw_j+\lambda|w_j|.

Case w_j>0. Then f'(w_j)=2w_j-2\hat w_j+\lambda=0 gives w_j=\hat w_j-\lambda/2, which is positive only if \hat w_j>\lambda/2.

Case w_j<0. Similarly w_j=\hat w_j+\lambda/2, valid only if \hat w_j<-\lambda/2.

Case w_j=0. Zero is optimal when 0\in\partial f(0)=-2\hat w_j+\lambda[-1,1], that is, when |\hat w_j|\le\lambda/2.

The three cases combine into the soft-thresholding operator S_t(z)=\operatorname{sign}(z)\max(|z|-t,\,0) (Donoho and Johnstone, 1994). Doing the same for ridge, f(w_j)=w_j^2-2\hat w_jw_j+\lambda w_j^2, and for best-subset selection with an \ell_0 penalty \lambda\cdot\mathbf 1(w_j\neq0), gives the three classic orthonormal estimators (Hastie et al., 2009, Table 3.4):

\hat w_j^{\mathrm{ridge}}=\frac{\hat w_j}{1+\lambda},\qquad \hat w_j^{\mathrm{lasso}}=S_{\lambda/2}(\hat w_j)=\operatorname{sign}(\hat w_j)\Big(|\hat w_j|-\frac\lambda2\Big)_{+},\qquad \hat w_j^{\mathrm{subset}}=\hat w_j\cdot\mathbf 1\big(|\hat w_j|>\sqrt\lambda\big). \tag{9}

Ridge scales every weight by the same factor. The lasso subtracts the same amount from every weight and stops at zero: everything inside the dead zone |\hat w_j|\le\lambda/2 becomes exactly zero (Figure 2c). Best subset is a hard threshold: it keeps large weights untouched and kills small ones, but jumps discontinuously, which makes it unstable. The lasso is the continuous compromise: it selects like best subset and shrinks like ridge.

w_1w_2w_3w_4w_5w_6w_7w_8zeros
Least squares \hat w_j3.2-2.41.61.1-0.80.5-0.350.20
Ridge \hat w_j/21.6-1.20.80.55-0.40.25-0.1750.10
Lasso S_{0.5}(\hat w_j)2.7-1.91.10.6-0.30003
Best subset (threshold 1)3.2-2.41.61.100004
Table 1. The three orthonormal estimators (9) with \lambda=1, applied to the eight weights of the video’s opening scene. Data: computed for this tutorial.
Try itSpring versus flat tax

The eight least-squares weights of Table 1 (orthonormal features). Ridge divides each weight by 1+\lambda; the lasso subtracts \lambda/2 and stops at zero, \operatorname{sign}(\hat w)(|\hat w|-\lambda/2)_+. Increase \lambda and count the zeros.

Worked example 3. Table 1 applies (9) with \lambda=1 to the eight least-squares weights shown at the start of the video. Ridge halves all of them, so the smallest, 0.2, becomes 0.1, still nonzero. The lasso subtracts \lambda/2=0.5 from each absolute value: 3.2\to2.7, 1.1\to0.6, -0.8\to-0.3, and the three weights with |\hat w_j|\le0.5, namely 0.5, -0.35 and 0.2, become exactly 0. Best subset with the same \lambda keeps the four weights larger than \sqrt\lambda=1 at full size and zeroes the other four.

4.3The general case: optimality conditions

Without orthonormality the weights interact, but the same subgradient argument gives the optimality (Karush–Kuhn–Tucker) conditions of the lasso. The gradient of the loss with respect to w_j is -2x_{\cdot j}^\top(y-Xw), so 0\in\partial J(w) means, for every j,

2\,x_{\cdot j}^\top(y-Xw)=\lambda\operatorname{sign}(w_j)\ \ \text{if }w_j\neq0,\qquad\qquad \big|2\,x_{\cdot j}^\top(y-Xw)\big|\le\lambda\ \ \text{if }w_j=0 . \tag{10}

The quantity x_{\cdot j}^\top(y-Xw) is the correlation of feature j with the current residual: how much the fit would improve by moving w_j. Condition (10) says it plainly: a feature stays out of the model unless its correlation with the residual is large enough to pay the tax, and every feature in the model has exactly the same absolute correlation \lambda/2 with the residual (Hastie et al., 2015, §2.4). Setting w=0 in (10) gives the smallest penalty for which the lasso selects nothing,

\lambda_{\max}=2\max_j\big|x_{\cdot j}^\top y\big| , \tag{11}

and, as \lambda decreases below \lambda_{\max}, the first feature to enter is the one most correlated with y.

4.4Computing the lasso: coordinate descent

Because the lasso has no closed form, it is solved iteratively. The most popular method is coordinate descent (Friedman et al., 2010): update one weight at a time, holding the others fixed, and cycle until nothing changes. With A=X^\top X and b=X^\top y, the objective as a function of w_j alone is A_{jj}w_j^2-2w_jz_j+\lambda|w_j|+\text{const}, where z_j=b_j-\sum_{k\neq j}A_{jk}w_k=x_{\cdot j}^\top r^{(j)} is the correlation of feature j with the partial residual r^{(j)}=y-\sum_{k\neq j}x_{\cdot k}w_k. This is the orthonormal problem again, scaled by A_{jj}, so

w_j\;\leftarrow\;\frac{S_{\lambda/2}(z_j)}{A_{jj}} . \tag{12}

Each update is one soft threshold. The algorithm is simple, fast, and produces exact zeros. Running it on a decreasing grid of \lambda values, starting each fit from the previous solution, computes the whole regularization path at little extra cost; this is what the glmnet package does (Friedman et al., 2010). The LARS algorithm of Efron et al. (2004) computes the exact path, which is piecewise linear in \lambda.

Worked example 4. Run (12) on the correlated pair of Worked example 2, A=\begin{pmatrix}1&0.99\\0.99&1\end{pmatrix}, b=(1,\,0.98), with \lambda=0.1, so the threshold is \lambda/2=0.05. Start at w=(0,0). Update w_1: z_1=1-0.99\cdot0=1 and w_1=S_{0.05}(1)=0.95. Update w_2: z_2=0.98-0.99\cdot0.95=0.0395, which is below the threshold, so w_2=0. Updating w_1 again changes nothing, and we have converged to \hat w_{\mathrm{lasso}}=(0.95,\;0). The check (10) for the excluded feature is |2\cdot0.0395|=0.079\le\lambda=0.1. Least squares gave (1.497,\,-0.503) and ridge (0.565,\,0.383); the lasso keeps one of the two near-duplicates and drops the other.

Why plain (sub)gradient descent does not give zeros. Training a model with an L1 term by ordinary stochastic gradient descent, adding \lambda\operatorname{sign}(w_j) to the gradient, almost never lands a weight exactly on zero: the fixed-size push makes it jump back and forth across zero, so small weights hover near zero instead. To get exact zeros one uses the soft threshold explicitly, as coordinate descent does, or the proximal gradient method (ISTA), which alternates a gradient step on the loss with a soft threshold, w\leftarrow S_{\eta\lambda}\big(w-\eta\nabla\|y-Xw\|^2\big) (Daubechies et al., 2004).

Pause and think. In Worked example 4, what would happen if we started coordinate descent with w_2 instead of w_1?

Show the answer

By symmetry of the update, w_2=S_{0.05}(0.98)=0.93 first, then z_1=1-0.99\cdot0.93=0.0793>0.05, so w_1=0.0293 also enters. Further sweeps move weight from w_2 to w_1 until, after 52 sweeps, w_2 drops out and the iterates reach the same optimum (0.95,0): the objective is strictly convex here, so its minimum is unique (checked numerically). The order changes the path of the iterations and their number, not the answer; strong correlation makes coordinate descent slow. (With exactly duplicated features the minimum is no longer unique, and different algorithms may return different splits.)