svish.dev

← All lessons · AI / ML · 10 min

Gradient descent you can see

The one algorithm that trains almost every neural network — rolled down a hill you can drag around.

Training a model means finding parameters that make a loss small. Gradient descent does it the way you’d find the bottom of a foggy valley: feel which way is downhill, take a step, repeat.

x ← x − lr · slope(x)

slope is the derivative of the loss; lr is the learning rate, the step size. That’s the whole algorithm. A neural network with a billion parameters does the same thing, just in a billion dimensions at once.

Play first (widget at the top of this page)

The widget above shows a loss curve with two valleys. Pick a starting point and a learning rate, then run it.

Three things to discover

  1. Too small a learning rate — it gets there, but takes forever.
  2. Too large — it overshoots the valley and bounces up the far side. At lr = 1.0 starting from 1.5 it never settles, hopping between the valleys (other starts can even jump into the wrong valley); push it to about 1.7 and it flies off the chart (the loss explodes — divergence).
  3. Starting point decides the answer — start at the right and you settle in the shallower valley; start on the left and you find the deeper one. Gradient descent only sees the slope under its feet. This is a local minimum, and it’s why initialisation and tricks like momentum exist.

From here to deep learning

  • Real losses average over a dataset; computing the slope on the whole set is slow, so we estimate it on small random batches — stochastic gradient descent.
  • The slope for each parameter comes from backpropagation, the chain rule applied layer by layer. You’ll write it by hand in the next lesson.
  • Optimisers like Adam keep a running average of past slopes (momentum) and rescale the step per parameter. Same idea, smarter steps.

Takeaways

  • Learning is descent on a loss surface.
  • Learning rate is the first knob to check when training misbehaves.
  • You only ever see the local slope, so where you start matters.