Backpropagation
Watch one backward pass find all 17 gradients at once.
Explain it simply
You see the network from lesson 5.02 as a chain of small steps. The two inputs feed four sum nodes, and each sum goes through ReLU. A last sum goes through the sigmoid to give the guess ŷ. The last node compares the guess with the target and gives the loss.
A computational graph. Inputs x1 and x2 feed 4 sum nodes, then 4 ReLU nodes. One output sum s feeds a sigmoid node for the guess, then a loss node. The strip below lists the 17 parameters.
Each node is one step on the values that flow in.
Show the code ▾Hide the code ▴ · Try it yourself · Python 3
import math
# the 5.C3 net at its seeded start (not trained) and the 4 XOR examples
W1 = [[-0.28, 0.93], [-0.21, 0.95], [-0.14, -0.87], [0.76, 0.4]] # W1[j] = [w_j1, w_j2]
b1 = [0, 0, 0, 0]
w_out = [-0.87, 0.11, 0.07, 0.03]
b_out = 0
X = [(0, 0), (0, 1), (1, 0), (1, 1)]
Y = [0, 1, 1, 0]
x, y = X[3], Y[3]
act = lambda z: max(0.0, z)
dact = lambda z, h: 1.0 if z > 0 else 0.0 # ReLU', 0 at z = 0 as in PyTorch
def forward(W1, b1, w_out, b_out):
z = [W1[j][0] * x[0] + W1[j][1] * x[1] + b1[j] for j in range(4)]
h = [act(zj) for zj in z]
s = sum(w_out[j] * h[j] for j in range(4)) + b_out
yhat = 1 / (1 + math.exp(-s))
loss = -(y * math.log(yhat) + (1 - y) * math.log(1 - yhat))
return z, h, yhat, loss
# forward pass: every node keeps its value
z, h, yhat, loss = forward(W1, b1, w_out, b_out)
print(f"yhat {yhat:.4f} loss {loss:.4f}") # yhat 0.3895 loss 0.4935
# backward pass: the chain rule, from the loss back to every parameter
d_s = yhat - y # through the loss and the sigmoid
g_w_out = [d_s * h[j] for j in range(4)]
g_b_out = d_s
d_h = [d_s * w_out[j] for j in range(4)]
d_z = [d_h[j] * dact(z[j], h[j]) for j in range(4)]
g_W1 = [[d_z[j] * x[0], d_z[j] * x[1]] for j in range(4)]
g_b1 = d_z
show = lambda a: ' '.join(f'{g:.4f}' for g in a)
print("dl/ds ", f"{d_s:.4f}") # dl/ds 0.3895
print("dl/dw_out", show(g_w_out)) # dl/dw_out 0.2532 0.2883 0.0000 0.4519
print("dl/dz ", show(d_z)) # dl/dz -0.3389 0.0428 0.0000 0.0117
print("dl/dw_j1 ", show(g[0] for g in g_W1)) # dl/dw_j1 -0.3389 0.0428 0.0000 0.0117
print("dl/dw_j2 ", show(g[1] for g in g_W1)) # dl/dw_j2 -0.3389 0.0428 0.0000 0.0117
# check one gradient with a finite difference: 2 more forward passes
eps = 1e-5
up = forward(W1, b1, [w_out[0] + eps] + w_out[1:], b_out)[3]
dn = forward(W1, b1, [w_out[0] - eps] + w_out[1:], b_out)[3]
print("check dl/dw_out1", f"{(up - dn) / (2 * eps):.6f}", f"{g_w_out[0]:.6f}") # check dl/dw_out1 0.253193 0.253193
Copy it into a file and run it. Plain Python, no libraries.
Common mix-ups
MYTHBackpropagation runs a separate pass for each weight.
FIXOne backward pass gives all 17 gradients. The finite-difference check needs 34 forward passes for the same numbers.
MYTHA large loss gives every weight a large gradient.
FIXA weight's gradient is ∂ℓ/∂z times its input. On (0, 0), ℓ is 0.69, more than the 0.49 of (1, 1), yet every w gets 0.
Takeaways
- Backpropagation walks the graph from the loss back, with the chain rule at each node.
- A weight's gradient is the gradient at its output node times its input.
- One backward pass gives every gradient, so training scales to millions of weights.