Search

Search papers and lessons

Fixing Weight Decay Regularization in Adam

Ilya Loshchilov, Frank Hutter · 2017

Loshchilov and Hutter (2017) show that an L2 penalty inside Adam is weaker than intended and propose AdamW, which decays the weights directly, closing much of Adam's gap to SGD.

On this page

  • Colour = topic mix
  • Discrete math
  • Optimizers
  • Calculus
  • Linear algebra
  • Probability
  • Losses & regularization
  • ReadReady to readNot yet

TL;DR

Adam is a training method that gives every weight its own step size. Its usual weight decay, an L2 penalty added to the gradient, is weaker than intended, because Adam's adaptive scaling quietly shrinks the penalty for weights that have had large gradients. In 2017 Ilya Loshchilov and Frank Hutter proposed AdamW, which subtracts the decay directly from the weights. On image benchmarks this made Adam generalise much better and brought it close to SGD with Momentum.

Builds on: Adam Optimizer, L2 Regularization, Stochastic Gradient Descent · Leads to: AdamW Optimizer, the method this paper introduces

Why it mattered

Language models. Later Transformer training recipes widely use AdamW, so a recipe that sets weight decay for a large model usually means the decoupled form.

Standard libraries. Once the decoupled form was published, deep learning libraries added AdamW as a built-in optimizer. Before that, they offered only L2 regularization under the name "weight decay", which is the naming confusion the paper addresses.

Choosing an optimizer. Before this paper, practitioners often had to choose between Adam, which trained quickly, and SGD with momentum, which generalised better on image tasks. The paper's stated goal was to make Adam competitive enough that people no longer need to switch.

The problem

Weight decay is a penalty that pulls weights towards zero so that the model does not fit noise. The usual way to implement it is an L2 penalty: add λ2∥θ∥2\frac{\lambda}{2}\lVert\theta\rVert^2 to the loss, whose gradient is λθ\lambda\theta.

For plain Gradient Descent the two ideas are the same. Shrinking each weight by a fixed fraction and adding the L2 gradient give identical steps, once the L2 strength is scaled by the learning rate. The paper calls this its Proposition 1.

The trouble starts with adaptive methods. Adam divides each weight's update by the square root of its recent squared gradients. If the penalty is added to the gradient, the penalty is divided by that same number. A weight with a long history of large gradients therefore receives only a small share of its penalty. The paper states the consequence directly: weights with large historic gradients are regularized less than they would be under decoupled weight decay.

The experiments show the cost. Adam trailed SGD with momentum, whose L2 penalty behaves as expected, and the authors argue that this gap is caused by the regularizer, not by adaptivity itself.

How it works

The change to Adam is one line, and it is easiest to see on one weight. Here is the pipeline in the order the update runs.

  1. Take the loss gradient gtg_t for the current batch. It contains no penalty.
  2. Update Adam's two running averages of gtg_t and of gt2g_t^2, and correct their start-up bias.
  3. Form the adaptive step: the averaged gradient divided by the root of the averaged squared gradient, times the learning rate.
  4. Compute the decay term λθt−1\lambda\theta_{t-1} from the current weights. Do not add it to the gradient.
  5. Subtract the adaptive step and the decay term from the weights together.
flowchart LR
    G["Loss gradient (no penalty)"] --> A["Adam averages, bias corrected"]
    A --> S["Adaptive step"]
    W["Current weights"] --> D["Decay term: lambda times weights"]
    S --> U["Subtract both from the weights"]
    D --> U
    U --> N["New weights"]

Now trace one weight through the pipeline. Take θ=2\theta = 2, decay λ=0.1\lambda = 0.1, learning rate α=0.5\alpha = 0.5, schedule multiplier η=1\eta = 1, and a loss gradient of 00 at the first step, so only the penalty acts.

  1. The loss gradient is 00.
  2. AdamW: the adaptive part is 00, because both averages are 00. The decay term is λθ=0.1×2=0.2\lambda\theta = 0.1 \times 2 = 0.2. The update subtracts it: θ1=2−1×(0.5×0+0.2)=1.8\theta_1 = 2 - 1 \times (0.5 \times 0 + 0.2) = 1.8. Check: the weight shrank by 0.20.2, which is 1010 percent.
  3. Adam with L2: the penalised gradient is g~1=0+0.1×2=0.2\tilde g_1 = 0 + 0.1 \times 2 = 0.2. The bias-corrected averages are m^1=0.2\hat m_1 = 0.2 and v^1=0.04\hat v_1 = 0.04, so v^1=0.2\sqrt{\hat v_1} = 0.2 and the adaptive ratio is 0.2/0.2=10.2 / 0.2 = 1. The update is θ1=2−0.5×1=1.5\theta_1 = 2 - 0.5 \times 1 = 1.5. Check: the weight shrank by 0.50.5, which is 2525 percent.
  4. Interpret. The same penalty gave different shrinkage. In the L2 version the penalty was divided by v^\sqrt{\hat v}, so its size depended on this weight's gradient history. With a longer history of large gradients the same penalty would have shrunk the weight far less. AdamW's decay never passes through that division.

