Weight decay: L2 against L1

When parameters get large, a network can learn a bumpy function. The classic fix from regression is to charge for the size of the weights. Five views of what that charge does: to each step, to the answer, to the fitted function, inside Adam, and to which classifier you get.

1. Every step shrinks the weights

Homework 2.1 adds a penalty to the loss, . Its gradient is , so a gradient descent step becomes a shrink followed by the usual step. Play both runs from the same start, or drag the start point.

Gradient descent with and without weight decay on a tilted quadratic bowl Left: contours of the loss, its minimum, the origin, and two gradient-descent paths from the same start, one without weight decay heading to the loss minimum and one with weight decay heading to a point pulled toward the origin, with the curve of all such points as the decay grows. At the current step, arrows show the loss's pull and the decay's pull. Right: the loss and the length of the weight vector over the steps for both runs.
decay λ learning rate ν

2. A ball against a diamond: why L1 zeroes weights

The same penalty can be read as a budget: find the best weights whose size is at most r. Measured by L2 the budget is a ball; measured by L1, |θ1| + |θ2|, it is a diamond. Drag the loss’s minimum (the cross) in either panel.

Constrained minima under an L2 ball and an L1 diamond Two panels with the same tilted loss. Left, an L2 ball: the best point inside it is where a loss contour just touches the circle, and both weights are usually non-zero. Right, an L1 diamond: the contour usually touches at a corner, where one weight is exactly zero; the shaded region shows every placement of the loss minimum for which that happens.
budget r

3. Large weights, bumpy functions

Twelve noisy points and a polynomial with twelve coefficients: exactly enough to pass through every point. The penalty λ·‖w‖² on the coefficients trades fitting the points against keeping them small. Slide λ from nothing to a lot.

A polynomial fit smoothed by a penalty on its coefficients Left: noisy training points, the true curve, and the fitted curve, which is wildly bumpy when the penalty is tiny and smooth when it is larger. Right: training and test error against the penalty on a log scale, with the training error rising and the test error falling and then rising again.
penalty λ

4. Weight decay inside Adam: L2 is not decay (AdamW)

For plain gradient descent, adding λ/2·‖θ‖² to the loss and shrinking the weights every step are the same thing (part 1). Inside Adam they are not, because Adam divides each weight’s step by the size of that weight’s own recent gradients, and whatever you put into the gradient gets divided too, including the penalty.

Adam with an L2 penalty (torch.optim.Adam, weight_decay=λ)
AdamW: decoupled weight decay (torch.optim.AdamW)

The only difference is where λθ goes. In Adam with L2 it is added to the gradient, so it passes through m and v and gets divided by √v̂, which is roughly the size of that weight’s gradients. In AdamW it is added after the division, so every weight loses the same fraction per step:

A weight with large gradients (big √v̂) barely feels the Adam-with-L2 decay. A weight with tiny gradients has its decay divided by a tiny number, and when its gradients are smaller than λθ the decay dominates v itself, so the weight moves toward 0 at Adam’s full step size η per step. Below, eight weights all start at 1 with gradients of different sizes that flip sign every step and average to zero, so the decay is the only force that moves them.

Eight weights under Adam with L2 and under AdamW Two rows of eight bars, one per weight, ordered by gradient size from 0.001 to 10, animated over training steps: with L2 inside Adam the small-gradient weights collapse to zero while the large-gradient ones barely shrink; with AdamW all eight shrink together. At right, the shrink per step against gradient size on log scales: flat for AdamW, falling as one over the gradient size for Adam with L2.
step 0 decay λ 0.10 step size η 0.010
Why it matters. Weight decay is meant to be a prior: pull every weight toward 0 by the same fraction, so the ones the data doesn’t need fade away. Inside Adam, the L2 version does nearly the opposite. The weights with big, frequent gradients, often the ones doing the most work, are hardly regularized at all; weights with small or rare gradients, such as the embedding of a rare word, are wiped out at full speed whether the data needs them or not. Loshchilov and Hutter (“Decoupled Weight Decay Regularization,” ICLR 2019) proposed taking the decay out of the gradient, and found that it makes Adam generalize better and makes good values of λ much less tangled up with the learning rate. AdamW is now the default optimizer for training transformers. In SGD the two versions differ only by a rescaling of λ (part 1), which is why the difference went unnoticed until adaptive optimizers. (In the paper the whole update is also multiplied by a schedule factor ηt; PyTorch’s AdamW does the same thing by first shrinking θ ← (1 − ηλ)θ and then taking the Adam step, with a default λ of 0.01.)

