Optimizers
SGD, momentum and Adam
An optimiser combines the current gradient with its own memory to choose an actual displacement.
Same objective, common start
Shared round 0. Ravine, multiplier 1. Plain descent: objective 16.210000, 0 updates, Ready.
Actual full-objective history
Shared round 0. Ravine, multiplier 1. Plain descent: objective 16.210000, 0 updates, Ready.
Exact full gradients, not stochastic SGD. Contours sample 61×61 points. Display only: trajectories and arrows are cropped at ±4.5; out-of-window markers are omitted, never projected. Independent pixel scales can distort angles. Loss uses sign(f) log₁₀(1+|f|); raw objectives are below and in inspection.
Cross: common start; reference minimum labelled by location (0, 0). Gradient diamond: proposed actual endpoint. Plain descent: 0 recorded points outside the window. All four methods compute even when hidden.
- Plain descent
Affects future updates; stored momentum is not rescaled.
Paused. Step advances each active method once.
Rate edits retain memories. Beta edits restart. RMSProp decay 0.9, Adam β₂ 0.999; ε=10⁻⁸. Numerical stop: non-finite computed values or |coordinate| > 100000000. Maximum 200 rounds. No clipping or projection of updates.
Plain descent: 0 updates / 0 update-gradient evaluations. Objective 16.210000. Ready.
Budgets count update gradients only. Current-point diagnostics, previews and contour samples are extra analytical evaluations, not training updates or wall-clock measurements.
Plain descent · Ready
Current θ: 0.100000, 0.900000
Current gradient g: 0.200000, 36.000000
Stored memory: none
Last actual Δ: 0.000000, 0.000000
Proposed actual Δ: -0.010000, -1.800000
Proposed length in parameter units: 1.800028
Proposed full objective: 16.208100
Move the common start. Where will the next step land?
After the next plain-descent update, will its full objective be lower, higher or unchanged (tolerance 10⁻¹² × max(1, |current objective|))?
The gradient points uphill locally. Plain descent subtracts the current gradient times the learning rate. These gradients are exact, not sampled.
- Own parameters
- Exact gradient
- Update own memory
- Actual displacement
- Measure full objective
Exact gradient
The analytical derivative of the full displayed objective at the current point.
Check this stage
Challenge
Start on the original ravine at (0.1, 0.9), multiplier one. Change the rate or memory and watch the actual objective.
Get any method to objective ≤ 0.05 within 20 of its updates, from the original start.
Challenge not yet solved.
Check your understanding
Give four optimisers the same starting point and inspect how each turns its gradient and memory into a different displacement.
Same hill, different steps
Move the common starting cross, then press Step. The circle, square, diamond and triangle mark actual parameter values. The arrow shows the inspected method’s proposed displacement. Change the inspected method to see why its next step differs. Straight trail segments connect discrete updates; they do not describe motion along the loss surface.
This lab uses exact analytical gradients of the full displayed objective. Plain descent is often exposed through an SGD API, but these updates are deterministic. Genuine stochastic gradient descent samples observations or objective terms to estimate a gradient. No sampling or arbitrary noise is added here, so there is no random seed tool. All four methods update from the first round, including those not yet revealed or currently hidden.
Each shared round permits one update and one update-gradient evaluation per active method, at that method’s own parameters. The visible counters freeze independently when a method stops. Contours, diagnostics and previews require extra analytical evaluations; those are disclosed separately from update budgets. This is a comparison of objective values and computations, not a wall-clock race.
What does momentum remember?
On the bowl , the gradient at is . With rate , plain descent subtracts and lands at . A second step subtracts and lands at .
Our momentum convention stores a rate-scaled velocity , initially zero:
Here is the parameter pair, its gradient, and the method’s own update count. At , the first velocity is . The second is , landing at . Its units are parameter displacement, not gradient.
Change only the rate after that first step. Existing velocity is retained: a second-step rate of gives velocity and position . A gradient-accumulator convention would respond differently to rate changes. Neither convention should be silently substituted for the other. On the ravine, consistent contributions can accumulate along the valley while alternating ones partly cancel. Stored velocity can also overshoot and raise the objective.
A multiplier is not a step length
Inspect RMSProp. It starts its second moment at zero and computes, coordinate by coordinate,
At a coordinate gradient of , the first is . A repeated gradient of gives next. The displayed factor still has to multiply the gradient. A large factor with a tiny gradient need not produce a large step. Recent large gradients can keep the denominator large even after the current gradient becomes small.
Adam stores both first moment and second moment , initially zero. It increments its own before correcting their initial bias:
Adam’s factor multiplies , not the current gradient. Inspect both corrected values and the two actual displacement coordinates. The second raw moment is a mean of squared gradients, not a centred variance.
Both methods use here, but its placement differs deliberately. RMSProp retains the convention ; Adam follows its original Algorithm 1 with . At zero second moment those denominators are and . They are not interchangeable near zero. Changing the objective multiplier changes losses and gradients together; epsilon means adaptive updates are not exactly invariant to that rescaling.
Try a case where Adam is not best
In Compare fairly, choose Bowl counterexample, then Step once. The visible settings are , start , rate , memory decay , multiplier one. Plain descent subtracts exactly and reaches objective zero. Adam’s first corrected direction is approximately a coordinate sign step, so it moves approximately and retains a positive objective. Both spent one update-gradient evaluation.
That result establishes the lower objective after one update for these settings. It does not establish which method is best after tuning, on a different landscape, under sampled gradients or on held-out data. Adaptive scaling can help with unequal coordinate magnitudes; memory can help consistent directions. Both introduce state and tuning choices, and can hurt on other examples. There is no universal winner.
The ravine has curvature along x and along y. Large plain steps can alternate across the steep direction. An alternating path can still contract. This lab supplies no universal learning-rate zone for momentum, RMSProp, Adam or Beale; a bound derived for plain descent on a constant quadratic cannot certify those other updates.
Break it, then recover the whole experiment
Use Try growing ravine steps and Step several times. Read each method’s actual loss and stop reason. Three consecutive material increases raise a measured growth warning; they do not halt the other methods or prove infinite divergence. A trajectory leaving the displayed square is only outside the viewport. Its raw values remain in inspection, with no clipping or projection in the computation.
Use Stationary saddle to start at on . Both gradient components and all initial memories are zero, so every method stays there. It is not a minimum: moving along y lowers the objective without bound. Small gradients can also occur in flat regions or near local minima. The Beale option is the standard sum of three squared polynomials, with a known zero at ; its curved, nonconvex geometry is not a generic flat plateau.
Each method stops independently on a non-finite computation or a coordinate magnitude above , a small gradient and negligible proposed movement, or its update budget. The known-optimum label additionally needs the stated objective tolerance. These are finite numerical observations, not convergence certificates. Run remaining budget computes bounded chunks up to 200 shared rounds and can be cancelled. Play is reader-started, finite and stoppable; reduced motion retains Step.
Back to working restores the complete preceding experiment: all memories, own timesteps, controls, history and selected round. Seeking pauses and restores a complete saved round, including its rate. A subsequent Step discards the former future and branches. Rate edits retain memories; beta edits restart because the fixed-beta bias correction assumes one constant decay. Objective, multiplier and starting-point edits restart all methods. Stage changes, free exploration, visibility and inspection changes pause and preserve the run. Reset controls restores defaults at the current stage and clears history. Replay restarts the common start with current settings, then plays.
Shared links reproduce the starting experiment, including stage, controls and common start. They open paused at round zero with zero memories and fresh history. They do not transfer the current run or prediction answers.
Sources and next experiment
The updates follow Adam Algorithm 1, Hinton’s RMSProp lecture, the D2L RMSProp convention and PyTorch’s momentum-convention comparison. D2L’s stochastic-descent chapter explains the sampling omitted here. The Beale reference identifies the standard objective. Return to gradient descent to inspect a single update before changing several mechanisms at once.
Stage recap
Plain descent
The gradient points uphill locally. Plain descent subtracts the current gradient times the learning rate. These gradients are exact, not sampled.
Carry momentum
Velocity carries previous rate-scaled gradients. It can build motion along a valley, but can also overshoot and move uphill.
Scale each coordinate
RMSProp divides the current gradient by its recent root squared magnitude. Adam divides a bias-corrected first moment by a corrected second-moment root.
Compare fairly
Compare the same starting point, objective and update budget. A favourable example for one method does not establish a universal winner.
Reference
- Full-gradient descent
- θₜ = θₜ₋₁ − ηgₜ. An SGD API does not make an analytical gradient stochastic.
- Momentum
- uₜ = β₁uₜ₋₁ + ηgₜ; θₜ = θₜ₋₁ − uₜ; u₀ = 0. Stored displacement retains its original rate contributions.
- RMSProp convention
- sₜ = 0.9sₜ₋₁ + 0.1gₜ²; θₜ = θₜ₋₁ − ηgₜ/√(sₜ+ε); s₀=0, ε=10⁻⁸. No bias correction.
- Adam Algorithm 1
- mₜ=β₁mₜ₋₁+(1−β₁)gₜ; vₜ=.999vₜ₋₁+.001gₜ²; m̂=m/(1−β₁ᵗ), v̂=v/(1−.999ᵗ); θₜ=θₜ₋₁−ηm̂/(√v̂+ε). Both moments start at zero, t starts at one.
- Limits
- At most 200 shared rounds. Per-method small-gradient/step, known-optimum tolerance and finite numerical limits are distinct from convergence proofs. No clipping or projection.
Break it on purpose
At rate 1 on the ravine, plain descent and momentum produce rapid loss growth. Their numerical limits do not stop the adaptive methods. Leaving the displayed square alone is not proof of divergence.