Skip to content

The perceptron

A weighted sum and a threshold make a line classifier; nudging the line after each mistake finds a separating line whenever one exists, and never settles when none does.

Helps to know first
This topic

First, the picture

Move the line only when it is wrong. Below, a line learns from twelve dots, then meets four more. Before it plays: will the line always stop moving?

A line that learns from its mistakes

The perceptron checks each dot in turn and moves its line only when a dot is on the wrong side.

Twelve labelled dots and a starting line, upright through the middle: it says class A on its right and class B on its left.

pass
–
mistakes this pass
–
moves of the line so far
–
0.00 / 35.00 s
Describe this picture

One plane, feature 1 across and feature 2 up, both from −2.5 to 2.5, with twelve labelled dots: six round dots of class A towards the top right and six squares of class B towards the bottom left, with a narrow gap between the two groups. A straight line divides the plane, each side shaded by the class it predicts; it starts upright through the middle, class A on its right. In the clip the dots are visited one at a time, in the same order on every pass. A dot on the wrong side flashes, and the line jolts towards putting it right; a dot on its own side leaves the line where it is. On a wide screen a ledger beside the plane shows each pass dot by dot. The first pass makes 4 mistakes, the second 4, among them dots that were put right in pass 1, and the third 2. The fourth makes none, and the run stops with every dot on its own side. Then four new dots in a cross replace them: class A at two opposite corners, class B at the other two, with the line upright again. In each of three passes every dot is a mistake, and after its four moves the line is back where it started. The last caption reads “No line can separate these: however many passes, the line never settles.” Readouts give the pass, the mistakes this pass and the moves of the line so far. There is a play button and no other control.

The rule

The perceptron is the line rule of What machine learning is, now with its name. A point x\mathbf{x} gets the score w⊤x+b\mathbf{w}^\top\mathbf{x}+b, and the prediction is its sign:

y^=sgn(w⊤x+b),\hat y=\mathrm{sgn}(\mathbf{w}^\top\mathbf{x}+b),

+1+1 for class A and −1-1 for class B. Here the labels are signs too: y=+1y=+1 for class A and y=−1y=-1 for class B. That makes a mistake easy to write. The prediction is right when the score has the sign of the label, so a dot is a mistake when

y (w⊤x+b)≤0,y\,(\mathbf{w}^\top\mathbf{x}+b)\le0,

which counts a dot exactly on the line as a mistake too.

In linear algebra · the score as a dot product

The score is a dot product, as in Length and the dot product in the linear algebra course: w⊤x\mathbf{w}^\top\mathbf{x} is positive when x\mathbf{x} points within a right angle of w\mathbf{w} and negative beyond it. The line is where the score is zero, at right angles to w\mathbf{w}.

After a mistake on the dot x\mathbf{x} with label yy, the update is

w←w+y x,b←b+y.\mathbf{w}\leftarrow\mathbf{w}+y\,\mathbf{x},\qquad b\leftarrow b+y.

For a class A dot, add it; for a class B dot, subtract it. Why does that help? The dot’s new score is its old score plus y(∥x∥2+1)y\big(\lVert\mathbf{x}\rVert^2+1\big), so yy times its score rises by ∥x∥2+1\lVert\mathbf{x}\rVert^2+1, which is always positive. The line turns and shifts towards being right about that dot. It may not get there in one update, and it may now be wrong about another dot; it moves only when it is wrong, and never otherwise.

When it stops

Now the answer to the question, the first half. When some line puts every dot on its own side, the data are separable, and then the perceptron always stops: after finitely many mistakes a whole pass makes none. How many at most depends on two sizes.

  • The margin γ\gamma: take a line that separates the dots, scaled so that w\mathbf{w} and bb together, as one vector, have length 1. Every dot’s yy times its score is then positive, and γ\gamma is the smallest of them. A wide gap between the classes means a large margin.
  • The radius RR: append a 1 to each dot, as with the bias term in Linear regression with many features, and take the largest length of these vectors.

Then, starting from all zeros, the number of mistakes is at most

(Rγ)2,\Big(\frac{R}{\gamma}\Big)^2,

whatever the order of the dots. Each mistake pushes the weights at least γ\gamma further along the separating line’s direction, while their length grows more slowly, and the two can only go on together for so many mistakes. A narrow gap, as in the clip, means a small γ\gamma and possibly many mistakes.

The line the perceptron stops on is any line that separates the dots, not the best one. It depends on the start and on the order of the dots, and it often ends close to some of them.

Watch out

The perceptron finds a separating line, not the best one. A different order of the same dots can end on a different line.

Worked example

Four dots: class A at P1=(1,3)P_1=(1,3) and P2=(2,3)P_2=(2,3), class B at P3=(2,1)P_3=(2,1) and P4=(3,1)P_4=(3,1). Start from w=(0,0)\mathbf{w}=(0,0) and b=0b=0, and visit P1P_1, P2P_2, P3P_3, P4P_4 in that order on every pass. Each row is one correction.

Seven corrections, by hand

The worked example: four dots, visited in order, the line redrawn after each mistake.

Four dots: P₁ and P₂ of class A (label +1), P₃ and P₄ of class B (label −1). The line starts at w = (0, 0), b = 0, so every score is 0.

pass
–
corrections so far
0
0.00 / 26.10 s
Describe this picture

