Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 4: Neural Network Foundations

The perceptron learning rule and the XOR problem

P4.235 min●●○ IntermediateReviewed Dec 2, 2026

Why this matters for machine learning

The perceptron rule is one of the oldest learning algorithms for a neuron, and it is stochastic gradient descent in disguise. The XOR failure is the classic reason networks have hidden layers.

  • The update w ← w + η·y·x is a gradient-descent step on a simple loss, so the same loop (look at an example, nudge the weights) trains every neural network. (P4.13: Optimizers)
  • Fixing XOR needs hidden layers, which bend the plane until the classes can be split by a line. (P4.3: Multi-layer perceptrons)
  • Lifting data with an extra feature such as x₁x₂ is the idea behind the kernel trick. (P3.B7: The kernel trick)
  • The perceptron asks only for a correct side. A support vector machine asks for the widest empty street, and the lesson compares the two losses. (P3.B6: Support Vector Machines)

Before this: From neurons to the perceptron, Dot product and cosine similarity, Gradient descent

After this lesson you can

  • Apply the perceptron learning rule by hand, in both the ±1 and the 0/1 forms, and explain why each update moves the line toward the mistake
  • Run the rule on a small data set until an epoch has no mistakes, and say what the learning rate and the starting weights do
  • State what the convergence theorem guarantees and what it leaves open, and define linear separability
  • Prove that no line separates XOR, explain what the rule does there, and name the standard remedies
  • Describe the rule as stochastic gradient descent on max(0, −y·(w·x + b)), and place the 1969 book Perceptrons in its history

In P4.1 you set the weights of a perceptron for AND and OR by hand. That worked because the pictures were tiny. Real problems have thousands of inputs, and nobody can set those weights by hand.

So can the perceptron find its own weights? In the late 1950s Frank Rosenblatt showed that it can, with a rule so simple that it fits on one line: look at an example, and if the perceptron is wrong, nudge the weights toward the right answer. This lesson explains the rule, proves it works when it can, and then meets the four-point problem on which it fails for ever.

Learn from mistakes

Remember the perceptron from P4.1. It computes a score z=w⋅x+bz = \mathbf{w}\cdot\mathbf{x} + b. In P4.1 the step gave 11 when z≥0z \ge 0. For learning, this lesson uses the labels +1+1 and −1-1 and counts a score of exactly 00 as undecided (the tie convention below), so the answer is +1+1 when z>0z > 0 and −1-1 when z<0z < 0. The line w⋅x+b=0\mathbf{w}\cdot\mathbf{x} + b = 0 is its decision boundary, and w\mathbf{w} points at the side that gets +1+1.

Now suppose you have training examples with known labels yy, each +1+1 or −1-1, and you start with some weights. Go through the examples one at a time:

  • If the perceptron answers correctly, do nothing.
  • If it answers wrongly, change w\mathbf{w} and bb so that this example is a little closer to being right.

That is the whole idea. The rest of the lesson makes “a little closer” exact.

The rule

For one example (x,y)(\mathbf{x}, y) with y∈{+1,−1}y\in\{+1,-1\} and a learning rate η>0\eta>0 (the step size, as in P1.F3):

if y (w⋅x+b)≤0:w←w+η y x,b←b+η y.\begin{aligned} &\text{if } y\,(\mathbf{w}\cdot\mathbf{x} + b)\le 0:\\ &\quad \mathbf{w}\leftarrow\mathbf{w} + \eta\,y\,\mathbf{x},\qquad b\leftarrow b + \eta\,y . \end{aligned}

The product y zy\,z is positive when the answer is right and negative when it is wrong, so “≤0\le 0” means “wrong”. A score of exactly 00 is undecided, so the rule treats it as wrong for either label. This is a convention, and it matters: from the start w=0\mathbf{w}=\mathbf{0}, b=0b=0 every score is exactly 00, and the rule could never begin without it. scikit-learn’s Perceptron uses the same convention.

Read the update in words. For a positive example you add a copy of x\mathbf{x} to w\mathbf{w}, so w\mathbf{w} turns toward the point. For a negative example you subtract it, so w\mathbf{w} turns away. The bias is the weight of an input that is always 11, so it moves by ηy\eta y.

