3.2.5 · D4Training Deep Networks

Exercises — Adam and AdamW optimizers

2,939 words13 min readBack to topic

Every symbol used below was defined in the parent note. As a one-line refresher:


Level 1 — Recognition

Recall Solution
  • First moment = EMA of the gradient: . It smooths the direction — this is the momentum idea.
  • Second moment = EMA of the squared gradient: . It smooths the magnitude — this is the RMSProp idea. One tracker says where to go, the other says how big a step is safe there.
Recall Solution
  • (P) is AdamW. The decay term sits outside the fraction, so it is never divided by the magnitude tracker. See Weight Decay and L2 Regularization.
  • (Q) is Adam+L2. The is folded into , so it flows through and gets divided by — the very bug AdamW removes.
Recall Solution

It is a signal-to-noise ratio. is the mean gradient, is its typical size.

  • Ratio : the gradient points consistently the same way (mean magnitude) ⇒ confident, take a full step of size .
  • Ratio : the gradient jitters around zero (mean magnitude) ⇒ unsure, take a tiny step. This is a form of variance-aware stepping.

Level 2 — Application

Recall Solution
  • .
  • .
  • . (At this fully un-shrinks : recovers .)
  • . (Recovers .)
  • step .
  • . Notice: the step magnitude is exactly , independent of — the "compass step".
Recall Solution
  • .
  • .
  • .
  • .
  • step , so . On a constant gradient the corrected estimates stay pinned at , , so every step keeps size . This is the steady-state behaviour previewed in the parent note.
Recall Solution

The decay part is , so . Surviving fraction . Each step shrinks every weight by — the same for all weights, which is exactly what classical weight decay wants.


Level 3 — Analysis

Recall Solution

At : and . Bias correction: , and . Step . The magnitude is , free of . Look at figure s01: the SGD step (yellow) grows with the gradient, while the Adam step (blue) is clamped to length in both cases.

Figure — Adam and AdamW optimizers
Figure s01 caption: horizontal axis = gradient magnitude ; vertical axis = step magnitude. Yellow line = SGD step (a straight line through the origin, growing without bound). Blue flat line = Adam's first step (constant at height regardless of ). Dotted markers at and show SGD leaping to different heights while Adam stays pinned.

Recall Solution

Replace for all . Both EMAs are linear in their inputs:

  • (since is a weighted sum of gradients), so .
  • (inputs are ), so , hence . Step — the cancels. Consequence: Adam is immune to global gradient rescaling (e.g. a change of loss units). Plain SGD is not — its step scales with .
Recall Solution
  • With : step — a 10× blow-up.
  • With : , step . Barely different here.
  • Now push : with the step ; with the step is capped at . So is not just anti-divide-by-zero — it bounds the maximum effective learning rate in near-flat regions. It is a real hyperparameter.

Level 4 — Synthesis

Recall Solution

Decay contribution = the part of the step coming from the term. (i) Adam+L2: the is folded into the gradient, so it is divided by : .

  • : .
  • : . is decayed 100× harder than — purely because of gradient history. Backwards. (ii) AdamW: for both and . Uniform, gradient-independent — the correct behaviour. Figure s02 shows the two decay bars.
    Figure — Adam and AdamW optimizers
    Figure s02 caption: grouped bar chart. Left group = weight , right group = weight . Red bars = Adam+L2 decay contribution ( for , for — a 100× spread). Green bars = AdamW decay contribution ( for both — equal height). The visual punch: red bars are wildly uneven, green bars are identical, showing AdamW's uniform shrinkage.
Recall Solution

Unroll the EMA from : Take expectations, using : The sum is a geometric series . So This is smaller than the target by the factor . Dividing by that factor gives — unbiased. As , and the correction (vanishes). The identical argument on the squared stream gives .

Recall Solution
flowchart TD
  G["raw gradient g_t"] --> M["update m_t EMA of g"]
  G --> V["update v_t EMA of g squared"]
  M --> MH["bias correct m hat"]
  V --> VH["bias correct v hat"]
  MH --> R["ratio m hat over root v hat"]
  VH --> R
  R --> U["subtract eta times ratio"]
  TH["current weight theta"] --> D["decay subtract eta lambda theta"]
  U --> NEW["new weight"]
  D --> NEW

Node TH is the current weight ; node D is the decay step applied straight to it. The decay arrow (TH --> D) feeds the weight in directly, never touching the ratio node R. Adam+L2's mistake is to instead route decay through node G (fold into the gradient), so it passes through V/VH and gets divided by . AdamW keeps the two paths separate.


Level 5 — Mastery

Recall Solution

Observable: the norm of the very first update, , relative to .

  • Correct Adam: at , per-coordinate exactly (from L3-Q1). So for parameters — a clean, gradient-independent value.
  • Bug (both corrections off): use raw and raw . Per-coordinate step . The cancels, giving a gradient-independent constant: . Signature: the buggy first-step norm is instead of the correct . Concretely, dividing the measured first-step norm by should give if correct, if both corrections are missing — a clean, model-size-independent tell.
Recall Solution

Pick AdamW. Mechanistic argument:

  1. Transformer parameters have wildly different gradient scales across layers/embeddings, so a per-parameter adaptive rate (RMSProp-style ) is worth more than a single SGD rate.
  2. Heavy overfitting means regularization strength must be predictable. Adam+L2 makes decay gradient-dependent (L4-Q1: 100× spread between weights), so you cannot reason about how hard any weight is being pulled to zero.
  3. AdamW's decoupled shrinks every weight by the identical factor each step, giving clean, tunable regularization on top of the adaptive step. Net: you get the adaptivity of Adam and honest weight decay — exactly why AdamW is the default for transformers.
Recall Solution

The AdamW decay term is . If during warmup, then decay too — regularization is effectively off while is small, because decay is scaled by the same as the gradient step. Fixes:

  • Use a decay strength that is applied on a schedule decoupled from (some implementations let you scale decay by a fixed rate independent of the warmup ).
  • Or accept that regularization ramps in with the warmup, and set the schedule/ so that over the full run the accumulated shrinkage reaches the intended target. The lesson: in AdamW, multiplies both the step and the decay, so any -schedule silently reshapes regularization over time.

Recall

Recall One-line answers to re-derive

First-step Adam magnitude (any ) ::: exactly Steady-state Adam step on constant ::: AdamW surviving fraction per step from decay ::: What multiplying all gradients by does to the Adam step ::: nothing (scale-invariant) The factor is biased low by at step ::: The one thing decay in AdamW is NOT decoupled from ::: the learning rate