0:00Intro: MOFUSA Tech Academy
Welcome to MOFUSA Tech Academy. Beyond learning, towards mastery.
0:092,410 weights, one algorithm: the hook
Brightcart's digit network has two thousand four hundred and ten parameters: weights and biases. To train it with gradient descent, we need the gradient for every one of them: which way to turn it, and how strongly. The obvious approach is to wiggle each weight a tiny bit, rerun the whole network, and see how the loss changes. That's two thousand four hundred and ten runs for a single training step. For a language model with billions of weights, it would take billions of runs per step. Hopeless. Backpropagation gets every single gradient with one forward pass and one backward pass, at roughly the cost of running the network about three times.
It's the algorithm that made deep learning practical. And today, we'll do it completely by hand, with real numbers, on a small network. No steps skipped. Season Two, Module Seven, stop three: backpropagation, one of the most important stops on the whole map.
1:13Our tiny network
Here's our network: two inputs, two hidden neurons, one output, with sigmoid activations everywhere. Think of it as a tiny scorer for "will this customer reorder?", with two scaled customer features as inputs. Every weight and bias is written on screen. The inputs are zero point five and one. This customer did reorder, so the target is one. We'll measure the loss as half the squared error. The half is only there to make one derivative neater. We'll do three things: a forward pass to get the prediction and the loss, a backward pass to get every gradient, and an update.
Then we'll check our maths.
1:57The forward pass, with real numbers
Forward pass. First hidden neuron: zero point four times zero point five, plus negative zero point two times one, plus the bias zero point one. That's zero point one, and the sigmoid of zero point one is zero point five two five. Second hidden neuron: zero point three times zero point five, plus zero point six times one, minus zero point one: zero point six five. Its sigmoid: zero point six five seven. Now the output: zero point seven times zero point five two five, plus negative zero point five times zero point six five seven, plus zero point two.
That's zero point two three nine, and its sigmoid is zero point five five nine five. The network says fifty-six percent. The truth is one. The loss is half of zero point four four zero five squared: about zero point zero nine seven. Notice that we keep every intermediate value. We'll need them on the way back.
3:03The backward pass: the output layer
Now the backward pass. We'll walk from the loss back towards the inputs, applying the chain rule from episode three at every step. First: how does the loss change if the prediction changes? For half the squared error, the derivative is the prediction minus the target: minus zero point four four. Negative means raising the prediction would lower the loss. Makes sense, since the target is one. Next, step back through the sigmoid. Its slope, as we saw in episode eleven, is the output times one minus the output: zero point two four six five.
Multiply along the chain: minus zero point one zero eight six. That's the error signal at the output neuron, often written with the Greek letter delta. Now the output weights. The output's input sum is each weight times its hidden value, so moving a weight changes the sum by that hidden value. The gradient for the first output weight is minus zero point one zero eight six times zero point five two five: minus zero point zero five seven. For the second: minus zero point zero seven one three. And the bias gradient is just the error signal itself.
Here's a pattern worth remembering. A weight's gradient is the error signal at the neuron it feeds into, times the activation flowing into it from the other end.
4:30The backward pass: into the hidden layer
Now we carry the error signal back into the hidden layer. How does the loss change if a hidden neuron's value changes? Its value reaches the output through one weight, so the signal is the output's error times that weight. For the first hidden neuron: minus zero point one zero eight six times zero point seven, minus zero point zero seven six. For the second: times minus zero point five, which gives plus zero point zero five four three. Read those signs. Raising the first hidden neuron would lower the loss. But the second neuron feeds the output through a negative weight, so raising it would push the prediction down and the loss up.
The network wants it lower. Step back through each hidden sigmoid, multiplying by its slope: zero point two four nine four and zero point two two five three. That gives the hidden error signals: minus zero point zero one nine, and plus zero point zero one two two. And finally, the first-layer weights, using the same pattern: error signal times input. For the first hidden neuron, minus zero point zero one nine times zero point five, and times one. For the second, zero point zero one two two times zero point five, and times one.
Every one of the network's nine parameters now has its gradient. For any network, however deep, the whole procedure is these same three lines, applied layer by layer: compute the output's error signal, pass it backwards through the transposed weights while multiplying by each activation's slope, and turn it into weight gradients.
6:16The update, and the gradient check
Now the update, plain gradient descent from episode five with a learning rate of zero point five. Each weight moves against its gradient. The first output weight rises from zero point seven to zero point seven two eight five. The second becomes less negative, minus zero point four six four three. And so on, through every weight. Let's run the forward pass again. The prediction rises from fifty-six percent to fifty-eight point three five, and the loss drops from zero point zero nine seven to zero point zero eight seven.
One step, and the network is better. Repeat this thousands of times on thousands of examples, and that's training. But how do we know our calculus was right? There's a beautiful sanity check. Nudge one weight up by a tiny amount and down by the same amount, rerun the network both times, and measure the slope directly. For the first output weight, the measured slope is minus zero point zero five seven, exactly what backprop gave us. This is called a gradient check, and engineers use it to test new code. In practice, frameworks like PyTorch compute all of this automatically, a feature called autograd.
But now you know exactly what they're doing.
7:37Why backprop is so efficient
Why is backprop so efficient? Because of shared work. The error signal at each neuron is computed once, and then reused by every weight feeding into that neuron, and by every neuron in the layer before. Nothing is computed twice. That's why getting all the gradients costs only a small multiple of a single forward pass, no matter how many weights there are. Mathematicians call this reverse-mode automatic differentiation. The price is memory. To go backwards, we need every intermediate value from the forward pass, all those lockers.
That's why training a network needs far more memory than simply using it, something that will matter a lot when we fine-tune large models in Season Five. The core idea goes back to the nineteen-seventies, but a famous nineteen eighty-six paper by David Rumelhart, Geoffrey Hinton and Ronald Williams showed how well it trains multi-layer neural networks, and it's been the foundation of deep learning ever since.
8:44A glimpse of vanishing gradients
Before we finish, look at the slopes we kept multiplying by: zero point two five, zero point two five, zero point two three. A sigmoid's slope is never more than a quarter. In our tiny network that's fine. But in a network ten sigmoid layers deep, the sigmoid slopes alone multiply the error signal by at most a quarter at every layer on the way back. Unless the weights happen to compensate, by the first layer it's less than one millionth of its original size. The early layers barely learn at all. This is called the vanishing gradient problem, and it held deep learning back for years.
We'll see how it was solved later in this module. ReLU, from last episode, is part of the answer.
9:32Try it yourself: one more gradient
Let's make sure the pattern has clicked. What's the gradient for the second output weight? Remember: the error signal at the neuron it feeds, times the activation flowing into it. Pause and compute. The output's error signal is minus zero point one zero eight six, and the second hidden neuron's activation is zero point six five seven. Multiply: minus zero point zero seven one three, exactly what we found earlier. And a bias gradient is even simpler: it's just the error signal itself, because a bias is like a weight on an input that's always one.
The second hidden bias gets plus zero point zero one two two.
10:22Backprop through ReLU
What changes with ReLU instead of sigmoid? Only the slope at each neuron. ReLU's slope is one when the neuron is active, and zero when it's switched off. In our ReLU example, the first hidden neuron was off, so during backpropagation no error signal passes back through it. For this example, its incoming weights get no update at all. The active neuron passes the signal straight through, undiminished. That's ReLU's great advantage: a slope of one doesn't shrink the signal, unlike the sigmoid's quarter. And it explains the dead-neuron problem: a neuron that's off for every input never receives any gradient, so it can never learn its way back.
11:09Backprop over a whole batch
In real training, backpropagation runs on a whole mini-batch at once. Thirty-two examples go forward together as a matrix, the error signals for all of them flow backwards together, and the weight gradients are averaged across the batch before a single update. Written in matrix form, the backward pass is, again, mostly matrix multiplication, with the transposes we met in episode two making the shapes line up. Forward and backward, it's exactly the kind of work graphics cards are built for.
11:44Automatic differentiation in practice
You'll almost never write backpropagation by hand in practice. Modern frameworks record every operation as your code runs, building the computation graph automatically. Ask for the gradient of the loss, and they apply the chain rule backwards through that graph, putting a gradient on every parameter. This is automatic differentiation. What's left for you? Choosing the right loss, getting the shapes right, gradient-checking any custom layer you invent, and watching for warning signs, like losses that suddenly become "not a number", which usually means something exploded.
Knowing what happens underneath, as you now do, makes those problems far easier to diagnose.
12:29Why gradients vanish, with numbers
We glimpsed this already, but let's put a number on it. Through four sigmoid layers, the error signal is multiplied by at most a quarter four times: at most zero point zero zero three nine, less than half a percent of its original size. Through four active ReLU layers, the activation slopes are all one, so the signal survives much better, as long as the weights don't shrink it. Keeping that signal alive across dozens of layers is the topic of episode twenty-eight.
13:03The complete learning loop
Step back and look at what you now have: the complete learning loop. Data goes in. The forward pass makes a prediction. The loss measures the mistake. Backpropagation works out every weight's share of the blame. The optimiser takes a step. Repeat, and check on validation data. Every neural network ever trained, from a digit reader to a language model, follows this loop. Next episode, we run it for real.
13:34Myth vs Fact
Myth: backpropagation is how a network learns. Strictly, backpropagation only computes the gradients. A separate optimiser, like the gradient descent we just used, or the smarter optimisers coming in episode twenty-seven, decides how to use them. Backprop answers "which way?" The optimiser decides "how far?"
13:59Recap
Let's recap. The forward pass computes the prediction and stores every intermediate value. The backward pass applies the chain rule from the loss back to the inputs: each neuron's error signal is passed back through the weights and multiplied by the activation's slope. Each weight's gradient is the error signal it feeds, times the activation flowing into it. An optimiser then uses those gradients to update the weights. And because work is shared, all the gradients cost about as much as a few forward passes. One sentence to remember: errors flow backwards, and every weight gets its share of the blame.
14:38Next: training a network from scratch
Mission complete. We computed every gradient in a neural network by hand, proved them correct with a gradient check, and watched one update make the network better. Next, we put it all together and train Brightcart's digit reader from scratch: thousands of images, epochs, batches, loss curves and accuracy, and we'll watch the network's neurons learn to see. See you on the map.