The 0/1 form. Many books use targets t∈{0,1}t\in\{0,1\}, an output o=step(z)∈{0,1}o=\text{step}(z)\in\{0,1\}, and the update w←w+η (t−o) x\mathbf{w}\leftarrow\mathbf{w}+\eta\,(t-o)\,\mathbf{x}, b←b+η (t−o)b\leftarrow b+\eta\,(t-o). It is the same rule. The error t−ot-o is 00 when the answer is right, +1+1 when the target is 11 and the output 00, and −1-1 in the opposite case. On a mistake, t−ot-o equals yy (with y=2t−1y=2t-1), so the two updates are identical. The two forms differ only on ties, where z=0z=0 exactly: the step function must give a tie one value, while the rule above counts a tie as wrong for both labels. This lesson uses the ±1\pm1 form and the tie convention above throughout.

One update, with numbers

Let w=(0,0)\mathbf{w}=(0,0), b=−1b=-1 and η=1\eta=1, and take the example x=(0,1)\mathbf{x}=(0,1) with y=+1y=+1. The score is 0⋅0+0⋅1−1=−10\cdot0+0\cdot1-1=-1, so y z=−1≤0y\,z=-1\le0: a mistake. The update gives w=(0,0)+1⋅(+1)⋅(0,1)=(0,1)\mathbf{w}=(0,0)+1\cdot(+1)\cdot(0,1)=(0,1) and b=−1+1=0b=-1+1=0. The new score for this example is 0⋅0+1⋅1+0=10\cdot0+1\cdot1+0=1. It was −1-1 and is now +1+1, so this example is now right.

Why the update moves the line the right way

After the update on a mistake, the score of the same example changes by

znew−zold=η y (∥x∥2+1).z_{\text{new}} - z_{\text{old}} = \eta\,y\,\bigl(\lVert\mathbf{x}\rVert^2 + 1\bigr).

The new score is (w+ηyx)⋅x+(b+ηy)(\mathbf{w}+\eta y\mathbf{x})\cdot\mathbf{x}+(b+\eta y), and subtracting the old score leaves ηy x⋅x+ηy\eta y\,\mathbf{x}\cdot\mathbf{x} + \eta y. The factor ∥x∥2+1\lVert\mathbf{x}\rVert^2+1 is positive, so for a positive example the score goes up and for a negative example it goes down. That is always the direction that makes this example more correct. In the example above it rose from −1-1 to 11, a change of 2=∥(0,1)∥2+12=\lVert(0,1)\rVert^2+1.

Geometrically, the boundary is the set of points with score 00. Raising the score at x\mathbf{x} pulls the boundary toward the point x\mathbf{x}, or pushes it past the point, depending on how far away it was.

The last sentence is the catch. The rule only looks at one example, and the nudge that fixes it can break another. Whether this process ends up somewhere good is a real question, and a theorem answers it below.

A full run by hand: learning OR

Take the four points of OR, in this fixed order, with labels +1+1 or −1-1: (0,0)↦−1(0,0)\mapsto-1, (0,1)↦+1(0,1)\mapsto+1, (1,0)↦+1(1,0)\mapsto+1, (1,1)↦+1(1,1)\mapsto+1. Start from w=(0,0)\mathbf{w}=(0,0), b=0b=0, with η=1\eta=1. One pass through the four points is an epoch. Here is every step, with the score each example gets before the update.

Epoch 1.

  • (0,0)(0,0), y=−1y=-1: score 00, a tie, so a mistake. Subtract: w=(0,0)\mathbf{w}=(0,0), b=−1b=-1.
  • (0,1)(0,1), y=+1y=+1: score −1-1, a mistake. Add: w=(0,1)\mathbf{w}=(0,1), b=0b=0.
  • (1,0)(1,0), y=+1y=+1: score 00, a mistake. Add: w=(1,1)\mathbf{w}=(1,1), b=1b=1.
  • (1,1)(1,1), y=+1y=+1: score 33, correct.

Three mistakes. Now w=(1,1)\mathbf{w}=(1,1), b=1b=1, which says “output +1+1 when x1+x2+1>0x_1+x_2+1>0”. That gives +1+1 for all four points, so (0,0)(0,0) is wrong.

Epoch 2. (0,0)(0,0) has score 11 but y=−1y=-1: a mistake, so b=0b=0. The other three points have scores 11, 11, 22, all correct. One mistake. Now w=(1,1)\mathbf{w}=(1,1), b=0b=0.

Epoch 3. (0,0)(0,0) has score 00: a mistake, b=−1b=-1. (0,1)(0,1) has score 1−1=01-1=0: a mistake, so w=(1,2)\mathbf{w}=(1,2), b=0b=0. The last two are correct. Two mistakes.