The plot shows the same point over a range of gradient histories. The flat line is AdamW's decay, which does not depend on the history. The curve is the L2 penalty's relative strength, which falls as the historical gradient scale grows.

0.511.522.533.540246810historical gradient scalerelative strength of the penalty
  • L2 inside Adam
  • AdamW
Relative strength of the penalty for a weight whose historical gradient scale is x. Inside Adam the penalty is divided by x, so it falls off as 1 over x. The decoupled decay does not depend on x. The two agree only at x equal to 1.

The math

Definition: Decoupled weight decay

For a loss ftf_t with batch gradient gt=∇ft(θt−1)g_t = \nabla f_t(\theta_{t-1}), Adam with decoupled weight decay (AdamW) updates the weights θt\theta_t by the adaptive step and a separate decay term, with the decay strength λ\lambda kept out of the averages.

Write α\alpha for the base learning rate, ηt\eta_t for a schedule multiplier (1 for a fixed rate), β1\beta_1 and β2\beta_2 for the decay rates of the two averages, and ϵ\epsilon for a small constant. The paper's defaults are α=0.001\alpha = 0.001, β1=0.9\beta_1 = 0.9, β2=0.999\beta_2 = 0.999 and ϵ=10−8\epsilon = 10^{-8}. All operations on vectors are element by element.

Step 1 in symbols: the gradient

The gradient gtg_t is computed from the data loss alone. Nothing in it depends on λ\lambda.

Step 2 in symbols: the averages

mt=β1mt−1+(1−β1) gt,vt=β2vt−1+(1−β2) gt2m_t = \beta_1 m_{t-1} + (1-\beta_1)\, g_t, \qquad v_t = \beta_2 v_{t-1} + (1-\beta_2)\, g_t^2

Because both start at zero, they are corrected for their start-up bias:

m^t=mt1−β1 t,v^t=vt1−β2 t\hat m_t = \frac{m_t}{1-\beta_1^{\,t}}, \qquad \hat v_t = \frac{v_t}{1-\beta_2^{\,t}}

Step 3 in symbols: the adaptive step

st=α m^tv^t+ϵ\mathbf{s}_t = \alpha\, \frac{\hat m_t}{\sqrt{\hat v_t} + \epsilon}

Step 4 in symbols: the decoupled decay

The update is

θt=θt−1−ηt(α m^tv^t+ϵ+λ θt−1).\theta_t = \theta_{t-1} - \eta_t \left( \alpha\, \frac{\hat m_t}{\sqrt{\hat v_t} + \epsilon} + \lambda\, \theta_{t-1} \right).

This is the paper's decoupled update. The decay term is not multiplied by α\alpha. With ηt=1\eta_t = 1 and a zero loss gradient, each step multiplies the weight by 1−λ1 - \lambda, the same form as ordinary weight decay. That is the sense in which the decay is decoupled: its size is set by λ\lambda and ηt\eta_t, and the adaptive scaling never touches it.

Step 5 in symbols: the L2 version

With L2 inside Adam, the penalty is added to the gradient before the averages:

g~t=gt+λ θt−1,θt=θt−1−ηt α m^(g~)tv^(g~)t+ϵ.\tilde g_t = g_t + \lambda\, \theta_{t-1}, \qquad \theta_t = \theta_{t-1} - \eta_t\, \alpha\, \frac{\hat m(\tilde g)_t}{\sqrt{\hat v(\tilde g)_t} + \epsilon}.

The penalty's contribution to the step is now αλθt−1/(v^t+ϵ)\alpha \lambda \theta_{t-1} / (\sqrt{\hat v_t} + \epsilon), which is divided by the weight's gradient scale. The paper's Propositions 1 and 2 make this precise. For SGD the two forms are equivalent when the L2 strength is set to λ/α\lambda / \alpha. For Adam no single L2 strength reproduces the decoupled update. The reason is that Adam's per-weight scaling is not the same for every weight, so the penalty cannot be matched across weights with one constant.

Results

The main comparison. On image classification benchmarks (CIFAR-10 and a downsampled ImageNet), AdamW generalises better than Adam with L2, and the gain holds across the learning-rate schedules the paper tries.

