This page is the worked-examples companion to Adam and AdamW optimizers . The parent note derived the formulas; here we use them on every kind of input the algorithm can meet — the first step, later steps, zero gradients, huge gradients, sign flips, decay, and a couple of exam traps. By the end you should never hit a case you haven't seen.
Everything below leans on four moving parts you already met in the parent note. Let's re-anchor them in one place so no symbol is unexplained.
Definition The four quantities we recompute every step
For a single parameter θ at step t , with raw gradient g t :
m t = β 1 m t − 1 + ( 1 − β 1 ) g t — the first moment , a smoothed direction .
v t = β 2 v t − 1 + ( 1 − β 2 ) g t 2 — the second moment , a smoothed squared magnitude .
m ^ t = m t / ( 1 − β 1 t ) , v ^ t = v t / ( 1 − β 2 t ) — the bias-corrected versions (un-shrunk because the EMAs started at 0).
update: θ t = θ t − 1 − η v ^ t + ϵ m ^ t (Adam) with an extra − η λ θ t − 1 for AdamW.
Default hyperparameters unless a problem overrides them: β 1 = 0.9 , β 2 = 0.999 , ϵ = 1 0 − 8 .
Think of m ^ t / v ^ t as a compass : it mostly reports which way to move, and its length hovers near 1 rather than tracking how big the gradient is. Keep that picture — most surprises below come from it.
Every situation Adam can face falls into one of these cells. The examples that follow are tagged with the cell they cover.
Cell
What's special about the input
Covered by
A. First step
t = 1 , EMAs still empty
Example 1
B. Positive vs negative gradient
sign of g
Example 1 (+), Example 2 (−)
C. Steady state, constant g
t → ∞ , gradient stable
Example 3
D. Huge vs tiny gradient, same direction
magnitude extremes
Example 4
E. Zero / degenerate gradient
g = 0 , or g suddenly vanishes
Example 5
F. Sign flip / oscillating gradient
g changes sign each step
Example 6
G. AdamW vs Adam+L2 decay
λ > 0 , decoupled vs coupled
Example 7
H. Early-step instability without bias correction
t small, correction on/off
Example 8
I. Word problem (real training)
choosing η , λ in practice
Example 9
J. Exam twist
ϵ dominating, effective-LR cap
Example 10
Prerequisites worth a glance if any line feels unfamiliar: Exponential Moving Average , Momentum and Nesterov Acceleration , RMSProp , Stochastic Gradient Descent , Weight Decay and L2 Regularization , Bias-Variance in Gradient Estimation .
Worked example Example 1 — The very first step (Cells A, B+)
Start at θ 0 = 0.5 , gradient g 1 = 0.2 , with η = 0.1 , defaults, and ϵ = 0 for clarity.
Forecast: guess the step size before reading on. Does a gradient of only 0.2 give a tiny step?
m 1 = 0.9 ( 0 ) + 0.1 ( 0.2 ) = 0.02 . Why this step? m is an EMA that begins at 0 ; one gradient nudges it a little.
v 1 = 0.999 ( 0 ) + 0.001 ( 0. 2 2 ) = 4 × 1 0 − 5 . Why? Same EMA, but on the square 0.04 .
m ^ 1 = 0.02/ ( 1 − 0.9 ) = 0.2 . Why? At t = 1 the factor 1 − β 1 1 = 0.1 exactly cancels the ( 1 − β 1 ) shrink — bias correction fully restores the raw gradient.
v ^ 1 = 4 × 1 0 − 5 / ( 1 − 0.999 ) = 0.04 . Why? Restores the raw g 2 .
step = η m ^ 1 / v ^ 1 = 0.1 ⋅ 0.2/0.2 = 0.1 . Why? m ^ / v ^ = g /∣ g ∣ = sign ( g ) = + 1 ; the compass length is exactly 1 .
θ 1 = 0.5 − 0.1 = 0.4 .
Verify: the step magnitude is η = 0.1 , not η ∣ g ∣ = 0.02 . The first Adam step is a normalized unit-compass step whose size ignores ∣ g ∣ . ✓
Worked example Example 2 — First step with a negative gradient (Cell B−)
Same setup as Example 1 but g 1 = − 0.2 .
Forecast: the gradient flipped sign. Does the step size change, or only its direction?
m 1 = 0.1 ( − 0.2 ) = − 0.02 . Why? EMA carries the sign.
v 1 = 0.001 ( 0.04 ) = 4 × 1 0 − 5 . Why? Squaring kills the sign — magnitude tracker is sign-blind.
m ^ 1 = − 0.2 , v ^ 1 = 0.04 . Why? Same un-shrinking as before.
step = η m ^ 1 / v ^ 1 = 0.1 ⋅ ( − 0.2 ) /0.2 = − 0.1 . Why? Compass = sign ( g ) = − 1 .
θ 1 = 0.5 − ( − 0.1 ) = 0.6 . Why? Subtracting a negative step moves θ up , toward lower loss.
Verify: magnitude is still η = 0.1 ; only the direction reversed. Because v squares away the sign, positive and negative gradients of equal size give mirror-image steps. ✓
Worked example Example 3 — Steady state on a constant gradient (Cell C)
Feed the same gradient g = 0.3 forever. Find the limiting per-step size (take ϵ → 0 ).
Forecast: as the EMAs settle, does the step keep shrinking, grow, or lock onto a fixed value?
As t → ∞ an EMA of a constant equals that constant: m t → g , v t → g 2 . Why? m ∞ = β 1 m ∞ + ( 1 − β 1 ) g ⇒ m ∞ = g .
β 1 t , β 2 t → 0 , so bias correction vanishes: m ^ t → g , v ^ t → g 2 . Why? The ( 1 − β t ) denominators both go to 1 .
step → η g / g 2 = η sign ( g ) . Why? g 2 = ∣ g ∣ , so g /∣ g ∣ = sign ( g ) .
Verify: with g = 0.3 , η = 0.1 the steady step is 0.1 , and it would be the same 0.1 for g = 300 . Adam has normalized gradient scale completely away in steady state. ✓
What the figure shows: the plot below traces the Adam step magnitude against the step count t for three constant gradients — g = 0.3 (teal), g = 30 (orange), g = 0.001 (plum). Read three things off it. (1) All three curves start high or low but bend toward the same dashed line at η = 0.1 — that is the η sign ( g ) plateau this example predicts. (2) The convergence takes hundreds of steps, driven by the slow β 2 = 0.999 memory. (3) The final height is identical regardless of whether the gradient was 0.001 or 30 — visual proof that Adam erases gradient scale in steady state.
Worked example Example 4 — Huge vs tiny gradient, same direction (Cell D)
Two parameters, both fed a constant gradient forever: θ big sees g = 50 , θ tiny sees g = 0.001 . Compare their steady steps, η = 0.01 , ϵ → 0 .
Forecast: surely the parameter with g = 50 takes a 50000× larger step?
Both reach m ^ / v ^ = sign ( g ) = + 1 (Example 3 logic). Why? The ratio is scale-invariant : replace g by c g and m ^ → c m ^ , v ^ → ∣ c ∣ v ^ , cancelling.
Steady step = η ⋅ 1 = 0.01 for both . Why? Nothing left depends on ∣ g ∣ .
Verify: identical 0.01 steps despite a 50000× gradient gap. This is exactly the "bigger gradient ⇒ bigger step" misconception the parent note carefully dismantled — Adam responds to consistency , not raw size. ✓
Worked example Example 5 — Zero / vanishing gradient (Cell E)
A parameter has been getting g = 0.4 for many steps (so v has grown), then suddenly g = 0 for one step. State t = 1000 , v ^ 999 ≈ 0.16 , η = 0.01 , ϵ = 1 0 − 8 .
Forecast: does the step instantly become 0 ?
New m 1000 = 0.9 m 999 + 0.1 ( 0 ) : momentum still carries the old direction, decayed once. Why? EMA has memory; one zero doesn't erase it. If m 999 ≈ 0.4 then m 1000 ≈ 0.36 .
New v 1000 = 0.999 v 999 + 0.001 ( 0 ) ≈ v 999 ≈ 0.16 . Why? β 2 = 0.999 means the magnitude memory barely moves.
step ≈ η m ^ / v ^ ≈ 0.01 ⋅ 0.36/0.4 ≈ 0.009 . Why? Still a near-full compass step, because momentum remembers where we were heading.
Contrast: if g had been 0 from the very start (m = v = 0 ), then step = η ⋅ 0/ ( 0 + ϵ ) = 0 . Why? Nothing to move on; the ϵ floor keeps it from dividing 0/0 .
Verify: a momentary zero gradient still gives ≈ 0.009 , not 0 — Adam glides through flat spots. A permanent zero gives exactly 0 . Both cases handled without blow-up thanks to ϵ . ✓
Worked example Example 6 — Oscillating (sign-flipping) gradient (Cell F)
A parameter sits in a sharp valley: gradient alternates g = + 1 , − 1 , + 1 , − 1 , … . What does Adam do? η = 0.01 .
Forecast: with the direction flipping every step, does Adam keep taking full ± η steps or damp itself?
v tracks g 2 = 1 regardless of sign, so v ^ → 1 , v ^ → 1 . Why? Squaring erases the flips — the magnitude tracker sees a steady 1 .
m averages + 1 and − 1 , so m ^ → 0 . Why? Opposite-sign values cancel in the EMA; the direction signal is washed out.
step = η m ^ / v ^ → η ⋅ 0/1 = 0 . Why? Zero net direction ⇒ (near) zero step.
Verify: in a bouncing direction the step shrinks toward 0 — Adam refuses to oscillate wildly across a valley, unlike a large fixed-η SGD step that would ping-pong. This is the "signal-to-noise" intuition made concrete: high magnitude, low mean ⇒ small step. ✓
Worked example Example 7 — AdamW vs Adam+L2 on two weights (Cell G)
Two weights, both at θ = 1 , both with decay λ = 0.01 , η = 1 . Weight A has tiny gradients so v ^ A ≈ 0.01 ; weight B has big gradients so v ^ B ≈ 1 . Compare the decay contribution only .
Forecast: with the same λ and θ , do both weights shrink by the same amount under each method?
Adam + L2 folds λ θ into the gradient, so its decay term is later divided by v ^ : decay = η λ θ / v ^ .
A : 1 ⋅ 0.01 ⋅ 1/0.01 = 1.0 .
B : 1 ⋅ 0.01 ⋅ 1/1 = 0.01 . Why bad? A decays 100× more than B — decay strength depends on unrelated gradient history.
AdamW keeps decay outside the v ^ machinery: decay = η λ θ .
A : 0.01 . B : 0.01 . Why good? Uniform shrink factor ( 1 − η λ ) for every weight — genuine weight decay.
Verify: AdamW gives 0.01 for both; Adam+L2 gives 1.0 vs 0.01 , a 100× discrepancy. The two are provably not the same whenever λ > 0 and gradients differ. ✓
Worked example Example 8 — Early-step instability without bias correction (Cell H)
At t = 1 , g 1 = 0.2 , defaults, ϵ = 0 . Compute the step with and without bias correction.
Forecast: the correction "vanishes as t → ∞ ", so at t = 1 is it a tiny cosmetic tweak?
Uncorrected: m 1 = 0.02 , v 1 = 4 × 1 0 − 5 , step = η m 1 / v 1 = 0.1 ⋅ 0.02/ 4 × 1 0 − 5 . Why? Use the raw EMAs. 4 × 1 0 − 5 ≈ 0.006325 .
That gives step ≈ 0.1 ⋅ 0.02/0.006325 ≈ 0.316 . Why alarming? A step of 0.316 from a gradient of 0.2 — over 3× the intended η .
Corrected (Example 1): step = 0.1 exactly. Why? Correction divides m by 0.1 but v by 0.001 ≈ 0.0316 ; the mismatch is what fixes the ratio.
Verify: uncorrected ≈ 0.316 vs corrected = 0.1 — the correction is a 3.16× factor at t = 1 , exactly 1/ 1 − β 2 = 1/ 0.001 . Far from cosmetic when training is most fragile. ✓
Worked example Example 9 — Word problem: picking
η and λ (Cell I)
You fine-tune a transformer with AdamW. You want (a) each weight to decay by 1% per step independent of gradients, and (b) the raw update step near steady state to be about 3 × 1 0 − 4 . Which η , λ ?
Forecast: are η and λ independent knobs here, or do they interact?
Steady raw update magnitude ≈ η ⋅ ∣ sign ( g ) ∣ = η (Example 3). So set η = 3 × 1 0 − 4 . Why? In AdamW the adaptive part contributes ≈ η per step; that's your learning-rate budget.
Decay factor per step is ( 1 − η λ ) . Want it = 0.99 , i.e. η λ = 0.01 . Why? AdamW subtracts η λ θ , a fractional shrink of η λ .
Solve λ = 0.01/ η = 0.01/ ( 3 × 1 0 − 4 ) ≈ 33.3 . Why? Decouple: because decay is η λ θ , a small η forces a large λ for the same shrink.
Verify: η λ = 3 × 1 0 − 4 ⋅ 33.33 = 0.01 ✓ ⇒ shrink factor 0.99 ✓, and steady step η = 3 × 1 0 − 4 ✓. (In practice frameworks report the product η λ as the effective decay, which is why AdamW decay must be retuned whenever you change η — see Learning Rate Schedules .)
Worked example Example 10 — Exam twist:
ϵ dominates (Cell J)
A brand-new parameter fires once with a microscopic gradient g 1 = 1 0 − 6 . Defaults, ϵ = 1 0 − 8 , η = 1 0 − 3 . What is the actual step? Does the compass still have length 1 ?
Forecast: Example 1 said the first step is always η . Is that exactly true here?
m ^ 1 = g 1 = 1 0 − 6 , v ^ 1 = g 1 2 = 1 0 − 12 , so v ^ 1 = 1 0 − 6 . Why? Bias correction restores raw values at t = 1 .
Now compare v ^ 1 = 1 0 − 6 with ϵ = 1 0 − 8 : here v ^ 1 ≫ ϵ , so ϵ is negligible and step ≈ η = 1 0 − 3 . Why check? ϵ only bites when v ^ 1 ≲ ϵ .
Push to the extreme: if instead g 1 = 1 0 − 10 , then v ^ 1 = 1 0 − 10 ≪ ϵ = 1 0 − 8 , and step = η m ^ / ( v ^ + ϵ ) ≈ η ⋅ 1 0 − 10 /1 0 − 8 = η ⋅ 1 0 − 2 = 1 0 − 5 . Why? When v ^ is negligible next to ϵ , the denominator freezes at ϵ and the step becomes η m ^ / ϵ — which grows with m ^ but stays bounded because m ^ itself is tiny here.
What is the largest step ϵ permits? Set v ^ → 0 so the denominator is just ϵ : then step = η m ^ / ϵ . Since a single coordinate's ∣ m ^ ∣ ≤ v ^ can never exceed... more simply, in the extreme v ^ → 0 with m ^ → 0 together (as here, m ^ = g , v ^ = ∣ g ∣ ), the ratio m ^ / ( v ^ + ϵ ) = g / ( ∣ g ∣ + ϵ ) ≤ 1 , so the compass length is at most 1 and the step is at most η . Why this matters: ϵ prevents the tiny-gradient case from blowing up the step; it caps the compass at length 1, not above it.
Verify: with g 1 = 1 0 − 6 step ≈ 1 0 − 3 = η (compass ≈ 1 ); with g 1 = 1 0 − 10 step ≈ 1 0 − 5 (compass = 1 0 − 10 / ( 1 0 − 10 + 1 0 − 8 ) ≈ 1 0 − 2 ). So "first step = η " holds only while v ^ ≫ ϵ ; once ϵ dominates it shrinks the step below η . The exam trap is forgetting ϵ is a real hyperparameter, not just a divide-by-zero guard. ✓
Recall Cover the answers — one line each
First-step size regardless of |g| ::: η (compass length 1), as long as v ^ ≫ ϵ .
Steady-state step on constant g ::: η sign ( g ) , independent of ∣ g ∣ .
What an oscillating plus/minus gradient does to the step ::: drives m ^ → 0 so the step → 0 (Adam refuses to ping-pong).
Momentary zero gradient at step 1000 (m about 0.36, sqrt vhat about 0.4, eta 0.01) ::: step ≈ 0.009 — momentum carries it through.
AdamW decay for a weight at theta=1, eta=1, lambda=0.01 ::: 0.01 (uniform); Adam+L2 gives 0.01/ v ^ (gradient-dependent), e.g. 1.0 vs 0.01 .
Bias-correction size at t=1 on the v path ::: a ≈ 3.16 × step change (1/ 1 − β 2 = 1/ 0.001 ).
When does epsilon actually change the step ::: when v ^ ≲ ϵ ; then it shrinks the compass below length 1, keeping the step ≤ η .
Mnemonic The compass mantra
Direction from m ^ , brakes from v ^ , restart-boost from bias correction, uniform shrink from AdamW.