Epoch 4. (0,0)(0,0) has score 00: a mistake, b=−1b=-1. (0,1)(0,1) has score 2−1=12-1=1, correct. (1,0)(1,0) has score 1−1=01-1=0: a mistake, so w=(2,2)\mathbf{w}=(2,2), b=0b=0. (1,1)(1,1) is correct. Two mistakes.

Epoch 5. (0,0)(0,0) has score 00: a mistake, b=−1b=-1. The other three have scores 11, 11, 33. One mistake. Now w=(2,2)\mathbf{w}=(2,2), b=−1b=-1.

Epoch 6. The scores are −1-1, 11, 11, 33 with labels −1-1, +1+1, +1+1, +1+1. All correct. Zero mistakes, so the rule stops.

The mistakes per epoch were 3,1,2,2,1,03, 1, 2, 2, 1, 0: nine updates in all. The result is w=(2,2)\mathbf{w}=(2,2), b=−1b=-1: the perceptron outputs +1+1 when 2x1+2x2>12x_1+2x_2>1. That is OR, found without anyone setting a weight. Running the code in the Code tab below, and the widget in the next section, give the same numbers. AND from the same start takes more work: 99 epochs and 1818 updates, ending at w=(3,2)\mathbf{w}=(3,2), b=−4b=-4.

See it: the rule on five data sets

−0.200.20.40.60.811.2−0.200.20.40.60.811.2x₁x₂

■ label +1, ● label −1. × marks a point the current line gets wrong (a score of exactly 0 counts as wrong). The black ring is the next example. The green side is where w·x + b > 0, and the arrow shows the direction of w.

w₁: 0
w₂: 0
b: 0
epochs done: 0
updates: 0
wrong now: 4 of 4

Mistakes in each finished epoch

04epoch

Press Step to look at the first example, or Epoch to run a whole pass.

The perceptron rule on small 2-D data sets. Step looks at one example (the black ring) and updates on a mistake. Epoch finishes a whole pass. Play runs one example at a time (in Lite Mode it runs up to 10 whole epochs at once). The green line is the boundary, the arrow is the direction of w, and the chart counts the mistakes in each finished epoch. A green diamond in the chart means an epoch with no mistakes. Pick XOR and watch the chart: the bars never reach zero.

Try it.

  1. Pick OR, press Step three times, and compare with the first epoch of the hand run above. Then press Reset and Epoch five times to see w=(2,2)\mathbf{w}=(2,2), b=−1b=-1.
  2. Pick the random separable set and press Epoch. It needs 3 epochs, with 77, 22 and 00 mistakes.
  3. Pick XOR and press Epoch a few times. Every epoch has 44 mistakes, and the status tells you the weights have come back to where they were.
  4. Pick Noisy (the separable set with two labels flipped) and keep pressing Epoch. The mistakes bounce around and never settle at 00.
  5. Pick AND and set the learning rate to 0.50.5, then to 22. The mistakes per epoch stay the same. Press “Start at w = (1, −1), b = 0.5”. With η=1\eta=1 the run stops after 7 epochs. Set η=0.1\eta=0.1, press Reset and then Epoch repeatedly: it takes 20.

Epochs, stopping, the learning rate and the start

Epochs and stopping. The rule keeps passing over the data. It stops when one complete epoch has no mistakes: then every example is right and no update is ever triggered again, so nothing changes. Examples are usually shuffled between epochs in real code. This lesson keeps a fixed order so every run can be reproduced.

The learning rate η\eta does very little here, from a zero start. The mistakes on the first epoch depend only on signs. Start at w=0\mathbf{w}=\mathbf{0}, b=0b=0, and every update adds η⋅y x\eta\cdot y\,\mathbf{x} to w\mathbf{w}. After any number of updates, w\mathbf{w} and bb are η\eta times what they would be with η=1\eta=1, so every score is η\eta times larger. Multiplying scores by a positive number changes no signs and no ties, so the same examples are mistakes, and the boundary is exactly the same line. Running AND with η=0.5\eta=0.5 gives w=(1.5,1)\mathbf{w}=(1.5,1), b=−2b=-2 and the same mistakes per epoch as η=1\eta=1. So with the perceptron rule from zero, η=1\eta=1 is as good as any value, and you can pick it.

The starting weights matter. The argument breaks when the start is not zero, because the starting weights do not scale with η\eta. Starting AND at w=(1,−1)\mathbf{w}=(1,-1), b=0.5b=0.5, the rule with η=1\eta=1 stops after 7 epochs, and with η=0.1\eta=0.1 it takes 20. A small step now fights a start that was not chosen for this data. Both runs still stop. (The theorem below is stated for a zero start. The same proof gives a bound for any start, with an extra term for how far the start is from a solution.)

