1y, z, h = forward(p, x)2loss = (y - t) ** 2 / 23g = backward(p, x, t)4p = update(p, g, lr)x10 xor 000 xor 111 xor 011 xor 10h1x2yh2loss
Khaki: inputs · teal: hidden neurons (ReLU) · blue: output · salmon: lossLinks with slate tags: the weights the network learnsYellow pulses: signals forward · salmon pulses: the error flowing backA network of two inputs, two hidden ReLU neurons and one output, joined by six weighted links. At the back, the four-line training loop and the four XOR examples it learns from. Forward, signals carry (1, 1) through the weights and the network answers 1 where XOR says 0. The loss measures the miss; backpropagation sends the error back along the links; each weight takes a small step against its gradient. Repeated for 200 passes over the examples, the loss falls to 0.000047 and the network computes XOR. The starting weights are the XOR solution from the Deep Learning book (section 6.1) with one output weight changed from −2 to −1, so the numbers stay simple. Real networks start from small random weights.

The network

  1. A neural network wires simple neurons together in layers. Ours has an input layer of 2, a hidden layer of 2 and an output layer of 1. Each link carries a weight: the signal travelling along it is multiplied by that number.

Forward pass

Learning

What happens

;; step 1/10 · layers
input layer    x1, x2         the numbers we feed in
hidden layer   h1, h2         neither input nor answer
output layer   y              the network's answer

each neuron:   sum of (input x weight) + bias,
               then its activation function
links:         6 weights, plus 3 biases (h1, h2, y)

The starting weights are the XOR solution from the Deep Learning book (section 6.1) with one output weight changed from −2 to −1, so the numbers stay simple. Real networks start from small random weights. This network updates after every example (stochastic gradient descent). Large networks usually average the gradient over a small batch of examples first.