x1w12z → hw21.5ylosst1dw1dzdw2dy1z = w1 * x2h = max(0.0, z)3y = w2 * h4loss = (y - t) ** 21w1 -= lr * dw12w2 -= lr * dw21dy = 2 * (y - t)2dw2 = dy * h3dh = dy * w24dz = dh if z > 0 else 0.05dw1 = dz * x
Blocks: the network's parts, each showing its number on the frontKhaki chip: a value going forward · gold chip: a gradient going backGold cells: the gradients the backward pass fills inA tiny network on a table: input x, weight w1, a ReLU neuron, weight w2, output y and the loss, joined by wires, with the target t in front of the loss. At the back, the forward and backward pass in Python; in front, one gold gradient cell per part and the update code. The forward pass carries values left to right and the loss comes out at 4. The backward pass carries the gradient right to left, multiplying by each local slope (the chain rule), so one sweep fills in dw1 = 6 and dw2 = 8. A step of gradient descent then moves both weights and the loss falls to 0.0004. Real networks have many neurons per layer, a bias added in each neuron and millions of weights, and they average the loss over a batch of examples. The rule at each step is the same: multiply the upstream gradient by the local slope.

Forward pass

  1. Our network has one input, x = 1, two weights, w1 = 2 and w2 = 1.5, and one hidden neuron with ReLU: it keeps positive numbers and turns negative ones into 0. We want the output to be 1.

Backward pass

Learning

What happens

;; step 1/11 · network
input    x = 1          target  t = 1
weights  w1 = 2         w2 = 1.5
neuron   z = w1 * x,    h = relu(z) = max(0, z)
output   y = w2 * h

the weights are what training is allowed to change

Real networks have many neurons per layer, a bias added in each neuron and millions of weights, and they average the loss over a batch of examples. The rule at each step is the same: multiply the upstream gradient by the local slope. Backpropagation is reverse-mode automatic differentiation: one backward pass costs a small, fixed multiple of one forward pass, however many weights there are (Baydin et al., 2018).