Learning · Deep learning · Step 2
Cost & gradient descent
Step 1 said a network is a parametric function: feed in
x, multiply by some matrices, add some biases,
squash, repeat. The 13 002 numbers that
define those matrices (for our digit-recognition example)
are just sitting there, completely arbitrary, doing nothing
useful — until somebody fills them in.
That's what training is. You hand the network a pile of labelled examples — for MNIST, ~60 000 images each paired with the correct digit — and you adjust those 13 002 parameters until the output activations line up with the labels. Same story for 3Blue1Brown's lesson on this topic, same story for every neural network ever shipped: pick a number that says "how wrong are you", and minimise it.
Cost: telling the computer it's wrong
Show the network an image of a 3. It spits out 10 numbers — one
activation per digit. What you wanted is something like
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0] — high at index 3,
zero everywhere else. What you got on a freshly
randomised network is more like
[0.41, 0.05, 0.32, 0.18, 0.74, …] — basically
noise. The cost on this single example is
the sum of the squared differences:
C(x) = Σi ( y_predi(x) − y_truei(x) )²
Small when the network is right and confident; big when it's wrong or unsure. (Squaring punishes large errors more than small ones and keeps the sign positive — convex, differentiable, cheap.) Now you've told the computer how badly it did on this image.
Average cost over the training set
But you don't care about one image — you want the network to be
right about every image. So the actual quantity we
minimise is the average of C(x) over the whole
training set:
L = (1/N) Σx C(x) = (1/N) Σx Σi ( y_predi(x) − y_truei(x) )²
This number depends on every parameter in the network
— change a single weight and L moves a tiny bit.
Training is, literally, finding the (W, b) values
that make L as small as possible. Nothing else.
Gradient descent: a ball rolling downhill
Start with one parameter — call it w — and a cost
L(w) that happens to be a bowl-shaped curve. The
minimum is at the bottom. You don't know where the bottom is
in advance, but you know one local fact: the slope dL/dw at your current spot. If it's positive,
you're on the right wall — step left. If it's negative, you're
on the left wall — step right. Repeat.
In one dimension this is just middle-school calculus. The
update is w ← w − η · dL/dw — take the slope at
your current point, multiply by a small step size
η, subtract. Repeat. When the curve is steep the
step is big; near the minimum the slope is shallow so the
step is small and you naturally settle in without overshooting
(most of the time — see η below).
Two parameters: a surface to descend
Add a second parameter and the cost L(w, b)
becomes a surface — a bowl if you're lucky, something more
rugged if you're not. The slope generalises to the
gradient ∇L: a 2-vector
[ ∂L/∂w, ∂L/∂b ] pointing in the direction
L grows fastest. Step opposite to it:
θ ← θ − η · ∇L(θ)
θ = [ w, b ] is the parameter vector,
η is the learning rate (how big
a step), ∇L(θ) is the gradient at the current
point. Run this loop a few thousand times and you've walked
to a minimum. That's gradient descent — the entire algorithm,
in one line.
Try it
A toy problem with two parameters — slope m and
intercept b in y = m·x + b — so we can
plot the cost surface as a heatmap. Bright = low L
(good fit); dark = high L. Click anywhere on the surface to set
a starting (m, b), pick a learning rate, and watch
gradient descent walk down to the minimum while the red line on
the right fits the data.
Cost surface — top-down L(m, b)
click to set start · white line = path · ★ = true minimum
Cost surface — 3D bowl L(m, b)
drag to rotate · walls = high L · path rolls down the inside
Data & current fit
blue dots = labelled data · red = current y_pred · green dashed = true line
What to notice
- η controls everything. Crank it past
0.3— the path overshoots the bottom and bounces between the walls, sometimes diverging entirely. Drop it to0.001— convergence becomes glacial. The sweet spot is "as big as possible without diverging". - The path is always perpendicular to the contours.
Each step points along
−∇L, which by definition is normal to the level lines. Big steps where the bowl is steep, tiny steps near the bottom — the gradient itself shrinks as you approach the minimum, so the same η gives you naturally-adaptive step sizes for free. - Gradient magnitude = "which parameter matters
most". Each component of
−∇Lis a nudge for one parameter. Their relative magnitudes tell you which knob currently has the most bang for the buck — the steepest direction in the bowl is the parameter whose tweak would cut the cost the most. Not all 13 002 dimensions get the same attention per step. - This bowl is convex — one global minimum, no traps. Click a few different starting points; every path lands in the same place. Real deep-net losses are wildly non-convex with countless local minima and saddle points; different starting points can land in different valleys. Gradient descent still works there, surprisingly often.
- Labelled data → weights. The
(x, y)pairs above define the cost surface — change the data, the surface changes, the minimum moves. Training is literally "let the data sculpt the landscape, then roll down".
From 2 parameters to 13 002: give up on the bowl
The toy above has two parameters, slope m and
intercept b — which is itself the world's smallest
neural network: one input, one output, one weight, one bias, no
activation. We could draw the cost as a 3-D bowl because two
parameters fit on a screen. A real network blows past that
instantly. The 2 → 3 → 1 network
from Step 1 already has 13 parameters; the
MNIST network has 13 002; a frontier LLM has
over 1011. You're not going to plot
those.
So stop trying to. The trick is to abandon the spatial picture
and re-read ∇L as a list of nudges. For
each parameter, the corresponding entry of −∇L
says: "this is how much to add to me, this step, to make the
cost go down most efficiently." That's all. No bowl, no walls,
just a giant vector of per-parameter instructions.
Same update rule. The line
θ ← θ − η · ∇L(θ) doesn't care whether θ
is 2 numbers or 175 billion. It nudges every parameter
by a tiny amount opposite to its gradient component, and the
cost goes down. The bowl above is just the case where we can
draw it.
So this is what "learning" means
Re-read the chain of definitions: a network is a function with parameters; the cost is a scalar that says how wrong the function is on average; the gradient says which direction in parameter-space lowers the cost fastest; the update rule walks that direction. Put together: when somebody says a network "learns", they mean — and this is the entire technical content — they ran gradient descent on the cost function, and the parameters drifted toward values that make the cost smaller. No more, no less.
What's missing? Two things. First, a caveat: real loss landscapes aren't convex bowls. They're crumpled, full of saddle points and shallow valleys, and gradient descent can get stuck in a local minimum that's nowhere near the best possible one. The fact that it works as well as it does in practice — that the local minima you fall into tend to be good enough — is one of the lucky empirical facts of deep learning, not a theorem.
Second, a procedural one: we've described
what the gradient is and what we do with it,
but not how to compute ∇L efficiently
when the loss depends on every weight through a chain of
matrix products and nonlinearities going back through dozens
of layers. Naively differentiating one parameter at a time
would be hopelessly slow. The algorithm that gets you the
full gradient for the cost of roughly one forward pass is
called backpropagation, and it's the next
step.
Step exam
Answer all 3 questions correctly to complete this step.
-
What is a cost function (loss function) for a neural network?
-
Gradient descent updates parameters in which direction?
-
If the learning rate η is set too large, gradient descent will most likely: