Demos

A network that learns XOR

A small neural network in your browser. Press start and watch it adjust its weights with backpropagation until it can do XOR.

Step 0, loss 0.2575.

InputHidden layerOutputWeight 0.25Weight -0.99Weight 0.05Weight 0.96Weight 0.94Weight -0.44Weight 0.99Weight -0.09Weight -0.020.00x11.00x20.32b 0.230.80b 0.440.36b -0.150.38b -0.72
positive weightnegative weightthicker is a larger weight

The four examples

x1x2TargetOutputShow
0000.44 correct
0110.38 still wrong
1010.45 still wrong
1100.39 correct

What the network thinks

output above 0.5output below 0.5 Every point in the square is an input: x1 to the right, x2 upwards. The dots are the four examples.

The loss per step

10.10.010.0010.000103000 steps

What XOR is

XOR is a rule with two inputs. The result is 1 when exactly one of the two is 1.

x1x2XOR
000
011
101
110

Why one neuron cannot do it

One neuron computes a weighted sum, z = x1·w1 + x2·w2 + b, and passes it through a sigmoid. All inputs for which z is exactly 0 lie on a straight line. On one side of that line the output is above 0.5, on the other side below.

In XOR the two ones sit diagonally opposite each other, at (0, 1) and (1, 0). No straight line separates them from (0, 0) and (1, 1). That is why there is a hidden layer in between. Each hidden neuron draws its own line, and the output neuron combines those lines. You can see it in the map above: the area becomes a band instead of a half.

How it learns

Every step goes like this:

  1. Forward. For each of the four examples: weighted sum and sigmoid in the hidden layer, then the same in the output neuron.
  2. The loss. Per example (output - target)², averaged over the four. That is the number in the chart.
  3. Backward. With the chain rule, each weight gets a number for how the loss changes when that weight grows a tiny bit. Through a sigmoid that is always output · (1 - output).
  4. Adjust. Every weight moves a small step against that direction: weight = weight - learning rate · gradient.

Use "One step" to watch a single round. With 2 hidden neurons the network sometimes gets stuck, depending on the start weights. Press "Reset" then.