Unit 7, lesson 45

Perceptrons and Separating Hyperplanes

How a linear classifier splits feature space with a hyperplane, how the perceptron update rule corrects one mistake at a time, and when linear separability guarantees that the perceptron stops.

  • Video 45
  • Margins and Kernels

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 y=+1y = +1 and y=−1y = -1. A linear classifier on feature vectors x\mathbf{x} in Rp\mathbb{R}^p has a weight vector w\mathbf{w} in Rp\mathbb{R}^p and an intercept bb, computes the score

f(x)=w⊤x+b,f(\mathbf{x}) = \mathbf{w}^{\top}\mathbf{x} + b ,

and predicts y^=+1\hat{y} = +1 when f(x)≥0f(\mathbf{x}) \ge 0 and y^=−1\hat{y} = -1 when f(x)<0f(\mathbf{x}) < 0. For a labeled example (x,y)(\mathbf{x}, y) the product y f(x)y\,f(\mathbf{x}) 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 w\mathbf{w} and bb 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 w=(1,−1)\mathbf{w} = (1, -1) and b=0b = 0. The point (2,1)(2, 1) has score 2−1=12 - 1 = 1 and is predicted +1+1, and the point (1,3)(1, 3) has score −2-2 and is predicted −1-1. The weights (2,−2)(2, -2) with b=0b = 0 double the two scores to 22 and −4-4 and give the same predictions. The point (1,1)(1, 1) has score 00 under both and is predicted +1+1 by the convention.

Separating hyperplane

Definition For w≠0\mathbf{w} \ne \mathbf{0} the set of points x\mathbf{x} in Rp\mathbb{R}^p with score zero,

H={x:w⊤x+b=0},H = \{\mathbf{x} : \mathbf{w}^{\top}\mathbf{x} + b = 0\} ,

is a hyperplane, a flat set of dimension p−1p - 1. It is a point when p=1p = 1, a line when p=2p = 2 and a plane when p=3p = 3. The vector w\mathbf{w} is perpendicular to HH and points toward the side where the score is positive, and the signed distance from a point x\mathbf{x} to HH is f(x)/∥w∥2f(\mathbf{x})/\lVert \mathbf{w} \rVert_2. Changing bb moves HH parallel to itself, and changing the direction of w\mathbf{w} turns it. The hyperplane separates a labeled training set (x1,y1),…,(xn,yn)(\mathbf{x}_1, y_1), \dots, (\mathbf{x}_n, y_n) if

yi(w⊤xi+b)>0y_i(\mathbf{w}^{\top}\mathbf{x}_i + b) > 0

for every ii, 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 (1,0)(1, 0) and (2,0)(2, 0) with label +1+1 and the points (0,1)(0, 1) and (0,2)(0, 2) with label −1-1. The line x1−x2=0x_1 - x_2 = 0 separates them, with signed scores 11, 22, 11 and 22 in that order, and the nearest points (1,0)(1, 0) and (0,1)(0, 1) lie at distance 1/2≈0.7071/\sqrt{2} \approx 0.707 from it. The line x1−x2−0.5=0x_1 - x_2 - 0.5 = 0 also separates them but passes within 0.5/2≈0.3540.5/\sqrt{2} \approx 0.354 of (1,0)(1, 0). The next lesson chooses among separators by this kind of distance.

Linear separability

Definition A labeled training set is linearly separable if some w\mathbf{w} and bb satisfy yi(w⊤xi+b)>0y_i(\mathbf{w}^{\top}\mathbf{x}_i + b) > 0 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 00 and every negative example scores below 00, raising bb 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 (1,0)(1, 0) and (0,1)(0, 1) the label +1+1 and give (0,0)(0, 0) and (1,1)(1, 1) the label −1-1. A linear classifier with no training mistakes would need

