Regularization is a common technique in machine learning to prevent overfitting. It does so by penalizing overly complex models, and encouraging models to find simpler more generalizable patterns. Regularization often involves adding a regularization term to the loss function like so:
where is the model parameters, is the loss function, , and is a regularization term that captures model complexity. helps balance the goal of minimizing the loss with the goal of producing a simple model. Smaller prioritizes minimizing the original loss while large prioritizes simple models (which can cause underfitting if is too high).
Two common forms of regularization are L1/L2 regularization which we define below:
| Name | Regularization term | Penalizes | Effect on weights |
|---|---|---|---|
| L1 | The sum of absolute weights | Drives some weights exactly to zero, producing sparse solutions | |
| L2 | The sum of squared weights | Shrinks all weights toward zero without forcing them to vanish |
Translating Penalty Form to Constraint Form
To picture the geometry of L1/L2 regularization, we change the way we represent regularization. Earlier, we expressed regularization in terms of a loss function, known as penalty form:
Now, we express regularization as a Lagrangian, known as the constraint form:
where is inversely related to .
Visualizing the Geometry
With the problem in constraint form, we can visualize the regularization. The shaded region is the set of weights allowed by the constraint , and the concentric curves are level sets (contours) of the loss centered on the unconstrained optimum . The constrained solution is the first point a growing loss contour touches as it expands out toward the constraint.
For a point on the constraint boundary to be the optimum, the loss gradient and the constraint gradient at that point have to point in the same direction (up to a sign). In L2, and along the flat edges of L1, the boundary is smooth, so it has a single gradient direction. The loss gradient has to match that one direction exactly, which rarely happens.
A corner of the L1 diamond is different. The boundary is not smooth there, so no single gradient exists. Instead the corner has a whole set of subgradients consisting of every direction lying between the gradients of the two edges that meet at it. The corner is a candidate optimum as long as the loss gradient points in the same direction (up to a sign) as any direction in that set. Because the set spans a range of directions rather than a single one, a corner is far more likely to be the optimum.
This reasoning is why L1 prefers sparse weights while L2 prefers small weights.
Appendix
Refresher of the Lagrangian
The Lagrangian function is a mathematical tool used to optimize an objective function given a set of constraints. Let's denote the function we want to maximize as , and our constraint function as where is a constant.
To find the maximum values of with respect to the constraint, we first search for points on the constraint surface where the value of remains unchanged as we take small steps along the surface from that point. Intuitively, searching for such points makes sense, as leading up to a maximum, the slope of the curve is positive. At the maximum, the slope is zero, which means remains unchanged for small steps around that point. Past the maximum, the slope of the curve becomes negative.
Now, to find points on the constraint surface where remains unchanged for small steps, we need the directional derivative of along the constraint surface to be zero at those points:
where is any tangent vector of the constraint surface at point . This equation literally looks for points such that at has no component along any direction of motion on the constraint surface. In other words, at , you cannot immediately increase by moving in any direction on the constraint surface.
Given that remains unchanged along the constraint surface (since our constraint is where is constant), is perpendicular to every tangent direction because the gradient points in the direction of greatest change. Since and are both perpendicular to every tangent direction, they both point in the same direction (up to a sign). This implies:
for some .
You can now use equations (3) and (4) to construct a system of equations that helps you find a set of candidates for the maximum of . However, not all candidate points are maxima. This is because other kinds of points such as minima, saddle points, etc. also have the property of having a zero directional derivative. Thus, to discover the maximum, you must compute the value of for every candidate point, and the that produces the maximum value of is the optimal input.
Lagrangian Function
So, finding a set of candidates for the maximum of given our constraint amounts to finding points that satisfy equations (3) and (4). The Lagrangian function provides a convenient way to package (3) and (4) together:
Look at what happens when we set the partial derivatives of the Lagrangian equal to zero though.
These are exactly the equations (3) and (4) we had above. So effectively, finding candidates for the maximum of is the same as solving for and . Plug the candidate points back into to find the that produces the maximum .
Constraints with Inequalities
Let's say we allow our constraint to take on the form:
At the solution, the constraint is either inactive or active:
- Inactive (). The solution sits strictly inside the feasible region, so the constraint has breathing room and isn't doing any real work. The solution is just an unconstrained maximum that happens to be feasible, found by solving and keeping the points that satisfy the constraint.
- Active (). The solution sits on the boundary, so the inequality behaves like the equality constraint and we solve it with the Lagrangian from before.
The constrained optimum has to fall into one of these two cases, so we just gather the candidates from both: the feasible solutions of and the solutions of the equality-constrained Lagrangian. Plug each candidate into and keep the one that produces the maximum value.