Log inStart free
LESSON 5.03 · 6 STAGES · 2 CONTROLS · 3 CHECKS

Backpropagation

Watch one backward pass find all 17 gradients at once.


EXPLAINER · 5.03 · BACKPROP BAY
01
Stage 1 of 6 · The graph
The 2-4-1 network is a computational graph. Each node does one step: a sum Σ, ReLU, σ or the loss.
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.

inputs and node valuesweights and biasesloss, gradients, local slopesoutput s and ŷ, finite-difference checkgraph nodesa negative weight, or a gradient of 0
Toy: the 5.C3 net · a 2-4-1 MLP at its seeded start, not trained · the 4 XOR examples
HOVER OR TAP A SHAPE TO SEE ITS NUMBERS
1, 1
Pick which XOR input flows through.
ReLU
Pick the function in the hidden neurons.
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
x→z→h→s→y^→ℓ\sy{x}{data}{x} \to \sy{z}{data}{z} \to \sy{h}{data}{h} \to \sy{s}{out}{s} \to \sy{yhat}{out}{\hat y} \to \sy{L}{grad}{\ell}
17 parameters: 8 wjk, 4 bj, 4 wout,j, 1 bout17 \text{ parameters: } 8\ w_{jk},\ 4\ b_j,\ 4\ w_{\text{out},j},\ 1\ b_{\text{out}}

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.

Predict, then reveal
0 / 3 REVEALED
Q01With ReLU and the input (0, 0), which first-layer parameters get a nonzero gradient?
1 · GUESS2 · TRY IT IN THE DIAGRAM
Q02With ReLU, does neuron 3 send any gradient back on any of the 4 inputs?
1 · GUESS2 · TRY IT IN THE DIAGRAM
Q03On the input (0, 1), is the guess too high or too low?
1 · GUESS2 · TRY IT IN THE DIAGRAM

Takeaways

  1. Backpropagation walks the graph from the loss back, with the chain rule at each node.
  2. A weight's gradient is the gradient at its output node times its input.
  3. One backward pass gives every gradient, so training scales to millions of weights.