w1+b≥0,w2+b≥0,b<0,w1+w2+b<0.\begin{gathered} w_1 + b \ge 0, \quad w_2 + b \ge 0, \\[0.4em] b < 0, \quad w_1 + w_2 + b < 0 . \end{gathered}

Adding the first two inequalities gives w1+w2+2b≥0w_1 + w_2 + 2b \ge 0, and adding the last two gives w1+w2+2b<0w_1 + w_2 + 2b < 0, a contradiction. So every linear classifier misclassifies at least one of the four points. With the product x1x2x_1 x_2 as a third feature the labels become separable, since the score x1+x2−2x1x2−0.5x_1 + x_2 - 2x_1 x_2 - 0.5 equals −0.5-0.5 at (0,0)(0, 0) and (1,1)(1, 1) and 0.50.5 at (1,0)(1, 0) and (0,1)(0, 1).

Perceptron update rule

Definition Let (w,b)(\mathbf{w}, b) be the current parameters, (x,y)(\mathbf{x}, y) a training example with y∈{−1,+1}y \in \{-1, +1\}, and η>0\eta > 0 a fixed step size. If the signed score satisfies y(w⊤x+b)≤0y(\mathbf{w}^{\top}\mathbf{x} + b) \le 0, the perceptron update rule sets

w←w+η y x,b←b+η y,\begin{aligned} \mathbf{w} &\leftarrow \mathbf{w} + \eta\, y\, \mathbf{x}, \\[0.4em] b &\leftarrow b + \eta\, y , \end{aligned}

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 00, which the prediction rule labels correctly. Since y2=1y^2 = 1, the update changes the signed score of the same example to

y fnew(x)=y fold(x)+η (∥x∥22+1),\begin{aligned} y\,f_{\text{new}}(\mathbf{x}) &= y\,f_{\text{old}}(\mathbf{x}) \\[0.4em] &\quad + \eta\,\bigl(\lVert \mathbf{x} \rVert_2^2 + 1\bigr) , \end{aligned}

where the 11 comes from the intercept. Appending a constant feature 11 to x\mathbf{x} and treating bb 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 ≤0\le 0 rather than <0< 0 so that the all-zero starting point, where every score is 00, still moves.

Example Take w=(1,1)\mathbf{w} = (1, 1), b=0b = 0, η=1\eta = 1 and the negative example x=(2,1)\mathbf{x} = (2, 1). Its score is 33, so its signed score is −3-3 and it triggers an update. The new weights are (1,1)−(2,1)=(−1,0)(1, 1) - (2, 1) = (-1, 0) and the new intercept is −1-1, so the new score is −2−1=−3-2 - 1 = -3 and the signed score is 33. The increase of 66 matches ∥x∥22+1=4+1+1\lVert \mathbf{x} \rVert_2^2 + 1 = 4 + 1 + 1. With η=0.1\eta = 0.1 instead, the signed score rises only by 0.60.6, to −2.4-2.4, 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 (xi,yi)(\mathbf{x}_i, y_i) with yi∈{−1,+1}y_i \in \{-1, +1\}, a step size η>0\eta > 0, starting values, usually w=0\mathbf{w} = \mathbf{0} and b=0b = 0, an order of the examples and a maximum number of passes.

  1. Visit the examples in order and apply the update rule to each one.
  2. If a complete pass makes no update, stop. Every example then has a positive signed score, so the final hyperplane separates the training set.
  3. 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 η\eta times its value for step size 11, 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 η=1\eta = 1 on the four training points in the order below.

StepExample and labelScoreSigned scoreNew w\mathbf{w} and bb
1(1,0)(1, 0), +1+10000(1,0)(1, 0) and 11
2(0,1)(0, 1), −1-111−1-1(1,−1)(1, -1) and 00
3(2,0)(2, 0), +1+12222no update
4(0,2)(0, 2), −1-1−2-222no update