Linear separability and the convergence theorem

Two classes are linearly separable if some line (in more dimensions, some plane or hyperplane) has all the +1+1 points strictly on one side and all the −1-1 points strictly on the other: there are w\mathbf{w} and bb with yi(w⋅xi+b)>0y_i(\mathbf{w}\cdot\mathbf{x}_i+b)>0 for every example. AND and OR are separable. XOR, below, is not.

Rosenblatt proved convergence in the early 1960s. The clean bound below is Novikoff’s (1962).

Here x~=(x,1)\tilde{\mathbf{x}}=(\mathbf{x},1) is the input with the extra 11. The bound depends on how hard the data is (a small margin means a big bound) and not directly on how many examples there are or how many features (the features enter only through RR and γ\gamma).

For OR: R=3R=\sqrt3 (the point (1,1,1)(1,1,1)) and the best u\mathbf{u} has γ=1/3\gamma=1/3, so the bound is (3/(1/3))2=27(\sqrt3/(1/3))^2=27. The hand run needed 99 updates. For AND the bound is 5151 and the run needed 1818. The bound is a safety guarantee, and real runs are usually far below it.

What the theorem does not promise.

  • Which line. Any separating line may come out. The rule stops as soon as the data is separated, with no preference for the middle of the gap. A support vector machine (P3.B6) chooses the widest street, and the perceptron does not try to.
  • Anything about data that is not separable. The theorem needs a separating u\mathbf{u} to exist. If it does not, the bound has no content, and the rule does something else, as the next sections show.
  • Speed. At most (R/γ)2(R/\gamma)^2 updates can be a very large number when γ\gamma is small.
Go deeper Proof of the bound

Take η=1\eta=1 (a different η\eta only rescales everything below, from a zero start). Write w~k\tilde{\mathbf{w}}_k for the weights, bias included, after kk updates, with w~0=0\tilde{\mathbf{w}}_0=\mathbf{0}. At update kk the example (x~,y)(\tilde{\mathbf{x}},y) was a mistake, so y w~k−1⋅x~≤0y\,\tilde{\mathbf{w}}_{k-1}\cdot\tilde{\mathbf{x}}\le0, and w~k=w~k−1+yx~\tilde{\mathbf{w}}_k=\tilde{\mathbf{w}}_{k-1}+y\tilde{\mathbf{x}}.

It aligns with u\mathbf{u}. w~k⋅u=w~k−1⋅u+y x~⋅u≥w~k−1⋅u+γ\tilde{\mathbf{w}}_k\cdot\mathbf{u}=\tilde{\mathbf{w}}_{k-1}\cdot\mathbf{u}+y\,\tilde{\mathbf{x}}\cdot\mathbf{u}\ge\tilde{\mathbf{w}}_{k-1}\cdot\mathbf{u}+\gamma. After kk updates, w~k⋅u≥kγ\tilde{\mathbf{w}}_k\cdot\mathbf{u}\ge k\gamma.

It grows slowly. ∥w~k∥2=∥w~k−1∥2+2y w~k−1⋅x~+∥x~∥2≤∥w~k−1∥2+R2\lVert\tilde{\mathbf{w}}_k\rVert^2=\lVert\tilde{\mathbf{w}}_{k-1}\rVert^2+2y\,\tilde{\mathbf{w}}_{k-1}\cdot\tilde{\mathbf{x}}+\lVert\tilde{\mathbf{x}}\rVert^2\le\lVert\tilde{\mathbf{w}}_{k-1}\rVert^2+R^2, because the middle term is not positive on a mistake. So ∥w~k∥2≤kR2\lVert\tilde{\mathbf{w}}_k\rVert^2\le kR^2.

Put them together. Since u\mathbf{u} has length 11, kγ≤w~k⋅u≤∥w~k∥≤k Rk\gamma\le\tilde{\mathbf{w}}_k\cdot\mathbf{u}\le\lVert\tilde{\mathbf{w}}_k\rVert\le\sqrt{k}\,R. Divide by k γ\sqrt k\,\gamma: k≤R/γ\sqrt k\le R/\gamma, so k≤(R/γ)2k\le(R/\gamma)^2. ■\blacksquare

The two facts pull against each other: the weights must grow at least in step with the number of updates in the direction of u\mathbf{u}, but their length can grow only like the square root. That squeeze can only hold for a limited number of updates. The “middle term is not positive” step is where a mistake is used, and the “≥γ\ge\gamma” step is where separability is used.

