Gradient descent

AI from scratch · 3 of a series

Gradient descent: lesson transcript

1. Standing in fog

Last time you could see the whole map and just point at the valley floor. A real model has millions of dials, so the map cannot be drawn. You are standing somewhere on the bowl, in fog. All you can feel is the slope under your feet.

2. Feel the slope

Nudge each dial a hair and see whether the error rises or falls. Those two numbers make an arrow pointing straight uphill: the gradient. You want the opposite direction, so flip it. That is the only information you need.

3. Take a step, then another

Step a little way downhill. The fog moves with you, so feel the slope again and step again. Each step has the same recipe: current dials, minus the slope times a small number η that sets how far you dare to move.

4. Let it roll

Lift the fog and watch the whole walk. The steps are big where the bowl is steep and shrink as the floor flattens, so the walker settles by itself. On the right, the loss after each step: this is the training curve, the graph every AI engineer stares at.

5. How far do you dare to step?

η is the learning rate. Small and the walk crawls, spending its whole budget of steps getting nowhere. Near the limit it overshoots the valley floor and lands on the far wall, again and again. Past the limit each step lands higher than the last: the loss explodes.

6. Why it zigzags

Each step goes straight downhill, which means straight across the contour lines. In a long narrow valley "straight downhill" mostly means "into the opposite wall", so the walker bounces side to side while inching along the floor. Round bowls are easy; stretched ones are slow.

7. Name it: gradient descent

Feel the slope, step against it, repeat. That loop is *gradient descent*, and with small variations it is how every neural network you have heard of was trained. Two dials or two billion, the recipe on the right does not change.

8. Did it land?

A training curve that was falling nicely starts climbing after you double η.

Check your understanding

You double the learning rate and the loss now rises every step instead of falling. What is happening on the map?

The slope still points the right way; the step is just too long. It carries the walker past the floor and up the far side to a point higher than where it started, and the next step does the same in reverse.