Skip to content
AI360XpertBeta
Playgrounds

Optimizers

SGD, momentum and Adam

An optimiser combines the current gradient with its own memory to choose an actual displacement.

Guided steps 1 / 4

Stage 1 of 4: Plain descent

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
Adjust parameters

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.

Shared round0Shared round: 0
Plain descent objective16.210000Plain descent objective: 16.210000

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|))?

Your prediction

The gradient points uphill locally. Plain descent subtracts the current gradient times the learning rate. These gradients are exact, not sampled.

  1. Own parameters
  2. Exact gradient
  3. Update own memory
  4. Actual displacement
  5. 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 f(x,y)=x2+y2f(x,y)=x^2+y^2, the gradient at (1,1)(1,1) is (2,2)(2,2). With rate η=0.1\eta=0.1, plain descent subtracts (0.2,0.2)(0.2,0.2) and lands at (0.8,0.8)(0.8,0.8). A second step subtracts (0.16,0.16)(0.16,0.16) and lands at (0.64,0.64)(0.64,0.64).

Our momentum convention stores a rate-scaled velocity uu, initially zero:

ut=β1ut−1+ηgt,θt=θt−1−ut.u_t=\beta_1u_{t-1}+\eta g_t,\qquad \theta_t=\theta_{t-1}-u_t.

Here θ=(x,y)\theta=(x,y) is the parameter pair, gg its gradient, and tt the method’s own update count. At β1=0.9\beta_1=0.9, the first velocity is (0.2,0.2)(0.2,0.2). The second is 0.9(0.2,0.2)+0.1(1.6,1.6)=(0.34,0.34)0.9(0.2,0.2)+0.1(1.6,1.6)=(0.34,0.34), landing at (0.46,0.46)(0.46,0.46). Its units are parameter displacement, not gradient.

Change only the rate after that first step. Existing velocity is retained: a second-step rate of 0.050.05 gives velocity (0.26,0.26)(0.26,0.26) and position (0.54,0.54)(0.54,0.54). 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 ss at zero and computes, coordinate by coordinate,

st=0.9st−1+0.1gt2,Δθt=−ηst+ϵgt.s_t=0.9s_{t-1}+0.1g_t^2,\qquad \Delta\theta_t=-\frac{\eta}{\sqrt{s_t+\epsilon}}g_t.

At a coordinate gradient of 22, the first ss is 0.40.4. A repeated gradient of 22 gives s=0.76s=0.76 next. The displayed factor η/s+ϵ\eta/\sqrt{s+\epsilon} 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 mm and second moment vv, initially zero. It increments its own tt before correcting their initial bias:

mt=β1mt−1+(1−β1)gt,vt=0.999vt−1+0.001gt2,m^t=mt/(1−β1t),v^t=vt/(1−0.999t),Δθt=−ηm^t/(v^t+ϵ).\begin{aligned} m_t&=\beta_1m_{t-1}+(1-\beta_1)g_t, & v_t&=0.999v_{t-1}+0.001g_t^2,\\ \hat m_t&=m_t/(1-\beta_1^t), & \hat v_t&=v_t/(1-0.999^t),\\ \Delta\theta_t&=-\eta\hat m_t/(\sqrt{\hat v_t}+\epsilon). \end{aligned}

Adam’s factor multiplies m^\hat m, 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 ϵ=10−8\epsilon=10^{-8} here, but its placement differs deliberately. RMSProp retains the convention s+ϵ\sqrt{s+\epsilon}; Adam follows its original Algorithm 1 with v^+ϵ\sqrt{\hat v}+\epsilon. At zero second moment those denominators are 10−410^{-4} and 10−810^{-8}. 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 f=x2+y2f=x^2+y^2, start (1,1)(1,1), rate 0.50.5, memory decay 0.90.9, multiplier one. Plain descent subtracts exactly (1,1)(1,1) and reaches objective zero. Adam’s first corrected direction is approximately a coordinate sign step, so it moves approximately (0.5,0.5)(0.5,0.5) 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 x2+20y2x^2+20y^2 has curvature 22 along x and 4040 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 (0,0)(0,0) on x2−y2x^2-y^2. 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 (3,0.5)(3,0.5); its curved, nonconvex geometry is not a generic flat plateau.

Each method stops independently on a non-finite computation or a coordinate magnitude above 10810^8, 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

  1. Plain descent

    The gradient points uphill locally. Plain descent subtracts the current gradient times the learning rate. These gradients are exact, not sampled.

  2. Carry momentum

    Velocity carries previous rate-scaled gradients. It can build motion along a valley, but can also overshoot and move uphill.

  3. 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.

  4. 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.