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:

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:

  1. Bias. Push b up a touch and the pre-activation moves up, σ moves up, the neuron's activation moves up. Direct.
  2. Weights. Each weight w_i is multiplied by an input activation a_i. To raise the output: increase weights paired with active inputs, decrease weights paired with quiet ones. The bigger a_i is, the more bang per unit change in w_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.")
  3. Previous-layer activations. We can't tweak a_prev directly — those came from the layer before — but we can request changes to them. Symmetric to the weight case: ask each a_i to 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

  1. Forward — push x through every layer; cache each z(ℓ) and a(ℓ) on the way.
  2. Compute δ(L) at the output.
  3. Backward — apply the recursion δ(L−1), δ(L−2), …, δ(1).
  4. Read off ∂L/∂W(ℓ) = δ(ℓ)·(a(ℓ−1))T for every ℓ.
  5. 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:

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:

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

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.

  1. What does backpropagation compute?

  2. Stochastic gradient descent (SGD) differs from full-batch gradient descent because:

  3. Why is minibatch SGD the standard choice for training large neural networks?