Hyperparameter landscape. With L2, the good settings for learning rate and weight decay are coupled, so changing one alone hurts. With decoupled decay the good settings are much closer to independent, so the two can be tuned separately. This is the paper's main practical argument: the search is easier.

Long runs. In long training runs AdamW reaches lower test error than Adam with L2. The authors read the gain as a generalisation effect, not only faster convergence.

Warm restarts. Adding warm restarts with cosine annealing (AdamWR) speeds up AdamW, and it narrows the gap to SGD with warm restarts.

Normalized weight decay. The best raw decay value depends on how long the run is and on the batch size. The authors propose a normalized form that scales the decay with those quantities, so one setting carries across runs. They present it as one possible choice, not the only one.

Limits. The experiments are on image classification only, mostly CIFAR-10 and ImageNet32x32 with small residual networks. The authors say their results must be checked on a wider range of tasks, especially ones where regularization matters. The gap to SGD is closed "most of the way", not fully, and the paper does not claim that Adam always beats SGD.

Reading guide

  • Abstract and Section 1. The five observations are the paper's claims in order. Needs Adam Optimizer, Stochastic Gradient Descent and Momentum.
  • The method section. The core of the paper. Needs L2 Regularization and Gradient Descent. Read the two update rules side by side, then the propositions that compare them.
  • The experiments on schedules and hyperparameters. The schedule and hyperparameter plots. Needs Learning Rate. Pay attention to the axes of Figure 2.
  • Sections 4.3 to 4.5. Long runs, warm restarts and other uses. Needs Overfitting and Convolutional Neural Network for the generalisation discussion.
  • Appendix B. Normalized weight decay and AdamWR. Needs the idea of a learning-rate schedule that decreases on a cosine curve and restarts. Take the square-root rule as an empirical choice, which the authors call one possibility among many.

Check yourself

QuestionRecall: what is the form of weight decay that this paper argues is correct for Adam, and where does the decay enter the update?Show answer

It is decoupled weight decay, the update used by AdamW. The decay term λθt−1\lambda\theta_{t-1} is subtracted from the weights together with the adaptive step, and it is not added to the gradient, so the adaptive averages never see it.

QuestionComputation: a weight equals 1.01.0, the decay is λ=0.5\lambda = 0.5, the schedule multiplier is η=1\eta = 1, the learning rate is α=0.01\alpha = 0.01, and the loss gradient is zero at every step so far. What is the weight after one step under AdamW, and under Adam with L2 inside? Ignore ϵ\epsilon.Show answer

Under AdamW the decay term is λθ=0.5×1.0=0.5\lambda\theta = 0.5 \times 1.0 = 0.5, the adaptive part is zero, so the new weight is 1.0−0.5=0.51.0 - 0.5 = 0.5. Under Adam with L2 the penalised gradient is g~=0+0.5×1.0=0.5\tilde g = 0 + 0.5 \times 1.0 = 0.5. Then m^=0.5\hat m = 0.5, v^=0.5\sqrt{\hat v} = 0.5, and the adaptive ratio is 11. The update is 0.01×1=0.010.01 \times 1 = 0.01, so the new weight is 0.990.99. AdamW shrank the weight by half, and L2 inside Adam shrank it by one percent.

QuestionWhy: explain why an L2 penalty inside Adam is weaker for weights whose gradients have been large.Show answer

Inside Adam the penalty is part of the gradient, and the adaptive step divides the whole gradient by v^\sqrt{\hat v}, the typical size of that weight's past gradients. A large history gives a large v^\sqrt{\hat v}, so the penalty's share of the step is small. AdamW subtracts the decay outside that division, so every weight shrinks by the same fraction.

QuestionApplied: a model is trained with AdamW, with decay λ\lambda and a schedule multiplier ηt\eta_t that falls over training. You want the weights to be shrunk less near the end of training, without changing the learning rate α\alpha or λ\lambda. Which quantity should you change, and why does changing it leave the adaptive step alone?Show answer

Change the schedule multiplier ηt\eta_t. In Algorithm 2 the decay per step is ηtλ\eta_t \lambda, so lowering ηt\eta_t weakens the decay without touching α\alpha or λ\lambda. The adaptive step is ηtαm^t/(v^t+ϵ)\eta_t \alpha \hat m_t / (\sqrt{\hat v_t} + \epsilon), so lowering ηt\eta_t also shrinks the adaptive step. The two terms are scaled together, so the schedule changes the overall pace of the update, while the ratio between decay and adaptive step stays the same. Changing α\alpha alone would change only the adaptive step, not the decay.

Sources

Read the full paper

Ilya Loshchilov, Frank Hutter, 2017. arXiv:1711.05101