← All AI/ML articles

Gradient descent, visualised.

To fit a model, measure how wrong it is, find which direction makes it less wrong, and take a small step that way. Repeat. Watch that happen on a real loss surface, then break it with a bad learning rate.

1. Measure the slope2. Step downhill3. Repeat until it settles
Loss of a straight-line fit y = w·x + b to 60 noisy points. Bright = low loss. Click the map to set the starting point. Yellow path = the optimiser, cyan diamond = the exact least-squares optimum.
Step0
Loss0
‖gradient‖0
Behind the scenes · 1

Slope, step, repeat

The gradient of the loss points in the direction of steepest increase. Gradient descent moves the parameters the opposite way by a distance set by the learning rate η. The loss curve shows how far above the best possible loss the optimiser is after each step (log scale); a grey dashed line shows plain batch gradient descent with the same learning rate for comparison.


Cyan = current optimiser. Grey dashed = batch gradient descent, same start and learning rate. Excess loss = loss minus the least-squares minimum.
Behind the scenes · 2

The learning rate decides everything

Too small and progress crawls. Too large and each step overshoots, so the loss grows instead of shrinking. For this kind of loss (a quadratic bowl) there is an exact limit: batch gradient descent converges only if η < 2 / λmax, where λmax is the largest curvature. Click the chart to set η.


Excess loss after 100 batch steps from your start, for each η. Dashed lines: stability limit 2/λmax (right) and fastest rate 2/(λmax + λmin). Values above the top are clipped.
Behind the scenes · 3

Scaling the inputs reshapes the bowl

When x is far from zero, slope and intercept are strongly coupled and the loss becomes a long, narrow valley: steep across, shallow along. The learning rate must be small enough for the steep direction, so progress along the valley is slow. Tick Standardise x in the controls to centre and rescale x and the valley turns into a round bowl. The ratio κ = λmax / λmin measures how elongated it is.


Behind the scenes · 4

Batch, mini-batch and stochastic

Batch gradient descent uses all 60 points for every step. Mini-batch SGD uses a random subset (batch size 1 is classic SGD): each step is cheaper and unbiased on average, but noisy. The noise shrinks as the batch grows. With a constant learning rate, SGD keeps bouncing around the minimum instead of settling exactly. Try batch size 1, then 60.


Behind the scenes · 4b

The race: batch vs mini-batch vs SGD

Same start, same learning rate, same amount of data seen. One epoch = one pass over all 60 points. Batch makes 1 update per epoch, mini-batch (10) makes 6, SGD makes 60. More updates per epoch means faster early progress, but the smaller the batch, the higher the noise floor. Change η in the controls above and watch the curves.


Amber = batch (60), cyan = mini-batch (10), pink = SGD (1). Excess loss on a log scale. A line that stops early diverged.
Behind the scenes · 5

Beyond bowls: variants and landscapes

Real loss surfaces are rarely round bowls. Here three optimisers start from the same point on three test functions: plain gradient descent (amber), momentum with β = 0.9 (pink), and Adam (cyan). By default all three start with the same nominal learning rate, but each optimiser responds to it differently. This is a comparison at a shared setting, not a guarantee of equal tuning. Switch Learning rates to “Tuned per optimiser” to give each one a rate that is stable for the chosen function. Click the map to move the start.


Dark = high, bright = low. Amber = gradient descent, pink = momentum, cyan = Adam. White dots = known minima.

The mathematics

The mathematics behind gradient descent

Numbers in A to D come from the linear-regression playground and update live.

A. The update rule

θt+1 = θt − η ∇L(θt)

B. The gradient and Hessian of the squared-error loss

L(w, b) = (1/n) Σ (w xi + b − yi)²   ,   ∇L = (2/n) Σ (w xi + b − yi) (xi, 1)   ,   H = 2 [[mean(x²), mean(x)], [mean(x), 1]]

C. Convergence on a quadratic

converges iff 0 < η < 2/λmax   ,   best η = 2/(λmax + λmin)   ,   error factor per step = (κ − 1)/(κ + 1)

D. Stochastic gradients

E[gbatch] = ∇L   ,   E‖gbatch − ∇L‖² = (σ²g / b) · (n − b)/(n − 1)  (sampling without replacement)

Here σ²g is the total variance of the per-point gradients (the trace of their covariance), b the batch size and n = 60.


E. Momentum and Adam

Momentum: v ← βv − ηg θ ← θ + v Adam: m ← β1m + (1−β1)g s ← β2s + (1−β2)g² m̂ = m/(1−β1t) ŝ = s/(1−β2t) θ ← θ − η m̂ / (√ŝ + ε)
Reading it
  • Momentum accumulates past gradients, which can speed travel along a valley but can overshoot.
  • Adam rescales each coordinate by a running estimate of its gradient size. It is popular, not universally better.
  • On non-convex surfaces, where you end up depends on the start, and flat regions or saddle points can slow progress.
Experiment

Break it on purpose

Too large a step

Set η just above the stability limit 2/λmax and watch the loss grow.

Same η, scaled inputs

Compare how many steps batch gradient descent needs with and without standardising x, using the best learning rate each time.

Noisy steps

Batch size 1 versus 60 with the same learning rate: how close to the optimum does each get?

Check yourself

Three quick questions

Key takeaways

  • The rule: move parameters against the gradient, scaled by a learning rate.
  • Learning rate: too small is slow, too large diverges. On a quadratic loss the limit is exactly 2/λmax.
  • Conditioning: elongated valleys slow gradient descent down. Standardising inputs often helps a lot.
  • SGD and mini-batches trade exactness for cost: unbiased but noisy gradients, with noise falling as the batch grows.
  • Beyond convex: the path and the minimum reached can depend on the start, and the best optimiser and learning rate depend on the problem, so treat them as settings to tune.
← Back to all articles