The γ\gamma here is measured with the bias counted as an input, so it is related to, but not identical to, the street width of P3.B6.

The XOR problem

XOR (“exclusive or”) is 11 when exactly one of the two inputs is 11: the points (0,1)(0,1) and (1,0)(1,0) have label +1+1, and (0,0)(0,0) and (1,1)(1,1) have label −1-1. Draw them on the unit square: the two +1+1 points sit on one diagonal, the two −1-1 points on the other.

No line can separate them. Suppose w=(w1,w2)\mathbf{w}=(w_1,w_2) and bb did. Each point gives one inequality y(w⋅x+b)>0y(\mathbf{w}\cdot\mathbf{x}+b)>0, which simplifies to:

(0,0), y=−1:  b<0(0,1), y=+1:  w2+b>0(1,0), y=+1:  w1+b>0(1,1), y=−1:  w1+w2+b<0\begin{aligned} (0,0),\ y=-1:&\ \ b<0\\ (0,1),\ y=+1:&\ \ w_2+b>0\\ (1,0),\ y=+1:&\ \ w_1+b>0\\ (1,1),\ y=-1:&\ \ w_1+w_2+b<0 \end{aligned}

Add the two +1+1 inequalities: w1+w2+2b>0w_1+w_2+2b>0, so w1+w2+b>−bw_1+w_2+b>-b. The first one says −b>0-b>0, so w1+w2+b>0w_1+w_2+b>0. The last line says w1+w2+b<0w_1+w_2+b<0. Contradiction, so no such w\mathbf{w} and bb exist. ■\blacksquare

What the rule does. The theorem needs a separating line, so it does not apply. From the zero start and the fixed order, the rule on XOR does this in every epoch:

  • (0,0)(0,0), y=−1y=-1: tie, mistake. w=(0,0)\mathbf{w}=(0,0), b=−1b=-1.
  • (0,1)(0,1), y=+1y=+1: score −1-1, mistake. w=(0,1)\mathbf{w}=(0,1), b=0b=0.
  • (1,0)(1,0), y=+1y=+1: score 00, mistake. w=(1,1)\mathbf{w}=(1,1), b=1b=1.
  • (1,1)(1,1), y=−1y=-1: score 33, mistake. w=(0,0)\mathbf{w}=(0,0), b=0b=0.

All four are mistakes, and the weights are back at w=(0,0)\mathbf{w}=(0,0), b=0b=0. The next epoch is the same, and so on for ever: a cycle of length one epoch. Press Epoch in the widget on XOR and the status says exactly this. In general a run on non-separable data does not have to return to the same weights so neatly. On the noisy set the mistakes per epoch go 7,6,1,2,2,5,4,4,4,4,2,27, 6, 1, 2, 2, 5, 4, 4, 4, 4, 2, 2 and never reach 00.

Go deeper Why a run on non-separable data cannot run away

On any finite data set the weights are known to stay bounded (the perceptron cycling theorem, Block and Levin, 1970), so the run cannot run away. With whole-number data, η=1\eta=1 and a fixed order, bounded whole-number weights have only finitely many values, so the run must eventually return to weights it has had before and then repeat for ever.

When the data is not separable: practical fixes

Real data is rarely perfectly separable, and a mislabelled example is enough to ruin it. A plain perceptron on such data keeps moving, and the weights it has when you stop it can be much worse than weights it had earlier. Two cheap fixes are common:

  • The pocket algorithm (Gallant, 1990): run the rule as usual, but keep “in your pocket” the best weights seen so far, and return those. The “ratchet” version, used in the cell below, judges “best” by how many training examples the weights get right.
  • Averaging (the averaged or voted perceptron, Freund and Schapire, 1999): return the average of the weights over all steps. Averaging damps the jumps from the last few mistakes.

Here is a quick test on the noisy set of the widget (14 points, 2 labels flipped), over 50 epochs. The code runs in your browser.

Try it: pocket and averaging on the noisy set

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

The output on the machine used for this lesson:

epoch 49: plain 12/14
epoch 50: plain 8/14
pocket : 13 / 14
average: 12 / 14

The plain rule’s weights after epoch 50 get only 8 of 14 right, though it had 12 of 14 a moment earlier. The pocket keeps 13 of 14. Checking every line through two of the points, each nudged slightly both ways, finds no line above 13 of 14, so the pocket’s 13 is as good as any line found. Averaging gives 12 of 14. These numbers are for this one data set and order. They show the idea, and they do not rank the methods in general.

The rule is stochastic gradient descent

Here is the link to P1.F3. For one example, define the perceptron loss

