Weight decay and L1/L2 regularization

When a model memorizes its training data, its weights often grow large. So we add "a penalty if the weights are big" to the loss. See where the lowest point goes when the penalty is a circle (L2) or a diamond (L1), then follow how that penalty turns into "shrink the weights by a fixed ratio" inside one training step.

Short answer. Regularization adds a penalty on the size of the weights to the loss. With the sum of squares it is L2: every weight gets smaller, but none becomes 0. With the sum of absolute values it is L1: some weights become exactly 0. Put an L2 penalty into gradient descent and every step becomes w ← (1 − ηλ)w − η∇L: the weights shrink by a fixed ratio, which is why it is called weight decay. In Adam the two come apart, so we use AdamW, which does the shrinking separately.

The penalty is added to the data loss ½(w₁ − 3)² + (w₂ − 1)². This is a small toy with only two weights.

The purple ellipses are contours of the loss from the data alone, and the thick one passes through the current lowest point. The green circle or diamond is every point with the same penalty as the lowest point (the penalty fence). The orange dot is the data-only lowest point w*, the black dot is the lowest point with the penalty, and the blue dashed line is the path the black dot takes as λ grows from 0 to 4. A real model has millions of weights and its loss is not such a neat bowl, but near the lowest point people think of it as a bowl.

Definitions

The text below uses a small example with two weights. These are the textbook definitions. Let L(w) be the data loss, w = (w₁, w₂, …) the weights, λ ≥ 0 the regularization coefficient (the strength of the penalty), and η the learning rate.

L2 regularization (ridge)
Regularization that adds a penalty proportional to the sum of squared weights to the loss, and looks for the weights that make the total smallest. This page puts a ½ in front so the derivative is clean.

L(w)+λ2∑iwi2

L1 regularization (lasso)
Regularization that adds a penalty proportional to the sum of absolute weights to the loss. It can make some weights exactly 0.

L(w)+λ∑i|wi|

soft thresholding
The function that pulls x toward 0 by t, and stops at 0 if it would cross. If the loss along one axis is ½h(w − w*)² with curvature h and you add the L1 penalty λ|w|, the minimum is at Sλ/h(w*).

St(x)=sign(x)max(|x|−t,0)

weight decay
An update that shrinks the weights by a factor of 1 − ηλ at every step, then takes one step along the gradient of the loss. For plain gradient descent (SGD) it is the same as taking one step on the loss with the L2 penalty added.

w←(1−ηλ)w−η∇L(w)

decoupled weight decay (AdamW)
Weight decay where the shrinking is taken out of the gradient and done on its own. Adding it to Adam gives AdamW. Unlike Adam + L2, which adds the penalty gradient λw to the gradient, every weight shrinks by the same factor at every step. m̂ and v̂ are the first and second moment estimates of the gradient, and the formula has the form used in section 5.

w←(1−ηλ)w−ηm^v^+ϵ

1. What adding a penalty means

Training looks for the weights w with the smallest loss L(w) (how wrong the model is). Regularization adds a penalty that grows with the weights, so training looks for the smallest loss plus penalty instead.

L2: total loss = L(w) + λ × ½(w₁² + w₂² + …)
L1: total loss = L(w) + λ × (|w₁| + |w₂| + …)

λ (lambda) is the strength of the penalty. At 0 there is no penalty, and the bigger it is, the harder the weights are pressed toward 0. The ½ in L2 is there so the 2 cancels when you take the slope. It only changes the scale of λ (the formula on the overfitting page has no ½). That page already shows why big weights make memorizing easy and what happens to test error as λ goes up on a polynomial curve. This page looks at the shape of the penalty, one training step, and how it plays with Adam.

2. Circle and diamond: why only L1 makes zeros