The second pass finds the scores 11, −1-1, 22 and −2-2, all with positive signed scores, so it makes no update and the perceptron stops after two updates at the line x1−x2=0x_1 - x_2 = 0. Starting with (2,0)(2, 0), then (0,1)(0, 1), (1,0)(1, 0) and (0,2)(0, 2), it ends instead at w=(2,−1)\mathbf{w} = (2, -1) and b=0b = 0, the line 2x1−x2=02x_1 - x_2 = 0. Over all 2424 orders the final weights are (1,−1)(1, -1), (1,−2)(1, -2), (2,−1)(2, -1) or (2,−2)(2, -2) with b=0b = 0, which give three different lines, since (2,−2)(2, -2) is a multiple of (1,−1)(1, -1).

Perceptron convergence theorem

Definition Write each example in augmented form zi=(xi,1)\mathbf{z}_i = (\mathbf{x}_i, 1) in Rp+1\mathbb{R}^{p+1} and the parameters as θ=(w,b)\boldsymbol{\theta} = (\mathbf{w}, b), so that the score is θ⊤zi\boldsymbol{\theta}^{\top}\mathbf{z}_i. Suppose that

  1. there is a number RR with ∥zi∥2≤R\lVert \mathbf{z}_i \rVert_2 \le R for every example, and
  2. there are a unit vector u\mathbf{u} in Rp+1\mathbb{R}^{p+1} and a number γ>0\gamma > 0 with yi u⊤zi≥γy_i\,\mathbf{u}^{\top}\mathbf{z}_i \ge \gamma for every example.

Then the perceptron started at θ=0\boldsymbol{\theta} = \mathbf{0}, with any fixed step size η>0\eta > 0 and the examples presented in any order, even one that changes from pass to pass, makes a total number of updates MM with

M≤(Rγ)2.M \le \left(\frac{R}{\gamma}\right)^2 .

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 yiziy_i\mathbf{z}_i to θ\boldsymbol{\theta}, which raises u⊤θ\mathbf{u}^{\top}\boldsymbol{\theta} by at least γ\gamma. It changes ∥θ∥22\lVert \boldsymbol{\theta} \rVert_2^2 by 2yiθ⊤zi+∥zi∥22≤R22y_i\boldsymbol{\theta}^{\top}\mathbf{z}_i + \lVert \mathbf{z}_i \rVert_2^2 \le R^2, because updates happen only when yiθ⊤zi≤0y_i\boldsymbol{\theta}^{\top}\mathbf{z}_i \le 0. After MM updates the Cauchy-Schwarz inequality gives

Mγ≤u⊤θ≤∥θ∥2≤RM,\begin{aligned} M\gamma &\le \mathbf{u}^{\top}\boldsymbol{\theta} \le \lVert \boldsymbol{\theta} \rVert_2 \\[0.4em] &\le R\sqrt{M} , \end{aligned}

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 u\mathbf{u} a normalized separator and γ\gamma 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 γ\gamma small, and when points lie far from the origin, which makes RR 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 (1,0,1)(1, 0, 1), (0,1,1)(0, 1, 1), (2,0,1)(2, 0, 1) and (0,2,1)(0, 2, 1), so R=5R = \sqrt{5}. The unit vector u=(1,−1,0)/2\mathbf{u} = (1, -1, 0)/\sqrt{2} gives the values yiu⊤ziy_i\mathbf{u}^{\top}\mathbf{z}_i equal to 1/21/\sqrt{2}, 1/21/\sqrt{2}, 2\sqrt{2} and 2\sqrt{2}, so γ=1/2\gamma = 1/\sqrt{2} and

(Rγ)2=51/2=10.\left(\frac{R}{\gamma}\right)^2 = \frac{5}{1/2} = 10 .

No unit vector gives a larger γ\gamma for these points, so the theorem allows at most 1010 updates in any order, and every run above used 22. For the exclusive-or labels no such u\mathbf{u} exists. In the order (0,0)(0, 0), (1,0)(1, 0), (0,1)(0, 1), (1,1)(1, 1) the perceptron makes four updates in every pass and is back at w=(0,0)\mathbf{w} = (0, 0) and b=0b = 0 at the end of each pass.

