Chapter 8: Hidden neurons fix it
Add a middle layer and the impossible becomes easy.
The fix turned out to be: use MORE neurons, arranged in layers. A layer is a group of neurons that all work at the same stage, side by side.
Put a few neurons in the middle of the network. They are called hidden neurons, for a reason that is coming up. Each one draws its own line on the graph paper. Then an output neuron looks at what the hidden neurons said and combines their answers.
Here is the trick on the same graph paper as in chapter 7. One line couldn't split the XOR corners, but two lines together can fence off the diagonal stripe where the YES dots live. Each hidden neuron draws one of the lines:
Hidden neuron 1 asks "is at least one button pressed?" That is everything above the blue line. Hidden neuron 2 asks "is it NOT both buttons?" That is everything below the green line. The output neuron then asks the easiest question of all: "did BOTH hidden neurons say yes?" The only dots that pass both tests are the two YES corners, in the stripe between the lines. Three easy straight-line questions, chained together, make a shape that no single line could ever make.
The picture needed two hidden neurons. The network you are about to train has four, wired like this:
Why hidden? Because nobody tells these neurons what they should mean. The training data has an answer column for the output and nothing at all for the middle. Each hidden neuron finds its own job during training. Afterwards you might peek inside and find something like this:
- Hidden neuron 1 learned to mean "left button AND NOT right button"
- Hidden neuron 2 learned to mean "right button AND NOT left button"
- The output neuron learned: "fire if either hidden neuron fires"
Nobody told the network those rules. It discovers them by being a tiny bit less wrong over and over. Notice that this is a different solution from the stripe picture: there, the lines fenced off the middle; here, each hidden neuron fences off one YES corner and the output ORs them together. There are several ways to carve out those corners, and training can land on any of them. With four hidden neurons the jobs are usually messier than either story, shared out and overlapping.
The lone neuron stored 3 numbers. In this network every wire is a weight, and each hidden neuron and the output neuron has a bias of its own. How many numbers does it store?
8 wires from the inputs to the hidden layer, 4 wires from the hidden layer to the output, and 5 biases: 8 + 4 + 5 = 17. The two inputs are not neurons. They are just the numbers going in, so they have no bias.
Now watch it succeed
Same XOR data that defeated the lone neuron. New network: 4 hidden neurons and 1 output neuron, starting from random weights. Press "train 1,000 steps" and watch the four predictions. If any of them still has a ✗, press it again.
The bigger model
17 stored numbers instead of 3: 8 input→hidden weights, 4 hidden biases, 4 hidden→output weights, 1 output bias. More neurons means a longer list. It is still nothing but numbers being rewritten.
What just happened
The puzzle that no amount of training could teach one neuron is now solved, usually in a single press. We tried 2,000 random starts: 93% had all four right after 1,000 steps, and every one of them had after 3,000. By then the YES rows sit at about 0.96 and the NO rows at about 0.04, and they keep creeping toward 1 and 0 for as long as you train.
Press "reset weights" and train again, and compare the dark panel with last time. The predictions land on the same sides. The 17 numbers are completely different. Different random starts find different ways to carve up the graph paper.
Each hidden neuron draws its own line, and the output neuron combines them into shapes that no single line can make. Nobody tells the hidden neurons what to look for. Training finds jobs for them.
This is the central trick of deep learning: layers of neurons build up more and more sophisticated ideas without anyone programming those ideas in.
The piece of math that makes this work: the chain rule
How does the network know which way to nudge the hidden weights? With one neuron it was easy: compare its output with the right answer. But there is no right-answer column for a hidden neuron. It is buried in the middle. How do you blame it for a mistake?
The trick is a piece of calculus called the chain rule. Start at the output, where the error is in plain view, and work out how each output weight contributed to it, the same as in chapter 4. Then push the blame backward: each hidden neuron gets a share of the error in proportion to how strongly it is wired to the output. Now every hidden neuron has an error of its own, and its weights can be nudged in the usual way.
"Work out the error, then pass the blame backward, layer by layer" is called backpropagation. It is the method from the 1986 paper in chapter 7, and both widgets on this page run it on every step. (For a beautifully drawn explanation, see Chris Olah's "Calculus on Computational Graphs: Backpropagation".) In chapter 14 you will step through the whole thing yourself, with real numbers.
The network below starts from fresh random weights. Press "train 200 (fast)" once, then click through the four examples. What will its predictions look like after those 200 steps?
For the first few hundred steps this network looks every bit as stuck as the single neuron was. Then the hidden neurons start to pull apart into different jobs, and the error collapses. Keep pressing and watch for the moment it breaks free.
Watch the wires light up
Same kind of network, but now you can see inside it. Pick which example to feed in. The lines are the weights: thick blue is strong positive, thick red is strong negative, thin is weak. "Step + animate" shows the signal flowing forward and then trains for one round. "Train 200 (fast)" does 200 rounds at once.
Output math
What just happened
After the first 200 steps, the predictions are almost always still hugging 0.5. Then, usually on the third or fourth press, the wires thicken, the hidden neurons stop agreeing with each other, and the predictions pull away to the correct sides. In 3,000 test runs, all four were on the correct side by the fifth press 93% of the time, and the unluckiest start needed 26 presses.
Learning is often like this: a long flat stretch where nothing seems to happen, then a sudden drop. The flat stretch is not wasted. The weights are drifting, a little at a time, toward a place where the hidden neurons can start to disagree.
Run a real 2-layer network on XOR
This is neuro3.py: the same XOR data, a hidden layer of 4 neurons, and 20,000 rounds of training. The "backward" part of the loop is the blame-passing described above. The predictions end at about 0.99 for YES and 0.01 for NO.
In the Python code, change the hidden layer from 4 neurons to 2. Does it still learn XOR? What happens if you try 1? Explain the result with lines on graph paper.
Why can a network with hidden neurons solve XOR when a single neuron cannot?
Every neuron in the network does the same multiply, add and squish. The power comes from combining several lines, not from better neurons or longer training.
Who decides what each hidden neuron ends up meaning?
The data only says what the final output should be. The middle of the network is left to sort itself out, which is why two trained copies can solve the same puzzle in different ways.
There is no "right answer" column for a hidden neuron. So how do its weights get nudged?
That backward pass is backpropagation. It lets every weight in the network, however deep, find out which way to move.