Zain Dana Harpernight school / F3

Gradients. Which way is downhill.

Training a model means walking downhill on a landscape you cannot see. Pylon does it in fog on the hill above the harbour, and then hands you her lantern.

The film

About 5:45, hosted by Pylon. It asks you two questions and waits for your answer before it shows you. The narrated cut runs 4:33, voiced by Pylon in a synthesized voice made from the author's own voice; the author approved these cuts for publishing. Captions are on by default. The live version under it draws the same film in your browser, waits for your answer at each question, and runs a little longer; the full words are also below.

Nothing loads until you press play.

The lab: Pylon in the fog

Pick a hill, click the map to place your start, choose a step and walk down by gradient descent, seen from above or as a relief. On the tilted bowl the edge is 2 / 4 = 0.5: try either side of it. Then turn the dial on the attention model's last matrix and step it with the model's own backward pass, on 64 sentences it never trained on. In the film's run, the dial's lowest point was 0.92, and the gradient's length fell from 1.87 before training to 0.12 after.

Everything is computed on your computer, in this page. Nothing is sent anywhere.

Everything the film says

Gradients

Gradients. Which way is downhill.

Tonight the hill is in fog. I can't see the bottom. All I can do is feel the ground under my flippers.

Feel the slope

Put one flipper a little to the east and feel how much higher the ground is there. That's the slope going east.

Do the same going north. Two slopes, written as a list: that list is the gradient.

The gradient points the steepest way up. So to go down, step the opposite way. Feel, step, feel again. That's gradient descent. Augustin Cauchy wrote it down in 1847.

How big a step

Here is a bowl, steep one way and gentle the other: the steep way curves 8 times as sharply as the gentle way.

Bigger steps should get me down faster, so let's double mine.

If I double my step, do I get down twice as fast? (the film pauses here)

Over the edge. On this bowl, any step longer than two divided by the steep way's curvature, 4, overshoots further each time. That edge is 0.5, and running the bowl puts it exactly there.

Half that step gets down smoothly. Just under the edge, I zigzag and crawl. Just over it, I zigzag outwards. At twice it, the loss passes a trillion in just 12 steps.

A long, narrow valley

Now a valley that is steep across and almost flat along. At the bottom, the steep way curves about 2,508 times as sharply as the flat way.

The steep way sets the largest safe step, so along the flat way I creep. It takes 3,925 steps to get the loss under a hundredth, and 15,068 to get it under a millionth.

Which valley

One more hill, with two valleys and a ridge between them. I start just left of the ridge, then just right of it.

Do the two starts end in the same valley? (the film pauses here)

Different valleys. The right one is not even the lower one. Gradient descent finds a bottom near where it starts, not the lowest bottom there is.

The model's own hill

The language model from the attention lesson has 20,864 numbers. Its loss is a hill with 20,864 directions, and its gradient is a list of 20,864 slopes.

Before training, the length of that list was 1.87. After training it is 0.12: the ground under the trained model is much flatter.

To see any of it, we cut a slice through the trained point along two random directions, the way Li and colleagues did in 2018. The loss is 0.70 in the middle and climbs to 13.99 at one corner.

One warning. In a hill with 20,864 directions, two random ones are almost at right angles to the steepest one. This slice shows that the model sits in a dip along these two directions. It cannot show the whole hill.

One number, by hand

So let's follow one direction we choose. Freeze every number, and add one dial that turns the model's last matrix up or down. At one, it's the model as trained.

On sentences it never trained on, the bottom of this one-way slice is at 0.92, not at one, though the loss there is only a hair lower. Turning the dial down makes the model a little less sure of itself, and new text likes that.

Start the dial near zero, feel the slope with the model's own arithmetic, and step. With small steps, forty of them walk the dial up to 0.84. A step too big for the steep ground near zero overshoots to well past one, and has to walk back.

What the fog hides

This model's training never feels the whole hill at once. Each step feels the slope on 16 short stretches of text, so every step is a little off.

And I haven't said how a model with 20,864 numbers works out 20,864 slopes at once without feeling each one separately. That's the bucket chain, next.

Goodnight

The gradient is the list of slopes. Step the other way, not too far, and you go down. Where you end up depends on where you start.

Next along the hill: blame, passed backwards. Goodnight.

Sources

Every claim in the film, with where it comes from and what that source does not prove. The numbers about the model are computed from its weights, which you can load and check: facts.json, model.json.

What this lesson leaves out

The bowl, the narrow valley and the two valleys are toy surfaces chosen to show one effect each. Real loss surfaces have thousands or billions of directions, and the slice of the model's loss in the film is one flat view through them along two random directions, which are almost at right angles to the steepest one.

The model's own training uses AdamW with a warmup and gradient clipping, on 16 stretches of text per step, not the plain fixed-step descent shown here. Better ways to step are lesson F5.

Before this: Matrices (F2). Next along the hill: Backpropagation (F4).