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

05The geometry: why the diamond has corners

5.1Penalty or budget: two forms of the same problem

The famous picture of regularization uses a budget instead of a price: minimize the training loss subject to \Omega(w)\le t. The two forms are equivalent.

Proposition 1. Let w_\lambda minimize L(w)+\lambda\,\Omega(w) for some \lambda>0, and let t=\Omega(w_\lambda). Then w_\lambda also minimizes L(w) subject to \Omega(w)\le t.

Suppose some w with \Omega(w)\le t had L(w)<L(w_\lambda). Then L(w)+\lambda\Omega(w)<L(w_\lambda)+\lambda t=L(w_\lambda)+\lambda\Omega(w_\lambda), contradicting the optimality of w_\lambda.

Conversely, for convex L and \Omega every budget t (below the penalty of the unconstrained optimum) corresponds to some \lambda, the Lagrange multiplier of the constraint. So a large penalty \lambda means a small budget t, and both forms trace the same family of solutions. The budget form, used by Tibshirani (1996) to define the lasso, makes the geometry visible; the penalty form is what software implements (Parr, 2020).

5.2The picture

For two weights, the budget \|w\|_2\le t is a disc and \|w\|_1=|w_1|+|w_2|\le t is a square rotated by 45^\circ, a diamond with its four corners on the axes. The training loss of a linear model is a quadratic function of w, so its level sets are ellipses centred at the unregularized solution \hat w. The constrained solution is the point of the budget region with the lowest loss: grow the ellipses outward from \hat w until they first touch the region. Figure 3 does this exactly for the video’s example, a quadratic loss (w-\hat w)^\top A(w-\hat w) with \hat w=(2.2,\,1.2), A=\begin{pmatrix}2.1736&0.9848\\0.9848&1.8264\end{pmatrix} and budget t=1.

Constrained least squares with budget t=1. Grey ellipses are contours of the loss around the unregularized solution \hat w; the dark ellipse is the lowest contour that touches the budget region. (a) The L2 disc is touched at (0.82,\,0.57): both weights nonzero. (b) The L1 diamond is touched at its corner (1,\,0): w_2 is exactly zero. Data: contact points computed exactly for this tutorial; after Hastie et al. (2009, Fig. 3.11) and the companion video.
Figure 3. Constrained least squares with budget t=1. Grey ellipses are contours of the loss around the unregularized solution \hat w; the dark ellipse is the lowest contour that touches the budget region. (a) The L2 disc is touched at (0.82,\,0.57): both weights nonzero. (b) The L1 diamond is touched at its corner (1,\,0): w_2 is exactly zero. Data: contact points computed exactly for this tutorial; after Hastie et al. (2009, Fig. 3.11) and the companion video.
Try itWhere does the ellipse touch?

The loss (w-\hat w)^\top A(w-\hat w) of Figure 3. Move the unregularized solution \hat w or change the budget t; the constrained solutions are recomputed exactly. Watch how often the diamond is touched at a corner.

The disc is smooth: at every point of its boundary there is exactly one tangent direction, so the expanding ellipse touches it at a generic point where both weights are nonzero, here (0.82,\,0.57). The diamond has corners, and the corners sit on the axes. A corner is “sharp”: the ellipse touches it for a whole range of ellipse orientations, not just one, so the first contact is often a corner, here (1,0), where w_2=0.

5.3When is a corner the answer?

At the solution on the boundary, the loss cannot decrease along any direction that stays inside the region, so the negative gradient -\nabla L must point “outward”, into the set of normal directions of the region at that point. On the smooth disc, the normal direction at w is just w itself, so -\nabla L(w)=\mu w for some \mu>0: a single direction. At the diamond’s corner (t,0) the normal directions form a whole wedge, \{\mu(1,s):\ \mu\ge0,\ |s|\le1\}, a quarter of all directions. The corner is optimal whenever -\nabla L falls in that wedge, that is, when |\partial L/\partial w_2|\le|\partial L/\partial w_1| there.

Worked example 5. In Figure 3, the gradient of the loss at the corner (1,0) is 2A\big((1,0)-\hat w\big)=2A(-1.2,\,-1.2)=(-7.58,\,-6.75). The ratio of its components is 6.75/7.58=0.89\le1, so -\nabla L lies inside the wedge and the corner is the solution. Moving along the edge toward (0,1) trades a unit of w_1 for a unit of w_2; it changes the loss at the rate 7.58-6.75=0.83>0, so it makes things worse. The loss at the corner is 8.60, at the L2 contact 6.56, and at w=0 it is 18.35.

This is the same condition as (10): a weight stays at zero while the loss’s pull on it is weaker than the pull on the active weights. The geometry, the forces and the optimality conditions are three views of one fact. Parr (2020) checks this with 5{,}000 random two-dimensional quadratic losses: L1 gave a zero coefficient in 72\% of them and L2 in 5\%. L1 encourages zeros, since a whole zone of unregularized optima maps to a zero coefficient; L2 gives one only when \hat w lies on or very near an axis.

In many dimensions. In p dimensions the L1 ball \{\|w\|_1\le t\} has 2p corners, all on the coordinate axes, and its edges and faces lie in coordinate subspaces where many weights are zero. These low-dimensional pieces stick out, so an expanding ellipse usually makes its first contact on one of them, and the lasso solution typically has many zeros. The L2 ball is a smooth sphere with no preferred directions; it is rotation invariant, which is why ridge does not care which basis the features are written in and the lasso does (Ng, 2004).

Other penalties. The family \sum_j|w_j|^q interpolates: q=2 is ridge, q=1 the lasso, and q\to0 counts nonzeros (best subset). For q<1 the ball is pointier still and the problem is not convex; q=1 is the smallest q that keeps the problem convex, and the only one that is both convex and sparse (Hastie et al., 2009, §3.4.3).

Pause and think. If the unregularized solution \hat w lies inside the budget region, what do ridge and the lasso return?

Show the answer

\hat w itself, for both: the constraint is not active, which in the penalty form corresponds to \lambda=0. Regularization only changes the answer when the budget is smaller than the size of the least-squares solution.