Learning · Deep learning · Step 3
Backprop & stochastic gradient descent
Step 2 said: step opposite to ∇L.
Backpropagation is how you actually compute
that gradient — every ∂L/∂W⁽ˡ⁾ and
∂L/∂b⁽ˡ⁾ in a network with millions or billions
of parameters — in a single extra pass through the
layers. Brute force (perturb one weight, run a forward pass,
repeat) would need one forward pass per parameter; hopeless.
Backprop reuses every quantity the forward pass already
cached and gets the whole gradient for roughly the cost of
one more forward pass.
The formulas come from the chain rule, and we'll write them out below. But the easiest way to actually understand backprop is the way 3Blue1Brown teaches it: forget the calculus for a moment, and ask — for each output neuron — what does this neuron want?
What does this output neuron want?
Show our 784 → 16 → 16 → 10 digit-recogniser an image of a
2. The 10 output activations come back as
something like [0.41, 0.05, 0.32, 0.18, 0.74, …]
— call that y_pred. What we wanted was
[0, 0, 1, 0, 0, …] — call that
y_true. Look at each output neuron one at a
time:
- Neuron 2 (the digit-2 detector) is at
0.32— too low; we want it to go up, by about0.68. - Neuron 4 is at
0.74— way too high; we want it down, by0.74. - Every other neuron has its own delta — small for the already-near-correct ones, big for the badly-wrong ones.
Those ten per-neuron desires are, almost literally, the gradient at the output layer. The whole job of backprop is to translate them — desires expressed about output activations — into nudges for the weights and biases that produced those activations. And then to push the desires one layer back, and do it again.
Three ways to make a neuron's activation change
Stay with one output neuron — say the digit-2 detector,
currently at 0.32, wanting to go up. Its
activation is a = σ( W · a_prev + b ). Three
quantities feed that a; three things we can
adjust:
- Bias. Push
bup a touch and the pre-activation moves up,σmoves up, the neuron's activation moves up. Direct. - Weights. Each weight
w_iis multiplied by an input activationa_i. To raise the output: increase weights paired with active inputs, decrease weights paired with quiet ones. The biggera_iis, the more bang per unit change inw_i— so the natural per-weight nudge is proportional to its input activation. Bright inputs get most of the attention. ("The connections with the brightest neurons from the preceding layer have the biggest effect.") - Previous-layer activations. We can't tweak
a_prevdirectly — those came from the layer before — but we can request changes to them. Symmetric to the weight case: ask eacha_ito move proportional to its weight. Big positive weight → ask for an increase. Big negative weight → ask for a decrease. Small weights → don't bother.
Sum the requests, then propagate backward
Output neuron 2 has its own list of requests for the last hidden layer. Output neuron 4 — also wired to the same hidden layer — has its own list. So do neurons 0, 1, 3, 5, 6, 7, 8, 9. We can't satisfy ten competing wishes perfectly; we compromise the only way that makes sense — add them up. Algebraic sum, signs and magnitudes. The result is the hidden layer's overall desired change vector.
Now we're back where we started, one layer earlier. The last hidden layer has its own list of per-neuron desires. Three pathways: adjust its biases, adjust its weights with the same bang-for-buck logic, request changes from the layer before. Sum the requests. Hand them off. Repeat until you've made requests of every layer back to the inputs.
That's the algorithm in one paragraph. The name
backpropagation comes from the literal
mechanic: per-neuron desires at the output layer get
propagated backward through the network, layer by
layer, summing on the way. When the process terminates you
have a desired nudge for every weight and every bias —
which is, by definition, the gradient ∇L.
The math, layer by layer
Forward pass — at each layer l cache the pre-activation z and the activation a:
z(ℓ) = W(ℓ) · a(ℓ−1) + b(ℓ) ← pre-activation a(ℓ) = σ ( z(ℓ) ) ← after the nonlinearity
Now the key trick — define an error signal δ(ℓ) for each layer. It tells you
"how much does L change if the pre-activation
z(ℓ) wiggles":
δ(ℓ) ≡ ∂L / ∂z(ℓ)
With δ(ℓ) in hand, the chain rule gives the per-parameter gradients for free:
∂L / ∂W(ℓ) = δ(ℓ) · ( a(ℓ−1) )T ← weight gradient ∂L / ∂b(ℓ) = δ(ℓ) ← bias gradient
And δ(ℓ) itself is built recursively, starting at the output layer L and walking backward through the network:
δ(L) = ∇y_pred L ⊙ σ′( z(L) ) ← at the output δ(ℓ) = ( ( W(ℓ+1) )T · δ(ℓ+1) ) ⊙ σ′( z(ℓ) ) ← one layer back
Read the recursion aloud: "the error at layer ℓ equals the error at layer ℓ+1, pushed back through the transpose weight matrix, then gated by the slope σ′ of this layer's activation." The symbol ⊙ is element-wise multiplication.
One training step, end to end
- Forward — push
xthrough every layer; cache eachz(ℓ)anda(ℓ)on the way. - Compute
δ(L)at the output. - Backward — apply the recursion
δ(L−1), δ(L−2), …, δ(1). - Read off
∂L/∂W(ℓ) = δ(ℓ)·(a(ℓ−1))Tfor every ℓ. - Step
W(ℓ) ← W(ℓ) − η·∂L/∂W(ℓ)(same for biases).
What each weight gradient means
Look at the formula ∂L/∂wij(ℓ) = δj(ℓ) · ai(ℓ−1)
literally — a weight gets a big gradient when both:
- its input
ai(ℓ−1)was active (the pre-synaptic neuron fired), AND - its output
δj(ℓ)is responsible for a lot of loss (the post-synaptic error signal is large).
Either factor near zero → no update. Both nonzero → big update, with a sign telling you which way to push.
Hebbian intuition
Donald Hebb, 1949: "neurons that fire together,
wire together". Biology's hypothesis was that a synapse
strengthens when its pre- and post-synaptic neurons co-activate
— a rule of the shape Δw ∝ pre × post.
Backprop arrives at the same shape from pure calculus:
Δw_ij ∝ a_i · δ_j. The only differences vs. Hebb's
rule are that δ_j carries a sign
(push up or down) and a magnitude set by the
chain rule so the update actually decreases L. The
biological intuition was right; calculus pinned down the details.
Stochastic gradient descent — a drunk man on the hill
Backprop gets you the gradient for ONE training example
efficiently. But the gradient ∇L we actually want
is the average over the whole training set —
remember, one image's gradient on its own would push the
network to classify everything as a 2. For a million-sample
dataset that's a million forward+backward passes per step,
far too slow. The practical answer is to estimate
∇L from a small random subset every step.
3Blue1Brown's metaphor for the difference: full-batch gradient descent is "a carefully calculating man who takes slow, deliberate steps downhill"; stochastic gradient descent is "a drunk man stumbling aimlessly down a hill but taking quick steps". The drunk man's individual steps zigzag, but on average he still moves down — and he gets to the bottom much faster. The three flavours:
- Full-batch GD — ∇L over every sample. Smooth, deterministic, expensive. (What Step 2 ran.)
- SGD (stochastic GD) — ∇L over one random sample. Cheap per step, but each gradient is a noisy estimate of the true ∇L. The path zigzags.
- Minibatch SGD — ∇L over a small random batch (typically 32–256). The modern default: GPUs vectorise a minibatch in parallel, so per-step cost stays low while the gradient noise stays manageable.
Try it
Same toy as Step 2 — 8 labelled points, two parameters
(m, b). Now you also pick the
batch size: how many of the 8 to use per step.
Drop it from 8 (full) down to 1 (SGD)
and watch the path turn from a clean curve into a noisy zigzag.
Run twice with batch = 1, get two different trajectories.
Cost surface — top-down L(m, b)
click to set start · white = 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
- Batch = 8 (full): deterministic, smooth. Same path every run from the same start — identical to Step 2.
- Batch = 1 (SGD): the path is a noisy zigzag. Each step uses a single sample whose gradient direction can be wildly off; only on AVERAGE does it point downhill. Run it twice from the same start — the two paths diverge after step one because the random sample was different.
- Batch = 2 or 4: some noise, still heads downhill quickly. The "modern default" zone (just at a much smaller scale than real training, where 32–256 is typical).
- SGD never sits still. Near the minimum, every per-sample gradient is still nonzero (each sample wants its own slightly different fit), so the path keeps jittering around the optimum instead of locking onto it. Real training decays η over time to slowly tame this and let the path settle.
- Noise can help. In deep, non-convex networks, that zigzag is what lets SGD escape narrow local minima and saddle points that smooth full-batch GD might get stuck in — one of the reasons SGD beats batch in practice, not just in cost-per-step.
So what does "learning" mean now?
Step 1 said: a network is a function with parameters. Step 2 said: training is minimising the cost of that function over the data, and gradient descent is how you minimise it. Step 3 has now shown how to actually compute that gradient — by translating the output's "I want my activations to look like this" into a back-propagated chain of weight and bias nudges, averaged over a small random batch.
When you read that a network "learned" something, what literally happened was: stochastic gradient descent on the cost function, with backprop supplying the gradient at every step, for thousands or millions of steps, on a stream of labelled examples. The parameters drifted toward values that make the cost smaller. Whatever "feature detectors" or "concepts" the hidden layers ended up encoding emerged because they helped cut the cost — not because anybody designed them.
That's all of it. The same loop trains a 13-parameter toy regressor on 8 data points and a 175-billion-parameter LLM on a slice of the web. Only the sizes change.
Step exam
Answer all 3 questions correctly to complete this step.
-
What does backpropagation compute?
-
Stochastic gradient descent (SGD) differs from full-batch gradient descent because:
-
Why is minibatch SGD the standard choice for training large neural networks?