Press → to start · click the grid icon to jump to any slide
100%
Press → to start · click the grid icon to jump to any slide
Slide 1 / 15
Pinned slides 0
Pins stay while this tab is open, even after a refresh. Closing the tab clears them, so download to keep your notes; you’ll be warned before leaving if they haven’t been downloaded.

Beyond Vanilla Gradient Descent
Momentum · RMSProp · Adam

CS 4337 · Deep Learning · Optimizers
θ ← θ − η ∇L A jagged loss landscape Exponential moving average Momentum = EMA of g RMSProp = EMA of g² Adam = both + bias fix
Two weights θ₁, θ₂ · one bumpy loss surface · four ways to walk downhill
Prepared by Keshav
The old update — θ ← θ − η · ∂L/∂θ
Two weights, θ₁ and θ₂. Each one moves against its own slope, scaled by one shared learning rate η.
Weight 1
θ₁ ← θ₁ − η · ∂L/∂θ₁
Weight 2
θ₂ ← θ₂ − η · ∂L/∂θ₂
Vector form — what every framework does
θ ← θ − η · ∇L(θ)    with   ∇L = (∂L/∂θ₁, ∂L/∂θ₂)
for step in range(num_steps): g1, g2 = grad_L(theta1, theta2) # slopes at current point theta1 = theta1 - eta * g1 theta2 = theta2 - eta * g2
Only information used: the slope right here, right now. No memory of previous steps, and the same η for both weights. Keep that in mind — every trick today fixes one of those two blind spots.
0.050
Our playground — a jagged loss surface L(θ₁, θ₂)
One global minimum, three local minima, and small ripples everywhere — a cartoon of a real network’s loss.
3D surface
Contour view (top-down)

Surface

1.00
★ Global minimum
Deepest valley near θ = (2, 2), L ≈ −2.56. This is where we want to end up.
A · B · C local minima
Shallower valleys. Slope is zero at the bottom too, so plain GD happily stops there.
Ripples
L = smooth bowl + wells + 0.26·sin 3θ₁·cos 3θ₂ + 0.11·sin(5θ₁+3θ₂). Every little dimple is a tiny local minimum. Slide jaggedness to 0 to see the smooth version.
Vanilla GD on the jagged surface — where does it stop?
θ ← θ − η·∇L. Click the contour map to choose a start, or use a preset. Press ▶ to roll.
3D surface
Contour view · click to set start

Start point

Hyper-parameters

0.050
3×
Failure 1 · Ripple pockets
From (4, 4) GD stops at L ≈ 1.66 in a tiny dimple. ∇L = 0 there, so θ − η·0 = θ. Frozen forever.
Failure 2 · Wrong valley
From (4.3, −4.3) it slides into local minimum B. GD only sees the slope under its feet, so it can’t tell B from the global minimum.
Failure 3 · η is a gamble
Raise η to jump over dimples, and it starts bouncing across valleys. Keep it small, and it gets trapped. One global η can’t do both.
Zoom in on one valley — steep in θ₂, flat in θ₁
Real valleys are rarely round. Here L = 0.02·θ₁² + 2·θ₂²: θ₂ is 100× more curved than θ₁.
Ravine · contour view · click to set start
0.450
θ₂ direction · steep
Big ∂L/∂θ₂ → big step → overshoots the valley floor → zig-zag. Push η past 0.5 and it explodes.
θ₁ direction · flat
Tiny ∂L/∂θ₁ → tiny step → crawls toward the minimum. It needs ~185 steps to arrive.
The wish list
Remember past directions so zig-zags cancel and consistent pushes add up → Momentum. Rescale each weight’s step by how big its gradients usually are → RMSProp. Both need a running average → EMA.
The key tool — Exponential Moving Average (EMA)
A running average that needs only one number of memory: the previous average.
data xt (noisy) EMA αt
0.20
Hover the chart to see one EMA update
From our notebook
αt = xt × β + αt−1 × (1 − β)
new average = a bit of the new sample + most of the old average
def calculate_ema(data, alpha): ema = [data[0]] # start with the first point for i in range(1, len(data)): ema.append(alpha * data[i] + (1 - alpha) * ema[-1]) return np.array(ema)
Small β → trusts history → smooth line, but it lags behind turns.
Large β → trusts the newest sample → follows every wiggle.
Unlike the SMA, no window of past values is stored: one multiply-add per step. That is why optimizers can afford one EMA per weight.
EMA inside optimizers — memory, notation, bias
Notebook convention (β weights the NEW sample)
αt = β·xt + (1 − β)·αt−1
Optimizer convention (β weights the HISTORY) — used from here on
vt = β·vt−1 + (1 − β)·gt  βopt = 1 − βnotebook
How much does each past gradient count?
0.90
vt = (1−β)·[ gt + β·gt−1 + β²·gt−2 + … ]
Bias: v₀ = 0 drags early values toward zero
True gradient is ≈ 1 every step. With β = 0.9, v₁ = 0.1 — ten times too small. Dividing by (1 − βt) fixes early steps and fades to ÷1 later. Adam uses this; plain Momentum and RMSProp usually don’t.
Momentum — step along the EMA of gradients
Instead of the raw slope, follow a running average of slopes: a heavy ball that remembers where it was heading.
1 · Velocity = EMA of gradients (per weight)
v₁ ← β·v₁ + (1−β)·∂L/∂θ₁    v₂ ← β·v₂ + (1−β)·∂L/∂θ₂
2 · Update with velocity instead of gradient
θ₁ ← θ₁ − η·v₁     θ₂ ← θ₂ − η·v₂
v1, v2 = 0.0, 0.0 # velocity starts at rest for step in range(num_steps): g1, g2 = grad_L(theta1, theta2) v1 = beta * v1 + (1 - beta) * g1 # EMA of gradients v2 = beta * v2 + (1 - beta) * g2 theta1 = theta1 - eta * v1 theta2 = theta2 - eta * v2
Why it helps. In θ₂ the gradient flips sign every step (+, −, +, −). The EMA averages those flips toward 0, so the every-step zig-zag becomes a few gentle swings that settle. In θ₁ the gradient points the same way every step, so the EMA keeps its full strength. Because v is smooth, η can be 3× larger than GD’s limit: the minimum is reached in ~40 steps instead of ~185.
PyTorch’s SGD(momentum=0.9) uses v = βv + g (no 1−β). Same idea, just scaled by 1/(1−β) = 10.
Same ravine · GD vs Momentum
0.450
1.5
0.900
Momentum on the jagged surface — rolling through the ripples
Same η, same start. The velocity carries θ across small dimples where the gradient briefly points backwards.
3D surface
Contour view · click to set start

