Skip to content

Gradient Descent Playground

Drag the starting point, break the step size, and watch the path and loss curve respond

1. The playground

The yellow ring is the starting point; the filled yellow dot is the current point. Click or drag anywhere on the landscape to move the start, choose a bowl, a narrow valley, or a surface with two minima, then press Step or Play. The pink path is the walk. The orange arrow is the gradient at the current point; it points uphill, so the walk moves the other way.

The loss chart below the landscape has one point per step. If the loss climbs instead of falling, the step size is too large for that direction. Every number on this page is computed from the update rule while you interact; no path is pre-drawn.

The simulation is deliberately small: three smooth two-dimensional surfaces, full gradients, and a heavy-ball momentum rule. Real training uses noisy mini-batch gradients in many dimensions. The update rule is the same; the scale and the noise are not.

2. What to notice

  • Bowl. The path heads almost straight for the centre. With a small enough learning rate, the loss falls smoothly.
  • Narrow valley. The steep direction overshoots and flips sides while the gentle direction creeps. One learning rate is doing two very different jobs.
  • Two minima. The starting point decides which valley you reach. Start exactly on the saddle at (0, 0) and the gradient is zero, so the walk stays put.
  • Too large a step. The loss can rise or diverge. A small step is safe but can be too slow. Neither extreme is a universal answer.
  • Momentum. Raising μ lets the velocity build up along a consistent direction. It can speed up a narrow valley and it can also overshoot.

Changing the learning rate, momentum, terrain, or starting point restarts the walk. That is deliberate: a path with changing settings is hard to interpret.

3. The update rule

At each step, the playground evaluates the gradient at the current point and subtracts a multiple of it:

Plain gradient descent

w is the current point, α is the learning rate, and ∇f is the gradient of the selected landscape. The gradient points uphill, so subtracting it moves downhill.

With momentum, the playground keeps a velocity and carries part of it into the next step:

Momentum velocity
Momentum position

μ is the momentum coefficient, from 0 to 0.95 in the slider. At μ = 0 the update is plain gradient descent. A larger μ remembers more of the previous step. That can help in a narrow valley, where the useful direction is consistent even though the steep direction keeps flipping.

For a quadratic surface, a direction with curvature λ converges only when |1 − αλ| < 1. The largest curvature sets the limit:

Stability condition

The narrow valley has curvatures 2 and 8, so α must be below 0.25 for every direction to settle. The slider goes to 0.5 so you can watch what happens beyond the limit.

4. The three landscapes

Each landscape is smooth and has a known minimum. The formulas are shown so you can check the numbers by hand.

LandscapeLossMinimumStart
Bowlw₁² + w₂²(0, 0)(2.5, 2)
Narrow valleyw₁² + 4w₂²(0, 0)(2, 1)
Two minima0.25(w₁² − 1)² + w₂²(1, 0) and (−1, 0)(2.2, 0.8)

The two-minima surface also has a saddle at (0, 0). A saddle has zero gradient but is not a minimum. It is a useful reminder that “the slope is zero” does not always mean “we are done.”

Try the same learning rate on all three. The bowl is forgiving, the narrow valley is not, and the two-minima surface depends on where you start before the first step.

5. Related lesson

The lesson builds the same rule from a one-dimensional curve, with worked examples you can check by hand.

All Machine Learning lessons

Site soundtrack