Foundations

Optimizers: SGD to Momentum to Adam to AdamW

Adam buys per-coordinate step sizes by keeping two moment tensors alongside the parameters — extra state that makes full training far more memory-intensive than inference.

Updated

01 · Concept

Concept

Lesson 2.6 left a concrete failure on the table. In a bowl a hundred times steeper along one axis than another, any learning rate small enough to keep the steep direction stable leaves the shallow direction crawling, and any rate fast enough for the shallow direction blows the steep one up. Backpropagation cannot help — it reported both gradients correctly. The fix has to live in how the update uses them, which is what an optimizer is.

Plain minibatch SGD does the minimum: θt=θt1ηgt\theta_t=\theta_{t-1}-\eta g_t. Little state, clear interpretation, and exactly the failure described above.

Momentum adds a velocity, usually an exponential moving average of gradients, and steps along that instead of the raw gradient. Components that persist across steps accumulate; components that alternate in sign partly cancel. In the ravine, the wall-to-wall bouncing along the steep axis largely cancels itself while the steady push along the shallow axis builds up. Different libraries use slightly different equations and Nesterov variants, so hyperparameters do not transfer by name alone.

Adam attacks the scale mismatch directly. Per coordinate it keeps a first moment mtm_t, a smoothed gradient, and a second raw moment vtv_t, a smoothed squared gradient. After bias correction — necessary because both averages start at zero and are otherwise biased toward it early on — the update is roughly

θt=θt1ηm^tv^t+ϵ.\theta_t=\theta_{t-1}-\eta\,\frac{\hat m_t}{\sqrt{\hat v_t}+\epsilon}.

Work out what that does to the ravine. Along the steep axis the gradients are consistently large, so v^\sqrt{\hat v} is large and divides the step back down. Along the shallow axis the gradients are consistently tiny, so v^\sqrt{\hat v} is tiny and divides the step back up. If a coordinate’s gradient has a steady magnitude g|g|, then m^g\hat m\approx g and v^g\sqrt{\hat v}\approx|g|, so the ratio is approximately ±1\pm1 and the parameter moves by about η\eta — regardless of whether its gradient was 10x10x or 0.1y0.1y. Adam has made the step size roughly scale-free per direction. That is the property lesson 2.6 said one global η\eta could not provide.

Now the classic wrong turn, which shipped in real code for years. You want regularization, so you add an L2 penalty to the loss. Its gradient contribution is λθ\lambda\theta, and you hand the sum to Adam. Reason about what happens next: that term goes into mm and vv along with everything else, and then the whole thing is divided by v^\sqrt{\hat v}. A parameter whose gradients happen to be large gets its decay shrunk; a parameter with tiny gradients gets its decay amplified. The intended behavior — pull every weight toward zero by the same proportion each step — is not what occurs, and the strength of the regularizer now depends on unrelated gradient statistics. AdamW fixes it by decoupling: compute the adaptive update, apply it, then separately shrink the parameters by ηλθ\eta\lambda\theta. Same name, different operation, and the reason essentially every large model today is trained with AdamW rather than Adam plus L2.

Not every parameter should decay. Biases and normalization scale parameters are commonly placed in a no-decay group, a configuration detail that is a frequent source of silent bugs — log which parameter names entered which group and fail loudly on unexpected omissions.

Schedules matter as much as the update rule. Warmup keeps early effective steps small while activations and moment estimates settle; decay reduces the step later for convergence. A fair experiment reports the optimizer equations or implementation, the learning-rate curve, batch size, clipping threshold, decay groups, total tokens, and seeds. SGD, Momentum, Adam, and AdamW are update rules with different inductive biases, not a chronological ladder on which the newest name wins.

One operational detail that costs people days: optimizer checkpoints must include the step counter and both moment buffers, not only the model weights. Resuming Adam with empty moments silently changes the update rule for hundreds of steps even though the parameters match exactly, and stepping the schedule once per batch rather than once per accumulation boundary produces a different learning-rate curve entirely. A reproducible run records those boundaries and verifies the first resumed update against an uninterrupted reference.

02 · Analogy

Analogy

Four cyclists descend a winding track. Plain SGD steers only from the current slope. Momentum carries velocity, smoothing alternating turns. Adam keeps separate gauges for recent direction and squared magnitude on every control, taking smaller steps where gradients are consistently large. AdamW also applies a deliberate shrinkage to weights as a separate maintenance rule. No bicycle wins every track; schedule, surface, and tuning determine the race.

03 · Teach it back

Teach it back

Explain how Adam's second moment gives each coordinate its own effective step size, why AdamW decouples weight decay, and why Adam's additional state makes full training much more memory-intensive than inference.

Minimum: 80 characters and 15 words. Your writing stays only in this browser.

Waiting for your explanation.

Compare with a model answer

Adam keeps a first moment m (a smoothed gradient) and a second raw moment v (a smoothed squared gradient) per coordinate, bias-corrects both, and steps by m̂ divided by the square root of v̂ plus epsilon. Dividing by the typical magnitude makes the step roughly scale-free per coordinate, so a steep direction and a shallow one advance at comparable rates under one global learning rate. Adding an L2 term to the gradient does not survive that division as uniform shrinkage, so AdamW applies decay directly to the parameters after the adaptive step. The two moments, and often a higher-precision master copy, live alongside weights, gradients, and activations; their exact memory depends on dtype, bookkeeping, and sharding, whose full arithmetic belongs to lesson 9.3.

04 · Check your understanding

Check your understanding

01Lesson 2.6 showed one global learning rate cannot serve a steep and a shallow direction at once. How does Adam attack that specific problem?
Answer and explanation

It divides each coordinate's step by the square root of that coordinate's recent squared-gradient average, so steps become roughly scale-free per direction — The second moment acts as a cheap per-coordinate normalizer, approximating useful geometric behavior without forming a second-derivative matrix.

02What is AdamW's defining change relative to Adam with an L2 penalty added to the loss?
Answer and explanation

Weight decay is applied directly to the parameters, outside the adaptive gradient computation — Passing the penalty through Adam's per-coordinate division makes shrinkage depend on gradient history, which is not what weight decay is supposed to mean.

03Why does AdamW make full-training memory much larger than inference memory?
Answer and explanation

It keeps two moment tensors per trained parameter, often alongside a higher-precision master copy, gradients, and activations — The exact byte budget depends on dtypes, copies, and sharding and is derived once in lesson 9.3; the durable point here is that optimizer state adds tensors for every trained parameter.

Complete the teach-back and answer the quiz correctly to finish this lesson.

◎ · Evidence marker

Sources

  1. Diederik P. Kingma and Jimmy Ba (2014). Adam: A Method for Stochastic Optimization.
  2. Ilya Loshchilov and Frank Hutter (2017). Decoupled Weight Decay Regularization.
  3. Microsoft DeepSpeed Team (2026). DeepSpeed Memory Requirements.
  4. Qwen Team (2026). Qwen3.8-27B Model Card.