Part III ยท Ch. 13 โ€” Rolling Downhill

Part III ยท Chapter 13 of 20

Rolling Downhill

Gradient descent: learning as finding low ground


The mountain is real now

Chapter 5 taught a walk on a made-up hill. Chapter 12 quietly built a real one: every complete setting of the model's dials $\theta$ โ€” theta, the whole collection of weights from Chapter 3 โ€” is a place, and every place has a height, the exam score $L(\theta)$. Nobody drew this landscape and nobody can see it whole; it exists the way a mountain range exists at night. Training is the art of walking it downhill by feel.

The object has a name: the loss landscape. One dial gives you a curve. Two dials give you a rolling surface over a plane. Billions of dials give you a landscape in billions of dimensions that no one will ever picture โ€” and, as Chapter 5 promised, the arithmetic does not care how many dimensions there are.

Here is the plan. We will walk a one-dial landscape end to end by hand, meet the terrain features that real training fights โ€” valleys inside valleys, ridges, spikes โ€” and close with the analogy this whole site was secretly built around. Because finding low ground on a rugged landscape is, more or less, what my entire field does for a living.

A one-dial mountain

Let me be honest about the data before we start: there is none. To keep every number checkable, our machine has a single dial $w$ and a loss curve I made up for teaching โ€” small enough to hand-check, lumpy enough to be honest:

$$ L(w) = 0.1w^4 - w^2 + 0.2w + 3 $$

In words: the loss at dial-setting $w$. Plug in a number, get a height.