L(w,b)=max⁡(0, −y (w⋅x+b)).L(\mathbf{w},b)=\max\bigl(0,\,-y\,(\mathbf{w}\cdot\mathbf{x}+b)\bigr).

If the example is right, y z>0y\,z>0 and the loss is 00. If it is wrong, the loss is −y z>0-y\,z>0, which grows the further the score is on the wrong side. For a wrong example the loss equals −y (w⋅x+b)-y\,(\mathbf{w}\cdot\mathbf{x}+b), so its gradient is ∇wL=−y x\nabla_{\mathbf{w}}L=-y\,\mathbf{x} and ∂L/∂b=−y\partial L/\partial b=-y. A gradient-descent step subtracts η\eta times the gradient:

w←w−η (−y x)=w+η y x,b←b+η y.\begin{aligned} \mathbf{w}&\leftarrow\mathbf{w}-\eta\,(-y\,\mathbf{x})=\mathbf{w}+\eta\,y\,\mathbf{x},\\ b&\leftarrow b+\eta\,y . \end{aligned}

That is the perceptron rule. For a correct example the gradient is 00 and nothing moves. So the rule is stochastic gradient descent (one example at a time) on this loss. There is one subtlety. At a score of exactly 00 the loss has a corner and no single gradient, and the convention “a tie is a mistake” picks one of the allowed directions, called a subgradient. Note that w=0\mathbf{w}=\mathbf{0}, b=0b=0 already has loss 00 on every example. The rule leaves it only because the tie rule picks the direction −y x-y\,\mathbf{x} at the corner. So this is SGD with a particular subgradient choice, not a search for the minimum of a loss with a unique answer.

How does this loss compare with the ones you know, as a function of y zy\,z?

y zy\,zperceptronhinge (P3.B6)logistic (P3.A7)
−1-111221.3131.313
0.50.5000.50.50.4740.474
2200000.1270.127

The hinge loss max⁡(0,1−y z)\max(0,1-y\,z) is the perceptron loss shifted by one: it still charges for correct points that are inside the margin (y z<1y\,z<1). That is how the SVM gets its street. The logistic loss ln⁡(1+e−y z)\ln(1+e^{-y\,z}) (written for y=±1y=\pm1, where P3.A7 uses 0/10/1 labels) is smooth and never exactly zero. The perceptron loss is exactly zero for every correct example, so once an example is right it is ignored, however close to the line it sits. That is why the rule stops when the data is separated, and also why it picks no particular line. P4.6 covers the losses used for neural networks.

History: the book and the winter

Rosenblatt introduced the perceptron in the late 1950s, at the Cornell Aeronautical Laboratory, and had it built as a machine for recognising simple images. The learning rule and early results drew wide attention and ambitious claims.

In 1969 Marvin Minsky and Seymour Papert published the book Perceptrons (MIT Press). It was a careful mathematical study of what single-layer perceptrons can and cannot compute, and of how many inputs each unit needs to see. XOR, and its many-input version parity, were among the examples of things such a perceptron cannot do. The proofs are about single-layer perceptrons. They are not proofs that networks with hidden layers are weak.

In the 1970s funding for neural-network research became hard to find, and this is often called part of the first AI winter, a period of reduced funding and interest in artificial intelligence (the name came later, in the 1980s). Historians disagree about how much the book caused the funding decline: AI as a whole had over-promised, and funders cut back in several countries, including after the 1973 Lighthill report in the UK. What is clear is that the book’s message was often heard as “neural nets do not work,” which is more than its theorems say.

The way out was already visible. A network with a hidden layer can compute XOR (next section). What was missing was a good way to train the hidden weights. The perceptron rule needs a known correct answer for every unit, and a hidden unit has none. The idea that solves this, backpropagation, had been described by others before (for example Linnainmaa, 1970, and Werbos, 1974), and it became widely known with a 1986 paper by Rumelhart, Hinton and Williams. P4.7 and P4.8 teach it.

The way out

There are two standard ways to separate XOR with perceptrons.

1. Add a feature. Give the perceptron a third input, the product x1x2x_1x_2. The four points become (0,0,0)(0,0,0), (0,1,0)(0,1,0), (1,0,0)(1,0,0) and (1,1,1)(1,1,1), and the score x1+x2−2x1x2−12x_1+x_2-2x_1x_2-\tfrac12 is −0.5-0.5, +0.5+0.5, +0.5+0.5, −0.5-0.5 on them: exactly the labels. A plane separates them. The perceptron rule itself, run on the three inputs from zero, finds a separator: it stops after 12 epochs at w=(2,2,−5)\mathbf{w}=(2,2,-5), b=−1b=-1. Adding such features by hand is the idea of polynomial features (P3.A3), and the kernel trick (P3.B7) does it without writing the features out. The weakness is that someone must choose the feature.

