Skip to content

Lesson: The cost landscape

Last lesson ended with a clean goal and an awkward silence. We said learning is just minimizing the cost, finding the knob settings that make the wrongness number small. Clean. But then: we have about 13,000 knobs, and the cost is some complicated function of all of them. Standing in that space, which way do you even step? Turning a knob at random and hoping is hopeless.

This lesson hands you a way to see the problem. We are going to turn that abstract space of knob settings into a landscape, an actual terrain of hills and valleys, and once it has a shape, “make the cost small” becomes something almost physical: walk downhill.

Here is the picture. Every possible setting of the network’s weights and biases is a single point in a vast space. Sitting at that point is a cost: feed those particular knob values into the cost function and you get one wrongness number. Now do something simple but powerful. Treat that cost as a height. High cost, high ground. Low cost, low ground.

Do that for every point, and the cost function stops being an abstract formula and becomes a terrain stretched over the space of all possible networks. Bad regions, where the knobs are set foolishly, rise up as hills and peaks. Good regions, where the network does well, sink into valleys. The single best setting of the knobs is the bottom of the lowest valley.

And our goal restates itself beautifully: we are somewhere on this terrain (wherever our current, probably-random knobs put us), and we want to get downhill, toward a valley.

Two knobs you can draw, thirteen thousand you cannot

Section titled “Two knobs you can draw, thirteen thousand you cannot”

It helps to shrink the problem until you can actually see it. Imagine a toy network with just two parameters. Then the space of settings is an ordinary flat plane (one axis per knob), and the cost above each point is a height. That is a 3D surface, a literal hilly landscape you could mold out of clay, with peaks and bowls and ridges.

The cost as a hilly landscape over the plane of knob settings A 3D wireframe surface drawn over a flat base plane whose two axes are the network's two knobs. The height of the surface at each point is the cost for those knob settings, so the surface is hilly, with peaks where the settings are poor and a clear valley where they are good. A dot labeled "you are here" sits high on a slope, with an arrow pointing downhill toward the valley. A real network has 13,000 knobs, so its landscape cannot be drawn, but the idea is identical. a valley (low cost) downhill you are here ↑ height = cost base plane = the two knob settings
Treat every setting of the knobs as a point on a plane, and the cost there as a height. The cost function becomes a terrain: poor settings rise into hills, good settings sink into valleys, and the best setting is the lowest valley floor. "Make the cost small" turns into something almost physical: from wherever you are, walk downhill. A real network's terrain has 13,000 dimensions, but the intuition is exactly this.

A real network has 13,000 knobs, not two, so its landscape lives in 13,000 dimensions plus one for height. Nobody can picture that, and you should not try. Here is the reassuring part: you do not need to. Every idea we develop on the 2D landscape, slopes, downhill, valleys, lowest points, carries over to high dimensions as exact mathematics, even though the picture stops being drawable. When we say “the cost slopes steeply here” or “we are sitting in a valley” in 13,000 dimensions, the words mean precisely what they mean on the clay model. The picture is a crutch for intuition; the math walks on its own.

Stand at a point on the landscape. Ask a small, local question: if I take one tiny step, does the ground rise or fall? The answer depends on which way you step. Step one way and you head uphill (cost goes up). Step the opposite way and you head downhill (cost goes down). Step sideways along a ridge and you might barely change height at all.

So at any point, every direction has a slope, a rate at which cost changes if you move that way. (This is the derivative idea from calculus; if you want the precise definition, Track 8 covers it in depth, but here you only need the intuition of “how steeply does it tilt this way.”) Among all those directions, one is the steepest climb: the direction in which cost rises fastest. And exactly opposite to it is the steepest descent: the direction in which cost falls fastest.

That steepest-uphill direction has a name. It is called the gradient of the cost, written with an upside-down triangle. The gradient is not a single number; it is a direction (a vector) that points the way cost increases fastest. You can think of it as a bundle of all the individual knob-slopes at once: for each knob, “how fast does cost change if I nudge just this one?”, collected into a single arrow that happens to point straight uphill.

Now the move the whole chapter has been building toward. We do not want to go uphill; we want cost to drop. So we step in the direction of the negative gradient, which is simply the gradient turned around to point straight downhill. A step that way lowers the cost faster than a step in any other direction.

Let us make it concrete on the simplest landscape there is. Take a network with a single knob whose cost is that knob squared. That is a parabola, a single valley with its bottom where the knob is zero. Suppose we are currently at the value 3.