Check yourself

  1. A perceptron has w=(2,−1)\mathbf{w} = (2, -1), b=−2b = -2 and step size η=0.5\eta = 0.5. The next example is x=(1,3)\mathbf{x} = (1, 3) with label +1+1. 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 2⋅1−1⋅3−2=−32 \cdot 1 - 1 \cdot 3 - 2 = -3, so the signed score is −3≤0-3 \le 0 and the example triggers an update. The new weights are (2,−1)+0.5 (1,3)=(2.5,0.5)(2, -1) + 0.5\,(1, 3) = (2.5, 0.5) and the new intercept is −2+0.5=−1.5-2 + 0.5 = -1.5. The new score is

2.5⋅1+0.5⋅3−1.5=2.5.2.5 \cdot 1 + 0.5 \cdot 3 - 1.5 = 2.5 .

The signed score rose by 5.55.5, which matches η (∥x∥22+1)=0.5 (1+9+1)\eta\,(\lVert \mathbf{x} \rVert_2^2 + 1) = 0.5\,(1 + 9 + 1). The intercept update supplies the final 11. This example is now correct, but other training examples may now have nonpositive signed scores, so the pass continues.

  1. One feature is measured on three training examples. The inputs x=−1x = -1 and x=1x = 1 have label +1+1 and the input x=0x = 0 has label −1-1. Show that no linear classifier that predicts +1+1 when wx+b≥0wx + b \ge 0 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 −w+b≥0-w + b \ge 0, w+b≥0w + b \ge 0 and b<0b < 0. Adding the first two gives 2b≥02b \ge 0, which contradicts b<0b < 0. 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 η=1\eta = 1 in the order −1-1, 00, 11, the first pass makes three updates and ends at w=0w = 0 and b=1b = 1. 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 x2x^2 fixes this. The score x2−0.5x^2 - 0.5 equals 0.50.5 at x=−1x = -1 and x=1x = 1 and −0.5-0.5 at x=0x = 0, so every signed score is 0.50.5.

  1. One feature has the training inputs x=1x = 1 and x=3x = 3 with label +1+1 and x=−1x = -1 with label −1-1. In augmented form z=(x,1)\mathbf{z} = (x, 1), use the unit vector u=(1,0)\mathbf{u} = (1, 0) to compute RR, γ\gamma and the bound of the convergence theorem. Then run the perceptron from zero with η=1\eta = 1 in the order 11, 33, −1-1 and compare.
Show the answer

The augmented vectors are (1,1)(1, 1), (3,1)(3, 1) and (−1,1)(-1, 1), with lengths 2\sqrt{2}, 10\sqrt{10} and 2\sqrt{2}, so R=10R = \sqrt{10}. The values yi u⊤ziy_i\,\mathbf{u}^{\top}\mathbf{z}_i are 11, 33 and 11, so γ=1\gamma = 1 and the bound is (R/γ)2=10(R/\gamma)^2 = 10 updates.

In the first pass, x=1x = 1 has score 00 and triggers an update to w=1w = 1 and b=1b = 1. Then x=3x = 3 has score 44 and is correct. Next x=−1x = -1 has score −1+1=0-1 + 1 = 0, so its signed score is 00 and it triggers an update to w=1−(−1)=2w = 1 - (-1) = 2 and b=1−1=0b = 1 - 1 = 0. The second pass finds the scores 22, 66 and −2-2, all with positive signed scores, so the perceptron stops after 22 updates at the threshold x=0x = 0.

Two updates are well within the bound of 1010, which holds for every order. The theorem guarantees only that the updates stop on these training data. It says nothing about how the threshold x=0x = 0 will classify new inputs.

How this lesson connects