Start point

Hyper-parameters

0.050
0.900
3×
✓ What Momentum fixes
From (4, 4) and (4, −1), GD freezes in a ripple pocket while Momentum coasts over it and reaches the global minimum. Try β = 0 — the blue path collapses onto the red one.
✗ What it doesn’t fix
From (3.5, 4) θ starts inside a dimple, so the velocity never builds up and both get stuck. Momentum’s step is still η × (average gradient): tiny gradients give tiny steps, with the same η for every weight. We need steps whose size doesn’t shrink with the gradient → RMSProp.
RMSProp — a separate step size for every weight
Keep an EMA of the squared gradient, then divide by its root. Big-gradient weights get smaller steps; small-gradient weights get bigger ones.
1 · EMA of squared gradients (per weight)
s₁ ← β·s₁ + (1−β)·(∂L/∂θ₁)²    s₂ ← β·s₂ + (1−β)·(∂L/∂θ₂)²
2 · Divide the gradient by its RMS
θ₁ ← θ₁ − η / (√s₁ + ε) · ∂L/∂θ₁    (same for θ₂)
s1, s2 = 0.0, 0.0 for step in range(num_steps): g1, g2 = grad_L(theta1, theta2) s1 = beta * s1 + (1 - beta) * g1**2 # EMA of g² s2 = beta * s2 + (1 - beta) * g2**2 theta1 -= eta * g1 / (np.sqrt(s1) + eps) theta2 -= eta * g2 / (np.sqrt(s2) + eps)
Why it helps. g² throws away the sign, so √s only tracks how big a weight’s gradients usually are. If that size stays roughly steady, √s ≈ |g| and g/√s ≈ +1 or −1: only the sign of g survives, and every weight moves about η per step.
• θ₂ (steep, zig-zags): g = +6, −6, +6… flips sign, but g² stays 36 → √s ≈ 6 → steps of ±η instead of ±2.7. Still alternating, but too small to overshoot.
• θ₁ (flat, no zig-zag): g ≈ −0.18 every step → √s ≈ 0.18 → steps of −η instead of −0.08. No sign flip needed: it just walks instead of crawling.
ε ≈ 1e-8 only prevents division by zero.
Same ravine · GD vs RMSProp
0.450
0.100
0.990

Effective learning rate (log scale)

Inside RMSProp · same formula, two weights · hover a chart
0.90
θ₁ · flat · g ≈ −0.18 every step (no zig-zag)
θ₂ · steep · g = +6, −6, +6… (zig-zag)
Gray dots: g. Orange band: ±√s (it ignores the sign). Bold line: g/√s, the step in units of η. The gradients are held fixed here to show the steady state; in the live race above s starts at 0, so the first steps are larger, and the ratio shrinks once the weight settles near 0.
RMSProp on the jagged surface — normalized steps escape the dimple
GD and Momentum take steps proportional to the gradient. RMSProp takes steps of about η per weight, whatever the gradient size.
3D surface
Contour view · click to set start

Start point

Hyper-parameters

