PERCEPTRONS & LINEAR SEPARABILITY

Why one neuron can't
learn XOR

A single perceptron can only draw one straight line through its input space. That's enough for AND and OR — but not for XOR. Here's why, and how stacking a second layer of neurons fixes it.

01 — THE UNIT

What a perceptron actually does

A perceptron takes inputs x1, x2, multiplies each by a learned weight, adds a bias, and passes the result through a step function. If the weighted sum clears the threshold, it fires a 1. Otherwise, 0.

y = step( w1·x1 + w2·x2 + b )

Geometrically, w1·x1 + w2·x2 + b = 0 is a straight line. The perceptron fires on one side of that line and stays silent on the other. Training just moves the line around — it can never bend it.

Single unit

One line, two sides

x1 x2 Σ y

Two inputs, one summing unit, one output. This is the entire architecture — and its entire limitation.

02 — THE TRUTH TABLES

AND and OR are separable. XOR isn't.

Plot the four input combinations on a grid and color them by their correct output. If a single straight line can separate the red points from the teal points, a perceptron can learn it.

output = 0 output = 1 a valid separating line

Notice the pattern for XOR: the two "1" points sit on opposite corners of the square, diagonal from each other, and so do the two "0" points. Any straight line that separates one diagonal pair also has to cut through the other. There is no rotation or position for a single line that works — XOR is not linearly separable.

03 — PROVE IT YOURSELF

Try to fit one perceptron to XOR

Drag the weights and bias below. The line is the perceptron's entire decision boundary. Watch the score — see if you can ever reach 4 out of 4.

Decision boundary
Real-time Calculation
Point w1·x1 w2·x2 + b Sum Prediction Target Result

Go on, try every combination that feels right. The best any single line can do on XOR is 3 out of 4 — one corner is always misclassified, because it's diagonal from its own kind and neighboring its opposite.

04 — REVERSE-ENGINEERING THE FIX

Don't guess the weights. Derive them from the truth table.

Instead of pulling numbers out of thin air, work backward from what XOR actually means logically, then convert each logical piece into a perceptron.

Step 1 — decompose XOR into simpler gates. Stare at the truth table long enough and a pattern falls out: XOR is true exactly when the inputs are different, which is the same as saying "at least one is on, but not both."

XOR(x1,x2) = OR(x1,x2) AND NOT·AND(x1,x2)

That's two gates you already know are linearly separable (OR, AND) combined by a third linear decision. Three perceptrons, none of which individually need to solve XOR.

Step 2 — derive each hidden neuron's line from its own truth table. For OR, you need a line where (0,0) is alone on the "off" side. Any line between the origin and the other three points works — the simplest is x1+x2 = 0.5. For AND, you need (1,1) alone on the "on" side — x1+x2 = 1.5 does that.

h1 = step(x1 + x2 − 0.5)  ·  h2 = step(x1 + x2 − 1.5)

Step 3 — solve the output layer the same way you'd solve any single perceptron. Once h1 and h2 exist, the output's job is just h1 AND NOT h2 — itself a 2-input truth table over (h1, h2) instead of (x1, x2). It has exactly one truth table row, so any weights that make h1 − h2 cross zero only at that row will do.

y = step(h1 − h2 − 0.5)
Check by substitution
x1,x2h1h2yXOR
0,00000 ✓
1,01011 ✓
0,11011 ✓
1,11100 ✓

Every row checks out by hand — no training loop needed. This is the same solution the network below draws, just arrived at by reasoning instead of gradient descent.

Step 3.5 — see the wiring. Three perceptrons, each with its own inputs, weights, and bias — laid out exactly as derived above. Every perceptron is really two stages: a weighted sum (Σ), then a step activation (small circle) that turns it into a hard 0/1.

Derived architecture
x1 x2 Σ w1=1 w2=1 b=−0.5 h1 (≈OR) Σ b=−1.5 h2 (≈AND) Σ w1=+1 w2=−1 b=−0.5 y = h1 AND NOT h2 XOR

Each pair — a large Σ circle plus the small circle right after it — is one perceptron: the sum happens in Σ, the small circle applies step() and produces the actual 0/1 that travels to the next layer. Same two inputs feed both hidden perceptrons in parallel; only h1 and h2's activated outputs, never x1, x2 directly, reach the final perceptron.

A note on step(). This is the activation function sitting inside every perceptron call above. It's a hard threshold, not a smooth curve: any sum below zero collapses straight down to 0, and zero-or-above is what lets the neuron fire and pass through a 1.

step(z) = 0  if z < 0   ·   step(z) = 1  if z ≥ 0

In other words: crossing the line is binary, not gradual. A neuron doesn't "half-fire" — it's either on the firing side of its boundary (1) or strictly on the other side of it (0). That all-or-nothing gate is what turns a continuous weighted sum into the clean logical AND / OR / NOT behaviour each perceptron above is standing in for.

Step 4 — write it down. Three calls to the same tiny perceptron function, wired together exactly as derived above:

python
def step(z):
    return 1 if z >= 0 else 0

def perceptron(x1, x2, w1, w2, b):
    return step(w1 * x1 + w2 * x2 + b)

def xor_mlp(x1, x2):
    h1 = perceptron(x1, x2, w1=1, w2=1, b=-0.5)   # acts like OR
    h2 = perceptron(x1, x2, w1=1, w2=1, b=-1.5)   # acts like AND
    y  = perceptron(h1, h2, w1=1, w2=-1, b=-0.5)  # h1 AND NOT h2
    return y

for x1 in (0, 1):
    for x2 in (0, 1):
        print(f"XOR({x1},{x2}) = {xor_mlp(x1, x2)}")

Output: XOR(0,0)=0, XOR(1,0)=1, XOR(0,1)=1, XOR(1,1)=0 — no single perceptron call ever sees the full XOR problem; each only ever solves a linearly separable sub-problem.

05 — THE FIX, VISUALIZED

Two lines fold the space. One more neuron finishes the job.

A single perceptron can't bend space, but a hidden layer of two perceptrons can each draw their own line — and a third perceptron can combine their outputs. Together they carve out a region no single line could isolate.

Network with one hidden layer
x1 x2 h1 ≈ OR h2 ≈ AND y h1 AND NOT h2

Hidden neuron h1 fires like OR. Hidden neuron h2 fires like AND. The output neuron fires only when h1 is on and h2 is off — catching the two points that are "OR but not AND."

Folded decision region

The two hidden lines (teal and red) slice the plane into a band. The output neuron lights up only inside that band — exactly where XOR's two "1" points live, and nowhere else.

This is the general idea behind multilayer perceptrons: each hidden neuron still only draws a straight line, but the next layer can combine several of those lines with logical operations like AND / OR / NOT.

06 — SEE THE FOLD IN 3D

The output neuron's raw sum, lifted into the third dimension

Flatten the step functions away for a moment and just plot the output neuron's pre-activation value — h1 − h2 − 0.5 — as a height above each point in the x1–x2 plane. Where that height is above zero, the network fires 1. The two hidden biases b1, b2 control where the two cuts happen; drag the view around to see why only certain values raise the correct two corners above the plane.

Drag to orbit
below zero → output 0 above zero → output 1

At b1 = −0.5, b2 = −1.5 the surface forms a raised plateau sitting only over (1,0) and (0,1), with the other two corners sitting in the lower terrace. Drag either bias slider and watch the plateau slide until it swallows the wrong corners — or disappears entirely.

07 — KNOWLEDGE CHECK

Test Your Understanding: XOR & Perceptrons

Five medium-difficulty multiple-choice questions on linear separability, perceptrons, and multilayer solutions. Choose the best answer for each.