Four constructed points in the plane carry labels, two positive and two negative. Is there a straight line with every positive point on one side and every negative point on the other, and can a simple procedure find one by correcting a single mistake at a time? The perceptron is such a procedure, and this lesson runs its updates by hand. It also settles when the procedure is guaranteed to stop, and why on some data sets no line can classify every training point correctly.
Key terms
Linear classifier
Definition In this lesson the two classes are coded and . A linear classifier on feature vectors in has a weight vector in and an intercept , computes the score
and predicts when and when . For a labeled example the product is the signed score. It is positive when the sign of the score agrees with the label, negative when it disagrees, and zero when the point lies on the boundary, whatever its label. Multiplying and by the same positive number changes every score but no prediction.
In plain words The classifier adds up weighted features, adds a constant and looks only at the sign of the result. The size of the score is not a probability, and by itself it is not a distance either. A logistic regression model with threshold one half is also a linear classifier, because it predicts the positive class exactly when its linear score is at least zero.
Example Take and . The point has score and is predicted , and the point has score and is predicted . The weights with double the two scores to and and give the same predictions. The point has score under both and is predicted by the convention.
Separating hyperplane
Definition For the set of points in with score zero,
is a hyperplane, a flat set of dimension . It is a point when , a line when and a plane when . The vector is perpendicular to and points toward the side where the score is positive, and the signed distance from a point to is . Changing moves parallel to itself, and changing the direction of turns it. The hyperplane separates a labeled training set if
for every , so that every positive example lies strictly on the positive side and every negative example strictly on the negative side.
In plain words A separating hyperplane is a flat wall with no training point on it and no training point on the wrong side. When one exists there are always infinitely many, because a small enough shift or tilt keeps every one of finitely many points on its side. Separating the training data says nothing yet about how the wall will do on new data.
Example The constructed training set of this lesson has the points and with label and the points and with label . The line separates them, with signed scores , , and in that order, and the nearest points and lie at distance from it. The line also separates them but passes within of . The next lesson chooses among separators by this kind of distance.
Linear separability
Definition A labeled training set is linearly separable if some and satisfy for every example, that is, if a separating hyperplane exists. For a finite training set this holds exactly when some linear classifier makes no training mistakes under the rule above. Indeed, if every positive example scores at least and every negative example scores below , raising by a positive amount smaller than the smallest absolute score of a negative example, or by any positive amount when there are no negative examples, makes every inequality strict. Separability is a property of the chosen features, so a set that is not separable in its original features can become separable once new features such as squares or products are added.
In plain words Some labelings can be split by a straight wall and some cannot. When they cannot, no training procedure for a linear classifier on those features can reach zero training mistakes, and the limit lies in the representation, not in the optimizer.
Example The exclusive-or labels give and the label and give and the label . A linear classifier with no training mistakes would need
Adding the first two inequalities gives , and adding the last two gives , a contradiction. So every linear classifier misclassifies at least one of the four points. With the product as a third feature the labels become separable, since the score equals at and and at and .
Perceptron update rule
Definition Let be the current parameters, a training example with , and a fixed step size. If the signed score satisfies , the perceptron update rule sets
and otherwise it leaves both unchanged. An update is triggered by every misclassified example and also by every example on the boundary, including a positive example with score exactly , which the prediction rule labels correctly. Since , the update changes the signed score of the same example to
where the comes from the intercept. Appending a constant feature to and treating as its weight writes both updates as one.
In plain words A positive example that scores too low pulls the weight vector toward itself, and a negative example that scores too high pushes the weight vector away from itself. The update always raises the signed score of the example that caused it, but with a small step it need not make that example correct at once, and it can make other examples wrong. The rule uses rather than so that the all-zero starting point, where every score is , still moves.
Example Take , , and the negative example . Its score is , so its signed score is and it triggers an update. The new weights are and the new intercept is , so the new score is and the signed score is . The increase of matches . With instead, the signed score rises only by , to , and the example is still misclassified after the update.
Perceptron
Definition The perceptron is a training procedure for a linear classifier. Its inputs are training examples with , a step size , starting values, usually and , an order of the examples and a maximum number of passes.
- Visit the examples in order and apply the update rule to each one.
- If a complete pass makes no update, stop. Every example then has a positive signed score, so the final hyperplane separates the training set.
- Otherwise start another pass, unless the maximum number of passes has been reached. In that case stop and report that the procedure did not converge.
With zero starting values, every parameter after a given sequence of updates is times its value for step size , so the step size changes no prediction and no update decision.
In plain words The perceptron fixes one example at a time and keeps cycling until a full pass finds nothing to fix. It looks for any separating hyperplane, not a particular one, so its answer depends on the order of the examples and on the starting point. It stops at the first separator it reaches, which need not leave the widest gap, and its scores are not probabilities.
Example Run the perceptron from zero with on the four training points in the order below.
| Step | Example and label | Score | Signed score | New and |
|---|---|---|---|---|
| 1 | , | and | ||
| 2 | , | and | ||
| 3 | , | no update | ||
| 4 | , | no update |
The second pass finds the scores , , and , all with positive signed scores, so it makes no update and the perceptron stops after two updates at the line . Starting with , then , and , it ends instead at and , the line . Over all orders the final weights are , , or with , which give three different lines, since is a multiple of .
Perceptron convergence theorem
Definition Write each example in augmented form in and the parameters as , so that the score is . Suppose that
- there is a number with for every example, and
- there are a unit vector in and a number with for every example.
Then the perceptron started at , with any fixed step size and the examples presented in any order, even one that changes from pass to pass, makes a total number of updates with
Every pass that does not stop contains an update, so the perceptron stops after finitely many passes at a separating hyperplane. For unit steps the proof follows two quantities. Each update adds to , which raises by at least . It changes by , because updates happen only when . After updates the Cauchy-Schwarz inequality gives
and squaring gives the bound. For a finite training set the first condition always holds, and the second holds exactly when the set is linearly separable, with a normalized separator and its smallest signed score. For an unending stream of examples both conditions are real assumptions.
In plain words Each mistake moves the parameters a fixed amount further along a true separator, while their length grows only like the square root of the number of mistakes, so the mistakes must run out. The bound is a worst case over all orders. It grows when the classes come close to each other, which makes small, and when points lie far from the origin, which makes large. It promises nothing about errors on new data or about the width of the gap left by the line that is found. Without separability the theorem does not apply, and on a finite set that is not separable the perceptron never completes a pass without an update.
Example For the four training points the augmented vectors are , , and , so . The unit vector gives the values equal to , , and , so and
No unit vector gives a larger for these points, so the theorem allows at most updates in any order, and every run above used . For the exclusive-or labels no such exists. In the order , , , the perceptron makes four updates in every pass and is back at and at the end of each pass.
Check yourself
- A perceptron has , and step size . The next example is with label . Decide whether it triggers an update, find the new parameters and the new score, and check the change in the signed score.
Show the answer
The score is , so the signed score is and the example triggers an update. The new weights are and the new intercept is . The new score is
The signed score rose by , which matches . The intercept update supplies the final . This example is now correct, but other training examples may now have nonpositive signed scores, so the pass continues.
- One feature is measured on three training examples. The inputs and have label and the input has label . Show that no linear classifier that predicts when classifies all three correctly. What does the perceptron do on these data, and which added feature makes them separable?
Show the answer
A classifier with no mistakes would need , and . Adding the first two gives , which contradicts . So every linear classifier in this one feature makes at least one training mistake, and the data are not linearly separable.
The perceptron stops only after a pass in which every signed score is positive, and that would be a separating classifier. So it updates in every pass and runs until the pass limit. From zero with in the order , , , the first pass makes three updates and ends at and . A run that ends because of the limit has not converged, and its final weights should not be reported as a separator.
Adding the feature fixes this. The score equals at and and at , so every signed score is .
- One feature has the training inputs and with label and with label . In augmented form , use the unit vector to compute , and the bound of the convergence theorem. Then run the perceptron from zero with in the order , , and compare.
Show the answer
The augmented vectors are , and , with lengths , and , so . The values are , and , so and the bound is updates.
In the first pass, has score and triggers an update to and . Then has score and is correct. Next has score , so its signed score is and it triggers an update to and . The second pass finds the scores , and , all with positive signed scores, so the perceptron stops after updates at the threshold .
Two updates are well within the bound of , which holds for every order. The theorem guarantees only that the updates stop on these training data. It says nothing about how the threshold will classify new inputs.