A hallway light has two switches, one at each end. The light is on when exactly one switch is up. A single neuron can never learn this rule. Give it a hidden layer of two neurons and it can, and every number is still small enough to check by hand.
Green = numbers flowing forward. Red dashed = blame flowing backward. Yellow = being computed now. A dotted outline means the neuron's ReLU gate is closed (its sum was 0 or less), so it passes nothing forward and receives no blame.
Keyboard: → or space for Next, ← for Back.
The network outputs a number; anything above 0.5 counts as "on". The rule says on when exactly one switch is up.
A single neuron adds up weighted inputs and bends the result once. Flipping switch A adds the same amount, w₁, whether switch B is up or down. So flipping A must always push the answer the same way.
But the hallway rule says: with B down, raising A turns the light on; with B up, raising A turns it off. One weight cannot point both ways. In example 1 this could not happen, because fruit prices simply add up.
Two hidden neurons fix it. Each can watch for a different situation, and the output neuron combines what they report. Train the network and look at what the hidden neurons become: one ends up near ReLU(A − B), the other near ReLU(B − A), and the output adds them. That is |A − B|, which is exactly the rule.
Start: hidden 1 watches only switch A (w₁₁ = 1), hidden 2 watches only switch B (w₂₂ = 1), the output adds them (v₁ = v₂ = 1). All biases 0. Learning rate η = 0.1. Parameters are rounded to 4 decimals after each update.
| # | A | B | light | network |
|---|
The four rows are the whole training set. The network cycles through them in this order. One pass over all four is one epoch. The first row is where "just add the switches" goes wrong, so training starts there.
| step | ŷ | loss | h₁ weights | h₂ weights | output |
|---|
Nothing trained yet. Weights shown are the values after each update, as (w·x₁, w·x₂, bias).
z₁ = w₁₁·x₁ + w₁₂·x₂ + b₁ h₁ = ReLU(z₁)
z₂ = w₂₁·x₁ + w₂₂·x₂ + b₂ h₂ = ReLU(z₂)
zₒ = v₁·h₁ + v₂·h₂ + c ŷ = ReLU(zₒ)
L = ½ (ŷ − y)²
δₒ = (ŷ − y) · ReLU′(zₒ)
∂L/∂v₁ = δₒ·h₁ ∂L/∂v₂ = δₒ·h₂ ∂L/∂c = δₒ
δ₁ = δₒ · v₁ · ReLU′(z₁)
δ₂ = δₒ · v₂ · ReLU′(z₂)
∂L/∂w₁₁ = δ₁·x₁ ∂L/∂w₁₂ = δ₁·x₂ ∂L/∂b₁ = δ₁
∂L/∂w₂₁ = δ₂·x₁ ∂L/∂w₂₂ = δ₂·x₂ ∂L/∂b₂ = δ₂
every parameter: p ← p − η · ∂L/∂p
η = 0.1
The new idea versus example 1 is δ₁ and δ₂: the output's blame travels back along the same weight that carried the signal forward, and a closed ReLU gate (ReLU′ = 0) blocks it.