Hallway Light Network

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.

The network

1 switch A x₁ 1 switch B x₂ 1 always 1 (bias) hidden 1 hidden 2 1 always 1 (bias) output loss ? light y = 0

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.

FORWARD

Keyboard: → or space for Next, ← for Back.

Ask the network

→ light

The network outputs a number; anything above 0.5 counts as "on". The rule says on when exactly one switch is up.

Why one neuron cannot do this

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.

What the network has learned

hidden neuron 1 · z₁ = w₁₁·x₁ + w₁₂·x₂ + b₁
w₁₁
w₁₂
b₁
hidden neuron 2 · z₂ = w₂₁·x₁ + w₂₂·x₂ + b₂
w₂₁
w₂₂
b₂
output neuron · zₒ = v₁·h₁ + v₂·h₂ + c
v₁
v₂
c

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.

The rule, and what the network says

#ABlightnetwork

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.

Training log

stepŷlossh₁ weightsh₂ weightsoutput

Nothing trained yet. Weights shown are the values after each update, as (w·x₁, w·x₂, bias).

Cheat sheet

Forward pass

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)²

Backward pass (chain rule)

δₒ = (ŷ − 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₂ = δ₂

Update (gradient descent)

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.