MLDay 30 of 50Deep Learning
Backpropagation by Hand
A network's prediction misses, and each of its weights played a part.
A network's prediction misses, and each of its weights played a part. To learn, it needs each weight's share of the blame. Nudging one weight at a time costs extra runs for every weight. Backpropagation gets every share from one pass backward through the network.
The idea
A network is a chain of small steps (drawn as a computational graph, as below): a weight changes a hidden value, which changes the prediction, which changes the loss. Each step has a local slope: how much its output moves when its input moves by 1. The slope of the whole chain is the product of the local slopes along it.
Backpropagation walks the chain from the loss back to each weight, multiplying local slopes as it goes. The result is the weight's gradient, the slope of the loss with respect to that weight, which gradient descent uses to update it (D09). The slopes near the loss are shared by every weight, so each is computed once.
A tiny example
Take the smallest network with layers: one hidden neuron, one output neuron, no biases. The hidden neuron computes , where ReLU (from D29) turns negative numbers into 0. The output is , and the loss is . Use one example, and , with and .
Forward pass (left to right, keeping every value):
| Step | Calculation | Value |
|---|---|---|
| the score | 6 | |
| hidden output | is positive, so it passes | 6 |
| prediction | 3 | |
| loss | 1 |
Backward pass (right to left, one local slope per step):
| Local slope | Rule | Value |
|---|---|---|
| loss vs. prediction | −2 | |
| prediction vs. | , so the slope is | 0.5 |
| vs. | ReLU's slope: 1, since | 1 |
| vs. | , so the slope is | 3 |
Multiply them: the gradient for is . The chain for is shorter. The prediction moves with at rate , so its gradient is . Both are negative, so raising either weight lowers the loss.
One update with learning rate gives and .
The formula
The chain rule gives a weight's gradient as the product of the local slopes between it and the loss:
| Symbol | Meaning |
|---|---|
| the loss, | |
| the hidden neuron's weight | |
| the hidden output, | |
| the prediction, | |
| the slope of when only changes |
Worked: .
To check a hand-derived gradient, estimate it numerically: nudge the weight a tiny step each way and measure how the loss moves. This gradient check needs no calculus, so it catches mistakes in the backward pass:
| Symbol | Meaning |
|---|---|
| the weight being checked; all others stay fixed | |
| a tiny step, such as 0.001 | |
| the loss from a forward pass with the weight nudged up or down |
Worked, for and : , the same as backpropagation.
Try it
Step through backprop
One example: x = 3, y = 4. Change a weight and watch every number update, or take steps downhill.
∂L/∂w1 = (-2)·(0.5)·(1)·(3) = -3
∂L/∂w2 = (-2)·(h = 6) = -12
Check the slopes against the tables above, then click Take a step a few times. The prediction climbs from 3 toward 4, and the gradients shrink toward 0 as the miss disappears. Then press Reset and drag the weights: push to 1 and watch the gradient for flip sign, or pull below 0 and watch ReLU cut it off.
In code
The backward pass is three lines, and the gradient check referees it:
# script_id: day_030_backpropagation_by_hand/backprop
x, y = 3.0, 4.0
def loss(w1: float, w2: float) -> float:
return (w2 * max(w1 * x, 0.0) - y) ** 2
def backprop(w1: float, w2: float) -> tuple[float, float]:
z = w1 * x # forward pass, keeping each value
h = max(z, 0.0) # ReLU
y_hat = w2 * h
d_yhat = 2 * (y_hat - y) # backward: slope of the loss
d_w1 = d_yhat * w2 * (1.0 if z > 0 else 0.0) * x # times the slopes of w2*h, ReLU, w1*x
return d_w1, d_yhat * h # dL/dw1, dL/dw2
g1, g2 = backprop(2.0, 0.5)
eps = 1e-3
n1 = (loss(2.0 + eps, 0.5) - loss(2.0 - eps, 0.5)) / (2 * eps)
n2 = (loss(2.0, 0.5 + eps) - loss(2.0, 0.5 - eps)) / (2 * eps)
print(f"dL/dw1: backprop {g1:+.6f}, numeric {n1:+.6f}")
print(f"dL/dw2: backprop {g2:+.6f}, numeric {n2:+.6f}")
w1, w2 = 2.0 - 0.01 * g1, 0.5 - 0.01 * g2
print(f"after one step: w1 = {w1:.2f}, w2 = {w2:.2f}, loss = {loss(w1, w2):.4f}")
dL/dw1: backprop -3.000000, numeric -3.000000
dL/dw2: backprop -12.000000, numeric -12.000000
after one step: w1 = 2.03, w2 = 0.62, loss = 0.0503
One step cuts the loss from 1 to 0.0503.
Quick check
Quick check
1. Keep x = 3, y = 4 and w1 = 2, but set w2 = 1. What is dL/dw1?
2. With x = 3 and w1 = -1, what gradient does w1 get?
3. Backprop says -1.5, but the gradient check says -3.000000. What is most likely?
Next
In D31: PyTorch Basics: Tensors and Autograd,
PyTorch runs this backward pass for you: one call to .backward() fills in every gradient.