2. Add a hidden layer. Use three perceptrons. The first computes OR (weights (1,1)(1,1), bias −0.5-0.5), the second computes NAND, “not both” (weights (−1,−1)(-1,-1), bias 1.51.5). The third perceptron takes those two outputs and computes AND (weights (1,1)(1,1), bias −1.5-1.5). On the four inputs, the first two give (0,1)(0,1), (1,1)(1,1), (1,1)(1,1), (1,0)(1,0), and the AND of each pair is 0,1,1,00, 1, 1, 0: XOR. The hidden layer bends the plane: in the plane of the two hidden outputs, the classes are separable. P4.3 shows this with a widget and trains such a network, so the network finds the weights itself.

Intuition

Think of a teacher who looks at one answer at a time and says nothing when it is right. When it is wrong, the teacher pushes the boundary a step toward the correct side. Each push fixes one example and may disturb others. When the classes are separable, the pushes eventually stop disturbing anything. When they are not, the teacher keeps pushing for ever.

The analogy breaks in one place: the teacher never says how wrong an answer was. The update uses only the example and its label, and a correct answer that sits almost on the line gets no push at all.

Math

Rule: if yi(w⋅xi+b)≤0y_i(\mathbf{w}\cdot\mathbf{x}_i+b)\le0 then w←w+ηyixi\mathbf{w}\leftarrow\mathbf{w}+\eta y_i\mathbf{x}_i and b←b+ηyib\leftarrow b+\eta y_i.

Score change on that example: Δz=ηyi(∥xi∥2+1)\Delta z=\eta y_i(\lVert\mathbf{x}_i\rVert^2+1).

Loss: Li=max⁡(0,−yi(w⋅xi+b))L_i=\max(0,-y_i(\mathbf{w}\cdot\mathbf{x}_i+b)), with ∇wLi=−yixi\nabla_{\mathbf{w}}L_i=-y_i\mathbf{x}_i on a mistake and 0\mathbf{0} otherwise. So the rule is SGD with step η\eta.

Convergence: with ∥x~i∥≤R\lVert\tilde{\mathbf{x}}_i\rVert\le R and a unit u\mathbf{u} with yi u⋅x~i≥γy_i\,\mathbf{u}\cdot\tilde{\mathbf{x}}_i\ge\gamma, the number of updates is at most (R/γ)2(R/\gamma)^2.

Code

The function perceptron is in the cell right below these tabs: it loops over the examples, applies if yi * (w @ xi + b) <= 0 to find a mistake, updates w and b, and stops after an epoch with no mistakes.

Try it: the rule on OR, AND and XOR

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

Try changing the XOR labels to AND’s, or add a third column x1x2x_1x_2 to X and see XOR become learnable.

Common mistakes

Quiz

Check your understanding

  1. A perceptron has w = (1, 0), b = 0 and learning rate 1. It sees x = (0, 2) with label +1. Use this lesson’s rule, where a score of exactly 0 counts as a mistake. What are w and b afterwards?

  2. You run the perceptron rule on a separable data set from w = 0 and b = 0, once with learning rate 1 and once with learning rate 0.1 (ignoring rounding). What differs?

  3. What does the perceptron convergence theorem guarantee?

  4. On XOR the widget shows 4 mistakes in each of 1000 epochs. Which conclusion is correct?

  5. For a misclassified example, what is the gradient of the loss max(0, −y(w·x + b)) with respect to w?