Join all points with the same penalty and you get a circle centered at the origin for L2, and a diamond with its corners on the axes for L1. Making "loss + λ × penalty" smallest gives the same answer as making the loss smallest while keeping the penalty inside such a fence. So the lowest point is where a contour of the data loss first touches the fence from outside. The bigger λ is, the smaller the fence.

Try this. ① At the start (L2, λ = 1) the lowest point is (1.5, 0.67). Drop λ to 0 and it is the data-only (3, 1). Raise it to the end (4) and it is (0.6, 0.33). No weight becomes 0 at any λ. ② Switch to L1: at λ = 1 it is (2, 0.5), and at λ = 2 w₂ becomes exactly 0, giving (1, 0). The black dot sits on a corner of the diamond, and from there the blue path runs along the horizontal axis. From λ = 3 it is (0, 0). ③ Back on L2, set λ = 2 to get (1, 0.5). w₁ goes from 3 to 1, a third, while w₂ goes from 1 to 0.5, a half. L2 shrinks more along the axis where the loss curves less, the weight the data cares less about.

3. Building the rule: shrink by a fixed ratio every step

Now put the L2 penalty into gradient descent and look at one step. We watch two weights. A is a weight the data wants at 3 (data loss ½(w − 3)², slope w − 3). B is a weight the data does not use (slope 0). Both start at 4, with step size η = 0.1 and penalty strength λ = 0.5.

  1. Plain step: w ← w − η∇L. Walk η downhill on the loss. A heads to 3, and B, with slope 0, stays at 4. Nothing ever shrinks a weight the data does not use.
  2. Add the penalty's slope: w ← w − η(∇L + λw). Adding ½λw² to the loss adds its slope λw. Why: the step follows the slope of the total loss, so the penalty's slope comes along.
  3. Group the w terms: w − ηλw − η∇L = (1 − ηλ)w − η∇L. Open the brackets and w shows up twice, so they merge. Why: written this way, a step reads "first multiply w by (1 − ηλ), then take the usual step".
  4. Done: w ← (1 − ηλ)w − η∇L. With η = 0.1 and λ = 0.5, every step multiplies by 0.95, a 5% cut. This is weight decay. A stops at 3 ÷ (1 + 0.5) = 2 instead of 3, the same as the L2 formula h·w* ÷ (h + λ) from section 2 with h = 1. Walking lands on the lowest point of the loss with the L2 penalty.

Try this. ① In the first step, A is at 3.01 after 40 steps and B stays at 4. ② Press "Next": A stops at 2 and B is at 0.51 after 40 steps. ③ Press again and the first four values of B, 4 → 3.8 → 3.61 → 3.43, are written on the chart. Each is 0.95 times the one before. ④ In the last step, the orange dashed line marks 13.51 steps, where B reaches half (2).

4. One step by hand

Weight A from section 3 takes its first step. w = 4, the data loss slope is ∇L = 4 − 3 = 1, η = 0.1, λ = 0.5.

  1. The penalty's slope. λw = 0.5 × 4 = 2.
  2. One step with the combined slope. 1 + 2 = 3, 4 − 0.1 × 3 = 3.7.
  3. Again in the grouped form. (1 − 0.1 × 0.5) × 4 = 0.95 × 4 = 3.8, 3.8 − 0.1 × 1 = 3.7. Same answer.
  4. Without the penalty. 4 − 0.1 × 1 = 3.9. The penalty took off an extra ηλw = 0.05 × 4 = 0.2 in one step.

5. Adam splits them apart: AdamW

Adam picks a different step size for each weight. Roughly, w ← w − η · m̂ ÷ (√v̂ + ε), where m̂ is that weight's recent average slope and √v̂ is the recent size of its slope. A weight with large slopes is divided by a large number and takes small steps. A weight with small slopes takes bigger steps.

The AdamW paper (sources) shows that L2 regularization and weight decay are the same for plain SGD (once λ is rescaled by the learning rate) but not for Adam. It reports that the decoupled version improves Adam's generalization a lot, enough to compete with SGD with momentum on image classification. The paper also points out that many libraries call Adam + L2 "weight decay", which is easy to misread.