0.100
0.990
3×
✓ What RMSProp fixes
From (3.5, 4), GD and Momentum are frozen in the dimple. RMSProp divides the gradient by its own RMS, so it keeps stepping about η per weight and climbs out toward the ★.
✗ What it doesn’t fix
From (4, −1), Momentum finds the ★ but RMSProp lands in local minimum B. With no velocity, RMSProp just follows the local slope. Momentum brings direction memory, RMSProp brings per-weight scale. Why not both?
Adam — Momentum + RMSProp + bias correction
Adaptive Moment Estimation: two EMAs per weight. Click a line to see where it comes from.
from Momentum 1st moment = EMA of g
m ← β₁·m + (1−β₁)·g
from RMSProp 2nd moment = EMA of g²
v ← β₂·v + (1−β₂)·g²
bias correction undo the “started at 0” shrinkage
m̂ = m / (1 − β₁t)    v̂ = v / (1 − β₂t)
update direction from m̂, scale from v̂
θ ← θ − η · m̂ / (√v̂ + ε)
m1 = m2 = v1 = v2 = 0.0 for t in range(1, num_steps + 1): g1, g2 = grad_L(theta1, theta2) m1 = b1*m1 + (1-b1)*g1; m2 = b1*m2 + (1-b1)*g2 v1 = b2*v1 + (1-b2)*g1**2; v2 = b2*v2 + (1-b2)*g2**2 m1h, m2h = m1/(1-b1**t), m2/(1-b1**t) v1h, v2h = v1/(1-b2**t), v2/(1-b2**t) theta1 -= eta * m1h / (np.sqrt(v1h) + eps) theta2 -= eta * m2h / (np.sqrt(v2h) + eps)
Click a formula on the left.
Scale check · constant gradient g, η = 0.1, β₁ = 0.9, β₂ = 0.999
1
Whether g = 0.001 or 1000, Adam’s step stays ≈ η. Plain GD’s step would be η·g.
The race — GD vs Momentum vs RMSProp vs Adam
Same surface, same start, same number of steps. Click the contour map anywhere to start a new race.
3D surface
Contour view · click to race from there
0.050
0.100
0.900
0.990
1.00
4×
From (4.5, −1) only Adam reaches the ★. GD and Momentum stall on a ripple (tiny gradient, tiny steps). RMSProp escapes but has no velocity, so it rolls into B. Adam has both: normalized steps to get moving, and momentum to keep going the right way. Honest caveat: from (4.3, −4.3) everyone ends in B. No optimizer guarantees the global minimum. They just make good minima much more likely.
Check yourself — three quick questions
1. In the optimizer convention vt = β·vt−1 + (1−β)·gt, roughly how many past gradients does β = 0.99 “remember”?
2. θ₂’s gradient flips sign every step (+8, −7, +6, −5 …) while θ₁’s is always ≈ +0.2. Which optimizer idea directly cancels the θ₂ zig-zag?
3. Fill in Adam’s update. What goes in the blank?   θ ← θ − η · m̂ / ( ____ + ε )
def adam_step(theta, g, m, v, t, eta=1e-3, b1=0.9, b2=0.999, eps=1e-8): m = b1 * m + (1 - b1) * g # Momentum part v = b2 * v + (1 - b2) * g**2 # RMSProp part m_hat = m / (1 - b1**t) # bias correction v_hat = v / (1 - b2**t) theta = theta - eta * m_hat / (np.sqrt(v_hat) + eps) return theta, m, v
Recap — one EMA, two EMAs
GD: θ − η·g
+
EMA of g → Momentum
+
EMA of g² → RMSProp
+
÷(1−βᵗ) → Adam
OptimizerExtra state per weightUpdate for θᵢFixesTypical defaults
GD / SGDnoneθᵢ −= η·gᵢ—η ≈ 0.01–0.1
Momentumvᵢ (EMA of g)θᵢ −= η·vᵢzig-zag, ripple pockets, slow valleysβ = 0.9
RMSPropsᵢ (EMA of g²)θᵢ −= η·gᵢ/(√sᵢ+ε)one η for all weights, tiny/huge gradientsβ = 0.9–0.99, η ≈ 1e-3
Adammᵢ, vᵢθᵢ −= η·m̂ᵢ/(√v̂ᵢ+ε)both of the above + cold startβ₁ 0.9, β₂ 0.999, η 1e-3
Click a row in the table.
import torch params = model.parameters() opt = torch.optim.SGD(params, lr=0.01) # vanilla opt = torch.optim.SGD(params, lr=0.01, momentum=0.9) # Momentum opt = torch.optim.RMSprop(params, lr=1e-3, alpha=0.99) # alpha = β opt = torch.optim.Adam(params, lr=1e-3, betas=(0.9, 0.999)) for x, y in loader: opt.zero_grad() loss_fn(model(x), y).backward() # fills every θ.grad opt.step() # applies the rule above