5. Strong decay turns max margin into mean difference

Train a linear classifier, logistic regression with weight decay λ, on two separable classes. With no decay its direction heads for the max-margin separator, the one that stays as far as possible from the closest points. With strong decay it heads somewhere quite different: the mean difference μ+ − μ−, the direction of the old nearest-mean (“Euclidean” or minimum-distance) classifier, which ignores the individual points and looks only at the two class averages. The data here is built so those two directions disagree. Switch to a few stragglers for data where a third direction, Fisher’s (explained below), disagrees with both.

Logistic regression with weight decay, between max margin and mean difference Left: two classes of points, their means, the max-margin boundary and the nearest-mean boundary as dashed lines, and the logistic-regression boundary at the chosen decay as a solid line. Right: the angle of the weight vector against the decay on a log scale, moving from the max-margin angle at small decay to the mean-difference angle at large decay.
weight decay λ 0.001

Why the mean difference. Write the logistic loss in terms of each point’s margin mi = yi(w·xi + b), with yi = ±1. Strong decay keeps w tiny, so every margin is near 0, where the loss is nearly a straight line:

Keeping only the straight-line part, the objective is linear in w plus the decay, and its minimum is a scaled copy of the average of yixi, which for balanced classes is the mean difference:

Every point pulls on w equally, however far it is from the boundary, so only the averages survive. Why max margin. With little decay on separable data, w can grow without bound, and as it grows the loss e−m of all but the closest points becomes negligible; only the points nearest the boundary still pull, and w/‖w‖ approaches the max-margin direction (Rosset, Zhu & Hastie, 2004), the same direction plain gradient descent drifts toward with no decay at all (Soudry et al., 2018).

In between: measuring distance with the data’s own ruler. The nearest-mean rule measures distance with an ordinary ruler, the same in every direction. But these two clouds are long and thin. Along their length, points routinely sit far from their class mean; across the gap, even a short distance is unusual. A fairer ruler measures distance in units of the data’s spread in each direction, so a step along a long direction counts for less than the same step along a narrow one. That is the Mahalanobis distance,

where Σ is the covariance of each class around its own mean. With this ruler the classify-by-nearest-mean rule is still linear, and its weight vector is Σ−1(μ+ − μ−): the mean difference with the long directions discounted. That is Fisher’s linear discriminant, also called LDA. On the stretched clouds it lands within a few degrees of the max-margin direction, because once the long direction of the clouds is discounted, what is left is mostly the direction across the gap. But the two answer different questions. Fisher summarizes each whole cloud by its mean and spread; max margin looks only at the handful of points nearest the gap. Switch to a few stragglers: two wide-apart clouds set the means and spreads, while two odd points per class (circled) sitting in the gap are the only points near the boundary. Now the three directions separate, and the decay path visits all three: the mean difference at strong decay, turning toward Fisher at moderate decay, and on to max margin at weak decay.

Moderate weight decay sits between the two rulers. Keep one more term of the expansion, the m²/8, and it adds a penalty that depends on how the data is spread. The minimum then works out to

(the class separation inside the covariance only adds a multiple of μ+ − μ−, which does not change the direction). When λ is large, the 4λI term swamps Σ, and w is the plain mean difference: the ordinary ruler. As λ shrinks, Σ takes over and w turns toward Fisher’s direction: the data’s ruler. This is exactly ridge-regularized Fisher discriminant, with the decay playing the role of the ridge. It holds only while the margins stay small; with very little decay w grows large, and then only the closest points matter and the direction becomes the max-margin one. So the decay dial moves the classifier from “trust only the averages, measured with an ordinary ruler,” through “trust the averages, measured with the data’s own ruler,” to “trust only the closest points.”

Why it works