6. Key properties

PropertyIn plain wordsIn the lab
The bigger λ, the closer to 0The lowest point is where the data loss and the penalty balance. At λ = 0 it is the data-only pointThe blue dashed path
L2 never makes a 0The push λw gets weaker as w nears 0Still (0.6, 0.33) at λ = 4
L1 makes exact 0sIf λ beats the data's pull at 0, the weight stays at 0. It filters out useless inputsw₂ = 0 at λ = 2
L2 shrinks more where the data cares lessThe less the loss curves in a direction, the smaller h ÷ (h + λ)λ = 2: w₁ to a third, w₂ to a half
With plain SGD the L2 penalty equals weight decay, with Adam it does notOne step becomes (1 − ηλ)w − η∇L either way. Adam divides the penalty by the step size tooSections 3 and 5

7. Picking the strength

8. When to use what

OptionGoodBadUse it when
L2 penalty (ridge)Shrinks every weight together. The answer is stable, and for linear models one formula solves itUseless inputs never reach 0Linear or logistic regression with many or similar inputs
L1 penalty (lasso)Sets some weights to exactly 0. You see which inputs matter and the model gets smallerIt has a sharp point at 0 with no slope, so it needs a solver that cuts at 0. Among several similar inputs it tends to keep just oneMany inputs, of which you expect only some to matter
SGD + weight decayDoes the job of an L2 penalty with one multiplyThe strength is η × λ, so it changes with the learning rateNeural networks trained with SGD
Adam + L2 (Adam's weight_decay)Literally "add a penalty to the loss"Weights with large slopes feel less penalty, so the shrink ratio differs per weightWhen you must match an old codebase's results
AdamWShrinks every weight by the same ratio. In the paper, Adam's generalization improvedλ has to be tuned on its own. It does not make 0sTraining neural networks with Adam-style optimizers
No penaltyFits the data best. One less setting to pickEasy to memorize when data is scarceLots of data, or when using other tools like early stopping (overfitting)

Where it is used

Frequently asked

Are weight decay and L2 regularization the same thing?

With plain SGD, yes. Add ½λw² to the loss, take one step, and w gets multiplied by (1 − ηλ). With methods that pick a different step size for each weight, like Adam, they differ, because the penalty's slope also gets divided by that step size. That is why AdamW, which does the shrinking separately, was proposed.

Why does L1 set weights to 0 when L2 cannot?

The L2 push λw gets weaker as w nears 0, so even a small pull from the data keeps the weight off 0. The L1 push stays at λ all the way to 0, so if the data pulls on that weight with less than λ at 0, the weight stays at 0. In the picture, the diamond's corners stick out on the axes, so the contour touches a corner first.

What value should λ (weight_decay) be?

No value fits every model. Try a few values 10 times apart, like 0.0001, 0.001, 0.01 and 0.1, and keep the one with the lowest validation error. Weight decay shrinks by learning rate × λ per step, so when you change the learning rate, check λ again. The default in PyTorch AdamW is 0.01.

Words

Plain words used hereTextbook term
A penalty on the weightsRegularization
Squared penaltyL2 regularization, ridge, Tikhonov regularization
Absolute value penaltyL1 regularization, lasso
Penalty strength λRegularization coefficient. In code: weight_decay, alpha
Shrink by a fixed ratio every stepWeight decay
Shrinking taken out of the slopeDecoupled weight decay, AdamW
Pull toward 0, stop at 0Soft thresholding
Many weights at 0Sparsity
The penalty fence (circle, diamond)Constraint region, norm ball
Step size ηLearning rate
How sharply the loss curves, hCurvature, eigenvalues of the Hessian
Recent average and size of the slopeFirst and second moment estimates (m̂, v̂)

Sources

← DropoutEarly stopping and reading learning curves →