A small plane with the four dots of the worked example, labelled P1P_1 to P4P_4, beside a table with the columns pass, dot, score, new w and new b. A play button steps through the run: each of the seven corrections makes its dot flash, adds its row to the table and redraws the line, which is absent until the first correction. Then pass 5 scores 10, 6, −4 and −8, all on the right sides, and the run stops: seven corrections, four of them for P3P_3. Readouts give the pass and the corrections so far. There is no other control.

PassDotLabelScore beforeNew weightsNew bias term
1P1=(1,3)P_1=(1,3)+1+100(1,3)(1,3)11
1P3=(2,1)P_3=(2,1)−1-166(−1,2)(-1,2)00
2P3=(2,1)P_3=(2,1)−1-100(−3,1)(-3,1)−1-1
3P1=(1,3)P_1=(1,3)+1+1−1-1(−2,4)(-2,4)00
3P3=(2,1)P_3=(2,1)−1-100(−4,3)(-4,3)−1-1
4P2=(2,3)P_2=(2,3)+1+100(−2,6)(-2,6)00
4P3=(2,1)P_3=(2,1)−1-122(−4,5)(-4,5)−1-1

In pass 5 the scores are 10, 6, −4 and −8: both class A dots positive, both class B dots negative. The run stops after seven corrections, on the line −4x1+5x2−1=0-4x_1+5x_2-1=0, that is x2=(4x1+1)/5x_2=(4x_1+1)/5.

Four of the seven corrections were for P3P_3. One update did not fix it: its first correction left it on the line (score 0), and later updates for P1P_1 and P2P_2 put it back on or across the line.

The bound. The line x2=2x_2=2 separates the dots: as one vector, its weights and bias term are (0,1,−2)(0,1,-2), of length 5\sqrt5, and every dot’s yy times its score is 1. Scaled to length 1, the margin is γ=1/5=0.447\gamma=1/\sqrt5=0.447. With a 1 appended, the farthest dot is P2P_2, (2,3,1)(2,3,1), of length 14=3.742\sqrt{14}=3.742. The bound is (R/γ)2=14×5=70(R/\gamma)^2=14\times5=70 mistakes, far above the seven used: it is a guarantee, not a forecast.

Another order. Visit the same dots as P4P_4, P3P_3, P2P_2, P1P_1, from the same start. The run again needs seven corrections, in passes 1 to 4, but it stops on w=(−5,5)\mathbf{w}=(-5,5), b=−1b=-1, the line x2=x1+0.2x_2=x_1+0.2: a different separating line.

A quick check. With w=(1,0)\mathbf{w}=(1,0) and b=0b=0, the class A dot (−1,2)(-1,2) scores −1-1: a mistake. The update gives w=(0,2)\mathbf{w}=(0,2) and b=1b=1.

When it never stops

Now the second half of the answer. Four dots at the corners of a square, class B at (0,0)(0,0) and (1,1)(1,1), class A at (0,1)(0,1) and (1,0)(1,0): the pattern called XOR, class A when exactly one coordinate is 1. (The clip uses the corners ±1\pm1; it is the same pattern.) No line separates them. Suppose one did, even allowing the class B dots to sit on the line. Then:

  • the class B dots need b≤0b\le0 and w1+w2+b≤0w_1+w_2+b\le0, which add to w1+w2+2b≤0w_1+w_2+2b\le0;
  • the class A dots need w2+b>0w_2+b>0 and w1+b>0w_1+b>0, which add to w1+w2+2b>0w_1+w_2+2b>0.

Both cannot hold, so no line works. The data are not separable, and the perceptron makes at least one mistake in every pass for ever: more passes never help.

The way out is to combine several lines. Neurons and activation functions builds a network from units like this one, and Multilayer networks separates XOR with two layers. Logistic regression is a soft version of the same line, with a chance in place of a sign.

Where you’ll meet this

Learning as data arrive. The perceptron needs one example at a time and a single update, so it suits classifiers that learn from a stream: sorting messages or tagging text as they come in.

Neural networks. A perceptron is one unit of a network: a weighted sum, then a threshold. Networks replace the hard threshold with a smooth one, so they can be fitted by gradient descent.

An early learning machine. It was one of the first machines that learned from examples, built in hardware in the late 1950s, with adjustable weights set by this rule.

Reference card

QuantityFormulaNotes
Predictiony^=sgn(w⊤x+b)\hat y=\mathrm{sgn}(\mathbf{w}^\top\mathbf{x}+b)+1+1 class A, −1-1 class B
Mistakey(w⊤x+b)≤0y(\mathbf{w}^\top\mathbf{x}+b)\le0a dot on the line counts
Updatew←w+yx\mathbf{w}\leftarrow\mathbf{w}+y\mathbf{x}, b←b+yb\leftarrow b+yonly after a mistake
Effecty×y\times score rises by ∥x∥2+1\lVert\mathbf{x}\rVert^2+1for the dot just corrected
Separable datait stopson some separating line, not the best
Boundat most (R/γ)2(R/\gamma)^2 mistakesγ\gamma margin, RR radius with a 1 appended
XORno line separates itnever stops; needs layers

Keep reading with a free account.

By creating an account you accept the terms of use.

Sign in

Already have an account? Use the service you signed up with.

Or keep reading: get new lessons by email

Are you 13 or over?

Accounts and email updates are for people aged 13 or over. We keep only your answer, never your date of birth.

End of the lesson

Where to go next.

Phasorium

Account

Sign inKeep your place on any device

Subjects

Site

Look