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.
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 gets the score , and the prediction is its sign:
for class A and for class B. Here the labels are signs too: for class A and 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
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: is positive when points within a right angle of and negative beyond it. The line is where the score is zero, at right angles to .
After a mistake on the dot with label , the update is
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 , so times its score rises by , 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 : take a line that separates the dots, scaled so that and together, as one vector, have length 1. Every dot’s times its score is then positive, and is the smallest of them. A wide gap between the classes means a large margin.
- The radius : 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
whatever the order of the dots. Each mistake pushes the weights at least 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 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 and , class B at and . Start from and , and visit , , , 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.
Describe this picture
A small plane with the four dots of the worked example, labelled to , 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 . Readouts give the pass and the corrections so far. There is no other control.
| Pass | Dot | Label | Score before | New weights | New bias term |
|---|---|---|---|---|---|
| 1 | |||||
| 1 | |||||
| 2 | |||||
| 3 | |||||
| 3 | |||||
| 4 | |||||
| 4 |
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 , that is .
Four of the seven corrections were for . One update did not fix it: its first correction left it on the line (score 0), and later updates for and put it back on or across the line.
The bound. The line separates the dots: as one vector, its weights and bias term are , of length , and every dot’s times its score is 1. Scaled to length 1, the margin is . With a 1 appended, the farthest dot is , , of length . The bound is mistakes, far above the seven used: it is a guarantee, not a forecast.
Another order. Visit the same dots as , , , , from the same start. The run again needs seven corrections, in passes 1 to 4, but it stops on , , the line : a different separating line.
A quick check. With and , the class A dot scores : a mistake. The update gives and .
When it never stops
Now the second half of the answer. Four dots at the corners of a square, class B at and , class A at and : the pattern called XOR, class A when exactly one coordinate is 1. (The clip uses the corners ; 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 and , which add to ;
- the class A dots need and , which add to .
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
| Quantity | Formula | Notes |
|---|---|---|
| Prediction | class A, class B | |
| Mistake | a dot on the line counts | |
| Update | , | only after a mistake |
| Effect | score rises by | for the dot just corrected |
| Separable data | it stops | on some separating line, not the best |
| Bound | at most mistakes | margin, radius with a 1 appended |
| XOR | no line separates it | never stops; needs layers |