Lesson 3.2 · 8 min read · about 55 min to master at a very slow pace
Solving XOR with a Hidden Layer
An explicit 2–2–1 network that computes XOR, checked for all four inputs. The hidden layer changes the representation so that one output neuron can separate the classes.
The network
Two inputs, two hidden neurons, one output, all with the sign activation:
Hidden unit 1 computes : that's OR. Hidden unit 2 computes : that's AND. The output computes : "OR and not AND", which is XOR.
| Input | Hidden pre-activations | Hidden outputs | Output | XOR |
|---|---|---|---|---|
| (0, 0) | (−0.5, −1.5) | (−1, −1) | −1 + 1 − 1 = −1 | −1 ✓ |
| (1, 0) | (0.5, −0.5) | (+1, −1) | 1 + 1 − 1 = +1 | +1 ✓ |
| (0, 1) | (0.5, −0.5) | (+1, −1) | 1 + 1 − 1 = +1 | +1 ✓ |
| (1, 1) | (1.5, 0.5) | (+1, +1) | 1 − 1 − 1 = −1 | −1 ✓ |
What the hidden layer did
The hidden layer maps the four inputs to new coordinates in which the classes are linearly separable. Both positive inputs even land on the same point.
A common misstatement
"The hidden layer works by mapping to a higher dimension." Not here: the hidden layer has 2 units, the same as the input. It's the non-linear mapping that makes the classes separable, not extra dimensions.
Stuck? Common doubts
Questions real learners asked while studying this lesson. They stay closed so they don't spoil your own thinking: open only the ones you need.
First passWould gradient descent find these exact weights?
Not with sign activations: their derivative is zero almost everywhere, so there is no gradient to follow (Section 4). With sigmoid units, training finds some solution, usually a different one, and not always: with only 2 hidden units, a fair share of random starts get stuck. With 3 or more hidden units training succeeds far more reliably.
First passWhere do the thresholds 0.5 and 1.5 come from?
Look at the sum for the four inputs: it's 0, 1, 1 or 2.
- OR should fire when the sum is 1 or more, so put its threshold between 0 and 1: 0.5.
- AND should fire only when the sum is 2, so put its threshold between 1 and 2: 1.5.
Any value inside those gaps works; the middle of the gap is the safest choice. The output works the same way: is 0 for the negatives and 2 for the positives, so its threshold sits in the middle, at 1 (the bias ).
First passIs there an easy way to remember this network?
XOR = OR and not AND. The hidden layer computes OR and AND; the output says "yes" when OR is on and AND is off.
In the input square, OR and AND are two parallel lines. The two positive points, (1,0) and (0,1), lie in the strip between them. A single neuron can draw only one line, which is why it needs the hidden layer to draw the second.
Cheatsheet
XOR with a hidden layer
Check your understanding
Score 75% or more to master this lesson and earn XP. Answer first, then check.