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.
- 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.
- 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*).
- 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.
- 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.
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₂| + …)
L(w): the loss (how wrong the model is).w(w₁,w₂, …): the weights.λ: the strength of the penalty.
λ (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.
- L2 (circle). A circle is smooth, so a contour usually touches it away from the axes. Every weight gets smaller but none becomes 0. For a loss that splits by axis, like this toy,
w = h·w* ÷ (h + λ). Here h is how sharply the loss curves along that axis (1 for w₁, 2 for w₂), so no λ makes it 0. - L1 (diamond). The diamond's corners poke out on the axes, so a contour often touches a corner first. At a corner one weight is exactly 0. As a formula:
pull w* toward 0 by λ ÷ h, and stop at 0 if it would cross(soft thresholding). At 0 the data pulls on w₂ withh × w* = 2 × 1 = 2, so for λ of 2 or more, w₂ stays at 0.
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.
- 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. - 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. - 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". - 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 at3 ÷ (1 + 0.5) = 2instead of 3, the same as the L2 formulah·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.
- The penalty's slope.
λw = 0.5 × 4 = 2. - One step with the combined slope.
1 + 2 = 3,4 − 0.1 × 3 = 3.7. - 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. - Without the penalty.
4 − 0.1 × 1 = 3.9. The penalty took off an extraηλw = 0.05 × 4 = 0.2in 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.
- Adam + L2 penalty. The penalty's slope is added to the slope:
g = ∇L + λw. Thenλwis divided by √v̂ too. The larger a weight's data slope, the less the penalty works on it, and the shrink ratio differs from weight to weight. "Multiply by (1 − ηλ) every step" from section 3 no longer holds. PyTorch'sAdam(weight_decay=…)works this way, and its docs call it an "L2 penalty". - AdamW. The shrinking is taken out of the slope and done on its own:
w ← (1 − ηλ)w − η · m̂ ÷ (√v̂ + ε). The rule from section 3 survives, so every weight shrinks by the same ratio each step. This is PyTorch'sAdamW, with a default weight_decay of 0.01.
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
| Property | In plain words | In the lab |
|---|---|---|
| The bigger λ, the closer to 0 | The lowest point is where the data loss and the penalty balance. At λ = 0 it is the data-only point | The blue dashed path |
| L2 never makes a 0 | The push λw gets weaker as w nears 0 | Still (0.6, 0.33) at λ = 4 |
| L1 makes exact 0s | If λ beats the data's pull at 0, the weight stays at 0. It filters out useless inputs | w₂ = 0 at λ = 2 |
| L2 shrinks more where the data cares less | The 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 not | One step becomes (1 − ηλ)w − η∇L either way. Adam divides the penalty by the step size too | Sections 3 and 5 |
7. Picking the strength
- Try a few values 10 times apart and pick by validation error. Look on a log scale, like 0.0001, 0.001, 0.01, 0.1. Too big underfits, too small overfits. Validation data and cross-validation are on the data splits page.
- The speed of weight decay is η × λ. Each step shrinks by the ratio ηλ, and the number of steps to reach half is
log 0.5 ÷ log(1 − ηλ)(in the lab: 13.51 steps at ηλ = 0.05). So changing the learning rate changes the decay strength too. When you change the learning rate, check λ again. - Put the inputs on the same scale first. The penalty looks at the size of the weights, and that size depends on the units of the inputs. Scale them with normalization and standardization so the penalty is fair.
- Biases usually get no penalty. The textbook Deep Learning (sources) says it is usual to penalize only the weights each layer multiplies by and leave the biases alone. A bias only shifts the whole curve and has nothing to do with wiggliness.
8. When to use what
| Option | Good | Bad | Use it when |
|---|---|---|---|
| L2 penalty (ridge) | Shrinks every weight together. The answer is stable, and for linear models one formula solves it | Useless inputs never reach 0 | Linear 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 smaller | It 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 one | Many inputs, of which you expect only some to matter |
| SGD + weight decay | Does the job of an L2 penalty with one multiply | The strength is η × λ, so it changes with the learning rate | Neural 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 weight | When you must match an old codebase's results |
| AdamW | Shrinks every weight by the same ratio. In the paper, Adam's generalization improved | λ has to be tuned on its own. It does not make 0s | Training neural networks with Adam-style optimizers |
| No penalty | Fits the data best. One less setting to pick | Easy to memorize when data is scarce | Lots of data, or when using other tools like early stopping (overfitting) |
Where it is used
- Linear models. Ridge (L2) and lasso (L1) are the standard regularizers for linear and logistic regression.
- Picking inputs. Fit with L1, then drop the inputs whose weights became 0.
- Training neural networks. The weight_decay of SGD and of AdamW is weight decay. You see it often in code that trains neural networks and transformers.
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 here | Textbook term |
|---|---|
| A penalty on the weights | Regularization |
| Squared penalty | L2 regularization, ridge, Tikhonov regularization |
| Absolute value penalty | L1 regularization, lasso |
| Penalty strength λ | Regularization coefficient. In code: weight_decay, alpha |
| Shrink by a fixed ratio every step | Weight decay |
| Shrinking taken out of the slope | Decoupled weight decay, AdamW |
| Pull toward 0, stop at 0 | Soft thresholding |
| Many weights at 0 | Sparsity |
| The penalty fence (circle, diamond) | Constraint region, norm ball |
| Step size η | Learning rate |
| How sharply the loss curves, h | Curvature, eigenvalues of the Hessian |
| Recent average and size of the slope | First and second moment estimates (m̂, v̂) |
Sources
- Ilya Loshchilov, Frank Hutter, Decoupled Weight Decay Regularization, arXiv:1711.05101. L2 and weight decay are the same for SGD but not for Adam, and decoupled weight decay (AdamW).
- Ian Goodfellow, Yoshua Bengio, Aaron Courville, Deep Learning, chapter 7 (regularization). The L2 penalty and the multiply form of one step, how much each curved direction shrinks, the sparsity of L1, and leaving biases unpenalized.
- PyTorch docs: AdamW (default weight_decay 0.01), Adam (weight_decay is an L2 penalty).