At w = 3: the parabola slopes upward to the right, with slope 2w = 6.
The gradient (steepest uphill) points in the +w direction.
The negative gradient points in the -w direction.
Step a little in the -w direction (toward 0) and cost w² drops.

Moving from 3 toward 0 takes the cost from 9 down toward 0. Stepping against the slope lowered the cost, exactly as promised.

The same thing works with more knobs. Take two parameters, with the cost equal to the first knob squared plus the second knob squared, a round bowl with its bottom at the origin. At the point where the two knobs sit at 3 and 4, the gradient has components 6 and 8 (each is just that knob’s own slope, 2 times 3 and 2 times 4), pointing up and away from the bottom. The negative gradient, with components -6 and -8, points back toward the origin. Take a step that way and you slide toward the bottom of the bowl, and the cost (which is 25 at that point) goes down. Two knobs or thirteen thousand, the recipe for “which way is downhill” is the same: it is the negative gradient.

One honest complication. A landscape can have more than one valley, and they need not be equally deep. A point where every nearby direction leads uphill is called a local minimum: you are at the bottom of a valley, with no downhill step available from where you stand. But it might be a shallow valley, while a much deeper one sits somewhere else on the terrain. The deepest valley anywhere is the global minimum.

Walking downhill can get stuck in a shallow valley A wavy cost curve with two valleys separated by a hump. The left valley is shallow; the right valley is deeper and is the global minimum. A ball rests at the bottom of the shallow left valley. Because it only ever rolls downhill, it cannot climb over the hump to reach the deeper valley, so it is stuck at a local minimum that is not the best possible setting. knob setting cost stuck here local minimum (shallow) global minimum (deepest) can't climb over
Walking downhill only ever guarantees you reach the bottom of some valley, not the deepest one. Start in a shallow valley and step only downhill, and you settle there and stop, even though a better setting exists across the hump. So gradient descent finds a good network, usually not a provably best one.

This matters because walking downhill only ever guarantees you reach a local minimum, not the deepest one. If you start in a shallow valley and only ever step downhill, you settle at its bottom and stop, even though a better setting of the knobs exists elsewhere. So the downhill strategy does not promise the best possible network, only a locally good one. In practice this is often less damaging than the simple two-valley picture suggests, partly because high-dimensional landscapes behave in their own surprising ways, but it is a genuine limitation worth knowing: a trained network is usually a good solution, not provably the best one.

The landscape picture quietly explains several things you may have noticed about AI. Training is described as iterative, run for hours or weeks, because reaching a valley means taking many small downhill steps, not solving an equation in one shot. Two training runs of the same model can land on slightly different results because they started at different random points on the terrain and rolled into different valleys. And when people say a model “converged,” they mean the downhill walk reached a valley floor where steps stopped lowering the cost much. None of that is mystical once you can see the terrain: it is a long walk downhill across a landscape too big to see all at once.

Thinking the landscape is something the network contains. It is not stored anywhere. It is a way of picturing the cost function. The “height” at a point is just the cost you would get with those knob values.

Trying to visualize 13,000 dimensions. Do not. Build the intuition in 2D and trust that the math carries over. The inability to picture it changes nothing about how it works.

Thinking the gradient is a single number. In one dimension it is just a slope. With many knobs, the gradient is a direction (a vector) assembled from every knob’s individual slope. It points steepest-uphill; its negative points steepest-downhill.

Assuming downhill always reaches the best answer. It reaches a local minimum, a valley bottom, which may not be the deepest valley. Gradient-based learning finds a good solution, not a guaranteed-best one.

  • The cost landscape is the cost function pictured as terrain: every setting of the knobs is a point, and its cost is the height above that point. Low ground is good; the lowest valley is the best setting.
  • At any point, every direction has a slope. The gradient is the direction of steepest uphill; the negative gradient is steepest downhill.
  • Stepping along the negative gradient lowers the cost faster than any other step. Worked small: at the value 3 on the squared-knob parabola, the slope is 6, so stepping toward 0 drops the cost; with two knobs at 3 and 4 on a bowl, the negative gradient (components -6 and -8) points back to the bottom.
  • Downhill reaches a local minimum, not always the global one. A trained network is usually a good solution, not a provably best one.

The whole search has a shape now: you are standing on a vast hilly terrain, and the negative gradient is the compass that always points downhill.

Next: the cheatsheet puts the landscape, the gradient, and the worked slopes on one page. Then lesson 7 finally takes the walk. Start somewhere, read the negative gradient, take a small step, and repeat. That repeated downhill stepping is the algorithm this whole chapter is named for: gradient descent.