← All animations

⛰️ Gradient Descent

Almost every machine-learning model learns by minimising a loss — a number that says how wrong it currently is. Gradient descent is the recipe: stand on the loss surface, measure the slope, and take a small step downhill. Repeat. The ball below rolls in the direction of steepest descent −∇f until it settles in a valley. Scroll through to feel the learning-rate trade-off and see why a hill can trap you.

1. One dimension — step downhill by the slope

Here the loss is a curve and the ball has a single coordinate x. At each step we measure the slope f'(x) (the arrow), then move opposite it: x ← x − η·f'(x). The learning rate η sets the step size — and it is everything: too small and the ball crawls; too big and it overshoots and can fly off; just right and it converges smoothly to the minimum.

η = 0.18 slope −, stepping right

2. Two dimensions — local vs global minima

Real losses live in many dimensions. Here's a 2-D landscape drawn as a contour heatmap (dark = low loss). It has two valleys: a shallow local minimum and the deeper global minimum. Drag the white start dot anywhere, then press Drop — the ball follows −∇f straight into whichever basin it started nearest, and can get stuck in the shallow one. Turn on momentum and it builds speed downhill, sometimes rolling over the ridge to escape.

Drag the dot, then drop

3. Why this is how machines learn

The same loop, scaled to millions of dimensions, trains nearly every model.