Equations · Loshchilov and Hutter, “Decoupled Weight Decay Regularization” (2019)
The AdamW update rule (decoupled weight decay), explained
AdamW is Adam with weight decay applied directly to the weights, outside the adaptive step. Adding an L2 penalty to the loss instead sends the decay through Adam's per-parameter scaling, which weakens it for exactly the weights with large gradients. AdamW is the default optimizer for training Transformers.
Term by term
The parameters after and before step .
Adam's bias-corrected moving averages of the gradient and the squared gradient, computed exactly as in Adam.
The usual Adam step, with step size .
Alpha · Fraction · Square root · Epsilon
The decoupled weight decay: shrink every weight toward zero by a fixed fraction , regardless of its gradient history.
A schedule multiplier, such as a cosine schedule, applied to both parts. In PyTorch's version the decay is scaled by the learning rate: .
A small worked example
PyTorch defaults, and ; one weight with gradient on the first step. Decay: . Adam step: on step one, so . New weight: . With L2 in the loss instead, the gradient becomes , and Adam's normalization makes the first step exactly again: the decay vanished.
In code
import numpy as np
def adamw_step(theta, g, m, v, t, lr=1e-3, b1=0.9, b2=0.999, eps=1e-8, wd=0.01):
theta = theta - lr * wd * theta # decoupled decay (PyTorch's scaling)
m = b1 * m + (1 - b1) * g
v = b2 * v + (1 - b2) * g**2
m_hat, v_hat = m / (1 - b1**t), v / (1 - b2**t)
return theta - lr * m_hat / (np.sqrt(v_hat) + eps), m, vEvery symbol in it
Other equations
- Scaled dot-product attention
- Softmax
- Cross-entropy loss
- Adam update
- Layer normalization
- RMSNorm
- The ELBO
- KL divergence
- Backpropagation through one layer
- LoRA
- GELU
- Dropout
- Batch normalization
- Rotary position embedding (RoPE)
- Multi-head attention
- PPO clipped objective
- DPO loss
- Policy gradient (REINFORCE)
- Bellman equation
- Q-learning update
- InfoNCE contrastive loss
- Diffusion forward process
- DDPM simplified loss
- VAE reparameterization trick
- GAN minimax objective
- Cosine learning-rate schedule
- Perplexity
- Logistic regression
- Normal equation (linear regression)
- Singular value decomposition (SVD)
- Principal component analysis (PCA)