Skip to content

Inference EngineeringInteractive lab

Gradient descent on a surface

Change the learning rate and watch the run diverge

Drop a starting point on a loss surface and step downhill with plain SGD, momentum, or Adam, with the trajectory drawn over the contours.

What this teaches

  • Learning rate
  • Momentum
  • Why Adam adapts per parameter

The surface and the step

f = x^2 + 12y^2

Condition number 12. The gradient points across the valley rather than along it, which is what momentum fixes and what makes a single learning rate a compromise.

This surface is a quadratic, so the stability threshold is exact rather than a rule of thumb: plain gradient descent converges while lr < 2/L, and here L = 24, so the boundary is 0.0833. You are at 0.0200, below it. SGD ended at a loss of 0.0193. Walk the slider across 0.0833 and watch the prediction and the behaviour change together.

After 60 steps

from the same starting point
1.21e-5
Best final loss (Momentum)
0.0193
SGD
1.21e-5
Momentum
0.3378
Adam

At a learning rate of 0.0200 over 60 steps on the Elliptical bowl (ill-conditioned), the best final loss is 1.21e-5 from Momentum.

All three start at the same point and see the same gradients. Where they end up differs only in how they turn a gradient into a step, which is the entire content of an optimiser.

The path each one took

darker is lower loss
  • SGD
  • Momentum
  • Adam

Why Adam adapts per parameter

its step in x and in y, separately
Adam's position, gradient and effective per-coordinate step by iteration.
Stepxylossgrad xgrad ystep in xstep in y
0-1.60000.900012.28000000
5-1.50040.80059.9398-3.040519.6873-0.01990.0198
10-1.40140.70227.8806-2.842217.3203-0.01970.0195
15-1.30360.60656.1132-2.646115.0083-0.01940.0189
20-1.20780.51474.6373-2.453612.7835-0.01900.0180
25-1.11440.42813.4411-2.265710.6784-0.01850.0168
30-1.02390.34802.5018-2.08348.7233-0.01780.0154
35-0.93680.27551.7884-1.90786.9448-0.01710.0139
40-0.85340.21141.2643-1.73945.3637-0.01640.0121
45-0.77400.15610.8913-1.57903.9934-0.01550.0103
50-0.69880.10980.6329-1.42702.8392-0.01470.0085
55-0.62810.07230.4571-1.28371.8977-0.01380.0068
60-0.56180.04300.3378-1.14941.1576-0.01290.0052

The last two columns are the whole point. SGD multiplies both gradients by the same learning rate, so a coordinate with a gradient twelve times larger gets a step twelve times larger. Adam divides each coordinate by the square root of its own running squared gradient, so the two steps come out close to the same size whatever the gradients are. That is what "adapts per parameter" means, and it is these two numbers being different from each other in a way SGD's never are.

What is real here, and what is standing in

Real: every gradient is the analytic derivative of the formula shown, not a finite difference. All three update rules are the textbook ones, Adam's bias correction included and visible in the table. The stability bound on the quadratic is exact, and the divergence step beside it is measured by running until the loss stops being finite.

Standing in, and this is the big one: there is no data here. This is full-batch descent on a closed-form function, so the gradient is exact every time and there is no noise at all - the "S" in SGD is missing, and half of what momentum is for in practice is smoothing exactly that noise.

Also standing in: two dimensions. A real network has millions of parameters and a surface nobody can draw, and the mechanics generalise while the geometry does not - in high dimensions saddle points vastly outnumber local minima, which is the most important fact about a real loss landscape and the one a picture like this cannot show you. Momentum here is heavy ball rather than Nesterov, and there is no weight decay, no learning-rate schedule and no gradient clipping.

Everything on this page runs in your browser. Nothing you type is sent anywhere, there is no account, and it keeps working offline.