We also need the tilt of the ground โ€” the slope of that curve at each $w$. Chapter 5 gave us the one pattern we need: $x^2$ has slope $2x$. The same shrinking-secant game gives $x^4$ a slope of $4x^3$, and a straight-line term like $0.2w$ has the constant slope $0.2$ (a line's tilt never changes). Two bits of bookkeeping let us put the pieces together, and both are exactly what you would guess.

First, slopes add. A curve built by adding pieces has, at every point, the sum of the pieces' slopes โ€” each term tilts the total on its own, and they do not interfere. Second, a constant out front scales the slope. The term $0.1w^4$ climbs one-tenth as fast as $w^4$ does, so its slope is one-tenth as big: $0.1 \times 4w^3 = 0.4w^3$. The minus sign on $-w^2$ is that same rule with a constant of $-1$, so that term's slope is $-2w$. And the bare $+3$ lifts the whole curve upward without tilting it anywhere, so it contributes no slope at all. Add the four pieces:

$$ \frac{dL}{dw} = 0.4w^3 - 2w + 0.2 $$

In words: the tilt of the ground at dial-setting $w$ โ€” positive means the loss climbs to the right, negative means it falls to the right.

Do not take that on faith โ€” check it the way Chapter 5 taught. At $w = 3$ the formula claims a tilt of $0.4 \cdot 27 - 6 + 0.2 = 5.0$. Now nudge the dial by hand: $L(3) = 2.70$ and $L(3.01) = 2.7504$, so the ground rose $0.0504$ over a run of $0.01$, a measured tilt of about $5.04$. Shrink the nudge further and that reading walks in toward $5.0$ exactly. The formula told the truth.

Now look at the terrain before we take a single step โ€” picture before symbols.

The red loss curve L(w)=0.1wโดโˆ’wยฒ+0.2w+3 over w from โˆ’3.4 to 3.4. It has a deep valley near w=โˆ’2.28 at height 0.05, a shallow valley near w=2.18 at height 0.94, and a ridge crest near w=0.1 at height 3.01. A blue walker starts at w=3 and steps down into the right valley; an amber walker starts at w=โˆ’1 and slides into the deep left valley.
One dial, two fates. The made-up loss curve $L(w) = 0.1w^4 - w^2 + 0.2w + 3$ has a deep valley ($L \approx 0.05$), a shallow one ($L \approx 0.94$), and a ridge between. The blue walker starts at $w = 3$ and settles in the shallow valley โ€” from there, every downhill road leads right. The amber walker starts at $w = -1$ and glides into the deep one. Same rule, same step size; only the starting square differs.

The figure shows two valleys. A shallower one on the right, its floor near $w \approx 2.18$ at height $L \approx 0.94$. A deeper one on the left, its floor near $w \approx -2.28$ at height $L \approx 0.05$. Between them sits a ridge crest near $w \approx 0.10$ at height $L \approx 3.01$. You can verify any single point by plugging in โ€” the crest, for instance: $L(0.1) = 0.00001 - 0.01 + 0.02 + 3 = 3.01$.

The two valleys deserve their names. The deep one is the global minimum โ€” the best the whole landscape has to offer. The shallow one is a local minimum โ€” locally a success, globally a consolation prize. Chapter 5's parabola had one bottom, so the distinction never came up. Real landscapes are made of little else.

Walking it

The walking rule is Chapter 5's, with fresh letters โ€” same rule, real mountain:

$$ w_{\text{new}} = w - \eta \, \frac{dL}{dw} $$

In words: nudge the dial a small step against the tilt of the loss. Here $\eta$ โ€” eta, the learning rate โ€” sets how big that step is.

Let us walk, starting on the right-hand slope at $w = 3$, with $\eta = 0.1$. Step 1, in full:

Worked example

Step 1, from $w = 3$:

  • slope $= 0.4 \cdot 27 - 6 + 0.2 = 10.8 - 6 + 0.2 = 5.0$
  • step $= -0.1 \times 5.0 = -0.5$
  • position: $3 \to 2.5$; loss: $L = 2.70 \to 1.16$

Step 2, from $w = 2.5$:

  • slope $= 0.4 \cdot 15.625 - 5 + 0.2 = 6.25 - 5 + 0.2 = 1.45$
  • step $= -0.1 \times 1.45 = -0.145$
  • position: $2.5 \to 2.355$; loss: $L = 1.16 \to 1.00$

The pattern is set, so we summarize the walk in a table:

step$w$slope$L$
0$3.00$$5.00$$2.70$
1$2.50$$1.45$$1.16$
2$2.36$$0.71$$1.00$
3$2.28$$0.40$$0.96$
4$2.24$$0.23$$0.95$

In words: each step is smaller than the last, because the ground flattens as we near the floor. The walk settles onto the valley floor at $w \approx 2.18$, $L \approx 0.94$.

Success โ€” the walk found a valley and settled in. And now the rug: our walker is standing in the wrong valley. Across the ridge, the floor at $w \approx -2.28$ scores $0.05$, nearly twenty times better, and our walker will never find it. Why? Gradient descent only ever steps downhill, and from $w = 3$ every downhill road leads right. Worse: the walker is already below the ridge crest โ€” its loss $0.94$ is under the crest's $3.01$ โ€” so no sequence of honest downhill steps can ever climb back over. The fog never lifts, as Chapter 5 warned, and now the fog is hiding something that matters.

It could have gone differently. Start the identical walk at $w = -1$ instead. One step, in full: the slope there is $0.4 \cdot (-1) - 2 \cdot (-1) + 0.2 = -0.4 + 2 + 0.2 = 1.8$; the step is $-0.1 \times 1.8 = -0.18$; so $w \to -1.18$ โ€” moving left, away from zero, downhill into the deep valley. Summarized, it glides to $w \approx -2.28$, $L \approx 0.05$. Same rule, same $\eta$, same landscape โ€” a different starting square, and a twenty-fold better ending. Where you start matters. And real training starts $\theta$ at random values, which is worth one raised eyebrow before the next section explains why it is not fatal.

The learning-rate craft, on real terrain

Chapter 5 showed the $\eta$ pathologies on a parabola, where the worst that could happen was bouncing in place. On lumpy terrain the stakes climb โ€” and one of the outcomes genuinely surprises. Let us walk the gallery, all starts from $w = 3$; the widget below lets you replay every one.

Three panels of loss versus step. Left, ฮท=0.01: a red curve creeping down over 100 steps, still above 0.94, labeled 'still walking at step 100'. Middle, ฮท=0.1: a red curve dropping to 0.94 by about step 20, with a dashed line far below at 0.05 labeled 'the deeper valley it never found'. Right, ฮท=1.0: a jagged red curve spiking up and down then shooting off the top of the panel, labeled 'off the map by step 9'.
Three walks down the same mountain, differing only in $\eta$. Too small crawls โ€” real runs pay for every step. Right-sized glides in, but settles for the valley it can reach. Too big turns the loss into a seismograph: spikes, lucky dips, then a step so large the walk leaves the landscape entirely. When you see a spiky training curve in Chapter 15, this is what you are looking at.

$\eta = 0.01$, the crawl. After one hundred steps the walker sits at $w \approx 2.20$ โ€” still edging toward the local floor. Nothing failed. But at scale this is the pathology that costs the most money: Chapter 15's runs price every step in GPU-time, and a crawl finishes the walk long after the budget is gone.

$\eta = 1.0$, the rampage. The walker ricochets between the slopes โ€” $w$: $3 \to -2.0 \to -3.0 \to 1.6 \to 2.96 \to -1.71 \to -3.33 \to 4.6 \to -25.4 \to \dots$ โ€” and explodes off the landscape, the loss lurching $2.7 \to 0.2 \to 1.5 \to 1.4 \to 2.5 \to 0.6 \to 3.6 \to 27.6 \to$ thousands. Two lessons inside one disaster. First, the loss does not fall steadily โ€” it spikes up and down, and when Chapter 15 shows real loss curves with spikes, this is what a spike is. Second, one oversized step can undo a hundred good ones.

$\eta = 0.6$, the shock. The first step is $-0.6 \times 5.0 = -3.0$, vaulting the walker from $w = 3$ clear over the ridge to $w = 0.0$ โ€” and from there it rolls left: $0 \to -0.12 \to -0.38 \to -0.95 \to -2.0 \to \dots$ into the deep valley our careful $\eta = 0.1$ walker could never reach. A reckless step size escaped the trap. But watch the ending: near $w \approx -2.28$ the deep valley's walls are too steep for a step that big, and the walker bounces around the floor without ever settling. Big steps explore; small steps land; no single $\eta$ does both.

That last sentence is the whole point, so let us name the fix. Real training runs change $\eta$ over time โ€” a learning-rate schedule. Start careful, stride boldly through the early terrain, then shrink the steps so the walk can settle. You are about to perform this exact maneuver by hand in the widget below: vault with $0.6$, then land with $0.1$.

Two dials, then billions

Lift to two dials to reactivate Chapter 5's gradient. Now the loss is $L(w_1, w_2)$, and the tilt is two readings stacked into one arrow โ€” the gradient $\nabla L$. The walk subtracts $\eta$ times that arrow, component by component. No new arithmetic โ€” this is exactly Chapter 5's $(1,1) \to (0.5, 0)$ move, on lumpier ground.

A bird's-eye contour map of the two-dial loss landscape. Two nested-ring basins sit either side of a central ridge: a deep basin on the left labeled Lโ‰ˆ0.05 and a shallow basin on the right labeled Lโ‰ˆ0.94. A blue trajectory from (3,1.5) curves into the right basin crossing the contour rings roughly perpendicularly; an amber trajectory from (โˆ’1,โˆ’1.5) curves into the left basin.
The same mountain seen from above, with a second dial. Contour rings mark equal loss; the gradient walk cuts across them, always perpendicular to the ring it is crossing (Chapter 5's compass). Two walkers, two starting squares, two different basins โ€” with billions of dials the picture is unimaginable, but the per-dial arithmetic never changes.

Now the honest jump. A real model has billions of dials, so the walk happens in a space with billions of directions. Two things are worth knowing, and I will hedge both as what researchers observe, not as theorems. First: in enormous dimensions, dead-stop traps are rarer than the two-dimensional cartoons suggest. At most flat-ish points, some of the billions of directions still tilt downhill โ€” such a point is a saddle, not a true bottom โ€” so the walk usually finds a way to keep leaking downhill. Second: rugged landscapes hold astronomically many valleys that are all roughly equally good, so training twice from different random starts lands in different valleys of similar quality. That is why "a good valley, not the best one" is a livable creed rather than a disappointment.

$$ \theta_{\text{new}} = \theta - \eta \, \nabla L $$

In words: nudge every one of the billions of dials a small step against its own slope of the loss โ€” the same rule, run per dial, all at once.

And one deflating sentence to close the loop with Chapter 5: through all of this, the algorithm never got smarter. It is still exactly $w - \eta \cdot \text{slope}$, run per dial, in fog. Everything this chapter added was terrain.

The annealing mountain

Now the analogy this site was built around, and it gets a full section because it is genuinely the same mathematics. Materials science has an energy landscape. Every arrangement of the atoms in a solid has an energy; nature relentlessly nudges systems toward lower-energy arrangements; the map from arrangement to energy is a loss landscape with different labels. Metal cooling after a weld, water freezing into ice, a jostled pile of gravel settling โ€” all of them are walkers on rugged terrain, seeking low ground.

The local-minimum story is a materials engineer's daily bread, and it is told most cleanly with glass and quartz. Cool molten silica fast and the atoms get frozen mid-jumble: glass โ€” a material permanently stuck in a local minimum, beautiful and slightly wrong. Cool the same silica slowly and the atoms have time to find the deep, ordered arrangement: quartz crystal, the global basin. Identical ingredients; the difference is entirely the schedule of the cooling. Metallurgists and glassmakers have a word for that slow, staged cooling: annealing.

Two panels. Left, 'quenched โ€” cooled fast': blue circles in a jumbled arrangement labeled 'glass โ€” atoms frozen mid-jumble', with a small red two-valley energy curve below showing a blue ball stuck in the shallow valley, captioned 'stuck in a local minimum'. Right, 'annealed โ€” cooled slowly': the same blue circles in a neat hexagonal lattice labeled 'crystal โ€” the deep, ordered arrangement', with the identical energy curve showing the ball in the deep valley, captioned 'found the global minimum'. Between them: 'same atoms ยท different schedule'.
The materials-science version of this chapter. Cool molten silica fast and the atoms freeze mid-jumble: glass, a material stuck forever in a local minimum. Cool it slowly โ€” anneal it โ€” and the atoms find the deep ordered valley: quartz. Identical ingredients; the schedule decides the fate. Learning-rate schedules are cooling schedules for models.

The map is term for term: atomic arrangement $\leftrightarrow$ the dial settings $\theta$; energy $\leftrightarrow$ the loss $L(\theta)$; thermal jiggling $\leftrightarrow$ big early steps and noise; the cooling schedule $\leftrightarrow$ the learning-rate schedule; glass $\leftrightarrow$ a run that quenched into a poor valley; crystal $\leftrightarrow$ a run that annealed into a deep one. The $\eta = 0.6$-then-$0.1$ maneuver from the last section is annealing, performed by hand.

One forward seed, one sentence: Chapter 15 adds a genuine source of jiggle to the walk โ€” the gradient there is measured on random handfuls of data, so it shivers โ€” and that shiver, like thermal noise, helps shake walkers out of shallow dips. The full story of that noise belongs to the training run.

See it move

The descent-stepper returns from Chapter 5, on new terrain. The curve is now a red loss landscape with two valleys, and the walker is a dial-setting $w$. Everything this chapter claimed is replayable: drag the walker, pick a learning rate, and press Step or Auto ▸. Switching $\eta$ mid-run is allowed and encouraged โ€” it is the annealing exercise.

Why the LLM cares

State the equation of the whole enterprise once more, now with every symbol owned:

$$ \theta_{\text{new}} = \theta - \eta \, \nabla L $$

In words: step every dial against its slope of the loss. Chapter 12 built $L$; this chapter walked it.

The $\eta$ craft โ€” schedules, spikes, escapes โ€” is a real job description on real training teams, because at scale a bad learning-rate day costs more than a car. But this chapter has been living on an enormous IOU. Every step needs $\nabla L$: the tilt of the loss with respect to every one of billions of dials, refreshed at every single step. Measuring one tilt the shrinking-secant way means re-running the whole model. Measuring billions that way means billions of re-runs per step. At that price, deep learning would not exist.

There is a trick โ€” one of the great tricks in the history of computing โ€” that gets every one of those tilts for roughly the cost of two runs. It even has a wonderfully accusatory name. And you now hold the full loop conceptually: score the guess (Chapter 12), feel the tilt (next chapter), step downhill (this one), repeat. Everything after that is scale.

What you now know

  • The loss landscape is real: every full setting of the dials $\theta$ is a place, $L(\theta)$ is its height, and training is walking it downhill in fog โ€” $w_{\text{new}} = w - \eta\,\frac{dL}{dw}$, run per dial.
  • Our one-dial mountain $L(w) = 0.1w^4 - w^2 + 0.2w + 3$ has two valleys: from $w = 3$ the walk settles into the shallow one ($L \approx 0.94$), and from $w = -1$ it finds the deep one ($L \approx 0.05$) โ€” the starting square picks the valley.
  • Gradient descent never climbs, so a walker below the ridge can never cross it: training finds a good valley, not a certified best one.
  • On lumpy terrain the $\eta$ pathologies get expensive: $0.01$ crawls past the budget, $1.0$ turns the loss into spikes and then explodes โ€” and $0.6$ vaulted the ridge into the deep valley but could never settle there.
  • No single step size both explores and lands, which is why real runs use learning-rate schedules โ€” bold early, small late โ€” metallurgy's annealing, performed on a model.
  • In billions of dimensions the cartoons soften: flat spots are usually saddles with some direction still downhill, and many valleys are roughly equally good โ€” different random starts land in different, similar-quality valleys.

Where we're headed

One promise is still unpaid, and it is enormous. Every step of the walk consumed a slope reading โ€” and a real model needs billions of them, one per dial, recomputed at every single step. Measure them the shrinking-secant way and each reading costs a full run of the model: billions of runs per step, forever. At that price, none of this would exist. The actual trick gets every one of those billions of slopes for roughly the cost of two runs โ€” one pass forward through the machine, one pass backward carrying blame. It is the reason deep learning is possible at all, and you already own every mathematical part of it. Next chapter: backpropagation.