↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading the recording…

What AdamW is

AdamW gives every weight its own step size. It keeps two running averages per weight, of the gradient and of its square, divides the first by the square root of the second, and shrinks the weights by a separate decoupled weight decay. Most large language models are pretrained with it.

This tab trains the explainer's small GPT (16,081,664 parameters, 4 layers, width 256) on TinyStories for 2,000 steps with the same data, initial weights and schedule as the SGD, Lion and Muon tabs, and records AdamW's state for a corner of block 2's attention value projection at the steps in the toolbar.

First and second moments

With the gradient of weight on batch , AdamW updates two averages that start at zero:

, the first moment, is a momentum over roughly the last gradients; , the second moment, is a running mean of the squared gradient over roughly steps, with and as in GPT-3's recipe. The First moment and Second moment rows of the panel draw , and for a 64 × 64 corner of , with last step's state drawn small above each update.

Bias correction

Because , after steps both averages are too small: their weights sum to , not 1. Adam divides that out,

which matters only early on. At step 1, and , so the ratio below is for almost every weight: in the recording, 99.99 % of 's entries have at step 1. By step 1,000 the correction factors are within 5 × 10⁻²³ of 1.

A step of about η for every weight

The update divides the corrected momentum by the root of the corrected second moment:

The ratio has no units: scaling a weight's gradients by any factor leaves its step unchanged. When a weight's gradients keep their sign, and it moves by about ; when they flip from batch to batch, averages towards zero while does not, and the step shrinks. In the recording the ratio's RMS over falls from 0.9999 at step 1 to 0.197 at step 1,000. The Every weight tensor panel shows the effect across the model: AdamW's relative steps are far more even than SGD's, spanning a factor of 20 between groups of tensors at step 1,000 against SGD's 96.

Decoupled weight decay

Adding an L2 penalty to the loss would put into the gradient, where Adam's division by would shrink weights with large gradients less than others. Loshchilov and Hutter's AdamW applies the decay directly instead, , so it never enters or . Here on the matrices and embeddings and 0 on the LayerNorm gains and biases. On at step 1,000 the decay's RMS is 1.29 × 10⁻⁵, against 4.64 × 10⁻⁴ for the adaptive part.

In this run

The sweep of 500-step runs chose a peak learning rate of 4 × 10⁻³. After 2,000 steps AdamW reaches a validation loss of 1.908, against 3.059 for SGD, 2.190 for Lion and 1.729 for Muon. Its price is memory: two states per weight, twice SGD's buffer and Lion's single momentum.

Where it came from

AdaGrad (2011) first scaled each weight by its accumulated squared gradients; RMSProp replaced the sum by a running average; Adam (Kingma and Ba, 2014) added momentum and bias correction. Loshchilov and Hutter decoupled the weight decay (2017), and AdamW with became the standard for pretraining large transformers. The Lion tab keeps only the sign of a momentum, and Muon replaces the per-weight scale with a per-matrix orthogonalization.

Papers

  1. Decoupled Weight Decay Regularization Ilya Loshchilov, Frank Hutter · ICLR 2019 AdamW: weight decay applied to the weights directly instead of through the adaptive gradient.
  2. Adam: A Method for Stochastic Optimization Diederik P. Kingma, Jimmy Ba · ICLR 2015 The first and second moments and their bias correction.
  3. Adaptive Subgradient Methods for Online Learning and Stochastic Optimization John Duchi, Elad Hazan, Yoram Singer · Journal of Machine Learning Research 2011 AdaGrad, the first per-weight step size from accumulated squared gradients.
  4. Language Models are Few-Shot Learners Tom B. Brown et al. · NeurIPS 2020 GPT-3's training recipe: AdamW with β₂ = 0.95, weight decay 0.1, warmup and cosine decay, the settings this run follows.
  5. Noise Is Not the Main Factor Behind the Gap Between SGD and Adam on Transformers, but Sign Descent Might Be Frederik Kunstner, Jacques Chen, Jonathan Wilder Lavington, Mark Schmidt · ICLR 2023 Adam behaves much like sign descent on transformers, which explains most of its advantage over SGD.
  6. TinyStories: How Small Can Language Models Be and Still Speak Coherent English? Ronen Eldan, Yuanzhi Li · arXiv 2023 The dataset all four optimizer runs train on.

See also