Exercises

  1. Let w=(2,−1)\mathbf{w}=(2,-1), b=0.5b=0.5, η=0.5\eta=0.5 and the example x=(2,1)\mathbf{x}=(2,1) with y=−1y=-1. Is it a mistake? If so, give the new w\mathbf{w}, bb and the new score.
    Show answerThe score is 4−1+0.5=3.54-1+0.5=3.5, and y z=−3.5≤0y\,z=-3.5\le0, so it is a mistake. w=(2,−1)+0.5⋅(−1)⋅(2,1)=(1,−1.5)\mathbf{w}=(2,-1)+0.5\cdot(-1)\cdot(2,1)=(1,-1.5) and b=0.5−0.5=0b=0.5-0.5=0. The new score is 2−1.5+0=0.52-1.5+0=0.5. The change is −3-3, which equals ηy(∥x∥2+1)=0.5⋅(−1)⋅6\eta y(\lVert\mathbf{x}\rVert^2+1)=0.5\cdot(-1)\cdot6. The score is still positive, so this example is not yet right: one update was not enough.
  2. Run the first epoch of AND by hand (order (0,0),(0,1),(1,0),(1,1)(0,0),(0,1),(1,0),(1,1), labels −1,−1,−1,+1-1,-1,-1,+1, zero start, η=1\eta=1).
    Show answer(0,0)(0,0): score 00, mistake, w=(0,0)\mathbf{w}=(0,0), b=−1b=-1. (0,1)(0,1): score −1-1, correct. (1,0)(1,0): score −1-1, correct. (1,1)(1,1): score −1-1 with y=+1y=+1, a mistake, so w=(1,1)\mathbf{w}=(1,1), b=0b=0. Two mistakes, ending at w=(1,1)\mathbf{w}=(1,1), b=0b=0.
  3. In the 0/1 form, an example has target t=1t=1 and the perceptron outputs o=0o=0. Write the update, and compare it with the ±1\pm1 form.
    Show answert−o=1t-o=1, so w←w+ηx\mathbf{w}\leftarrow\mathbf{w}+\eta\mathbf{x} and b←b+ηb\leftarrow b+\eta. In the ±1\pm1 form the label is y=2t−1=+1y=2t-1=+1, so the update is w+η⋅(+1)⋅x\mathbf{w}+\eta\cdot(+1)\cdot\mathbf{x}, the same.
  4. Show that XNOR (labels +1+1 at (0,0)(0,0) and (1,1)(1,1), −1-1 at (0,1)(0,1) and (1,0)(1,0)) is not linearly separable either.
    Show answerThe inequalities are b>0b>0, w2+b<0w_2+b<0, w1+b<0w_1+b<0 and w1+w2+b>0w_1+w_2+b>0. Adding the middle two gives w1+w2+2b<0w_1+w_2+2b<0, so w1+w2+b<−b<0w_1+w_2+b<-b<0, which contradicts the last one.
  5. The data is x=1x=1 with y=+1y=+1 and x=−1x=-1 with y=−1y=-1, in one dimension. Find RR, a margin γ\gamma and the bound, then run the rule from zero with η=1\eta=1 and count the updates.
    Show answerWith the bias input, the inputs are (1,1)(1,1) and (−1,1)(-1,1), so R=2R=\sqrt2. The unit vector u=(1,0)\mathbf{u}=(1,0) gives y u⋅x~=1y\,\mathbf{u}\cdot\tilde{\mathbf{x}}=1 for both, so γ=1\gamma=1 and the bound is 22. The run: x=1x=1 has score 00, a mistake, giving w=1w=1, b=1b=1. x=−1x=-1 has score 00, a mistake, giving w=2w=2, b=0b=0. Then both are correct. Two updates, so the bound is tight here.
  6. Why does the pocket algorithm keep a separate copy of the best weights instead of just returning the last weights?
    Show answerOn non-separable data the running weights keep changing, and the last weights can be much worse than earlier ones (8 of 14 right after epoch 50, against 13 of 14 for the best weights seen). The pocket remembers the best set it has seen.

Key takeaways

  • The perceptron rule: look at one example. If the perceptron gets it wrong, add η·y·x to w and η·y to b (labels +1 or −1). If it is right, change nothing. Here a score of exactly 0 counts as wrong.
  • Each update moves the boundary toward the mistake: the score of that example changes by η·y·(‖x‖² + 1), in the correct direction. One update may not be enough to fix that example.
  • A pass through the data is an epoch. Training stops when a whole epoch has no mistakes. Starting from w = 0 and b = 0, the learning rate only scales w and b and does not change the mistakes or the boundary.
  • Convergence theorem: if some line separates the classes with margin γ and every input has length at most R (with the bias counted as an extra input of 1), the rule makes at most (R/γ)² updates, whatever the order. It does not say which line it finds, and it says nothing when no line separates the classes.
  • XOR has no separating line: the four inequalities contradict each other. The rule then keeps making mistakes in every epoch and may go round in a cycle.
  • The rule is stochastic gradient descent on the loss max(0, −y·(w·x + b)). Unlike the hinge loss it asks for no margin, and unlike the logistic loss it is exactly zero for every correct example.
  • Minsky and Papert’s 1969 book proved limits of single-layer perceptrons. Historians disagree about how much it caused the funding decline of the 1970s. Hidden layers fix XOR, and backpropagation (popularised in 1986) made them trainable.
Found a mistake? Tell us