SECTION 03 / 4

Perceptron Learning Algorithm and Its Proof of Convergence

8 min read
On this page 8 headings
BUILD INTUITION WITH NUMBERS

Correct one mistaken prediction

Include the constant bias input. Start with weights [−1, 0], augmented input [1, 2], and true label 1. Predict 1 when the score is at least zero.

  1. The current prediction is wrong.

    wTx=(−1)(1)+0(2)=−1⇒y^=0w^Tx=(-1)(1)+0(2)=-1\quad\Rightarrow\quad\hat y=0
  2. For a missed positive point, add the input to the weights.

    wnew=[−1,0]+[1,2]=[0,2]w_{new}=[-1,0]+[1,2]=[0,2]
  3. Check the same point after the correction.

    wnewTx=0(1)+2(2)=4⇒y^=1w_{new}^Tx=0(1)+2(2)=4\quad\Rightarrow\quad\hat y=1

What changed? The update changes a concrete mistaken decision. It does not guarantee that every other point is now correct.

Quick check: If this point had true label 0, would the original weights need an update?

No. The original score is negative, so its prediction of 0 would already be correct.

Swipe long formulas, tables or code sideways to see the rest.

Start here We don’t want to guess weights by hand. We want the perceptron to look at examples and fix itself whenever it makes a mistake. The Perceptron Learning Algorithm (PLA) does exactly that, using a simple rule: add the point if you wrongly said 0, subtract it if you wrongly said 1.

1. The perceptron as a binary classifier

Lecture example — movie data We have m movies. For each movie we know whether the user liked it (1) or not (0). Each movie is described by n features, some 0/1 and some real: x1=x_{1} = isActorDamon, x2=x_{2} = isGenreThriller, x3=x_{3} = isDirectorNolan, x4=x_{4} = imdbRating (scaled 0−10-1), …, x_{n} = criticsRating (scaled 0–1). We assume the data is linearly separable and want the perceptron to find the separating plane, i.e. find w0,w1,…,wnw_{0}, w_{1}, \ldots , w_{n}.

This is called binary classification: put each input into one of 2 classes.

2. The algorithm

P ← inputs with label 1
N ← inputs with label 0
Initialise w randomly
while not converged:
    pick a random x from P ∪ N
    if x ∈ P and wᵀx < 0:    w = w + x
    if x ∈ N and wᵀx ≥ 0:    w = w − x
# converged = every input is classified correctly

Here wTx=w0x0+w1x1+…+wnxnw^{T}x = w_{0}x_{0} + w_{1}x_{1} + \ldots + w_{n}x_{n} (the dot product), with x0=1x_{0} = 1.

In words:

Situation Mistake Fix
Positive point, but wTx<0w^{T}x < 0 said 0, should say 1 w=w+xw = w + x
Negative point, but wTx≥0w^{T}x \ge 0 said 1, should say 0 w=w−xw = w - x
Correct answer none do nothing

3. Why does it work? The angle picture

A little vector geometry. Write w=[w0,w1,…,wn]w = [w_{0}, w_{1}, \ldots , w_{n}] and x=[1,x1,…,xn]x = [1, x_{1}, \ldots , x_{n}].

The rule is now: y=1y = 1 if wTx≥0w^{T}x \ge 0, else 0.

The boundary is the line wTx=0w^{T}x = 0. For any point x on this line, cos⁡α=wTx/(∥w∥∥x∥)=0\cos \alpha = w^{T}x / (\Vert w\Vert \Vert x\Vert ) = 0, so the angle α between w and x is 90°. Since w is at 90° to every point on the line, w is perpendicular to the line itself.

Positive points should make an angle less than 90° with w; negative points more than 90°.
Positive points should make an angle less than 90° with w; negative points more than 90°.
Open image ↗
w perpendicular to the line; positive points within 90 degrees of w, negative points beyond 90 degrees
  • Points with wTx>0w^{T}x > 0 (positive side): angle with w is less than 90°.
  • Points with wTx<0w^{T}x < 0 (negative side): angle with w is more than 90°.

Case 1:x∈P1: x \in P but wTx<0w^{T}x < 0 (angle >90∘> 90{}^\circ , we want <90∘< 90{}^\circ ). New weight wnew=w+xw_{\mathrm{new}} = w + x:

cos⁡(αnew)\cos (\alpha _{\mathrm{new}}) ∝ (w+x)Tx=wTx+xTx(w + x)^{T}x = w^{T}x + x^{T}x

xTx=∥x∥2x^{T}x = \Vert x\Vert ^{2} is always positive, so cos⁡(αnew)>cos⁡(α)\cos (\alpha _{\mathrm{new}}) > \cos (\alpha ). A bigger cosine means a smaller angle. w turns towards x. ✔

Case 2:x∈N2: x \in N but wTx≥0w^{T}x \ge 0 (angle <90∘< 90{}^\circ , we want >90∘> 90{}^\circ ). New weight wnew=w−xw_{\mathrm{new}} = w - x:

cos⁡(αnew)\cos (\alpha _{\mathrm{new}}) ∝ (w−x)Tx=wTx−xTx(w - x)^{T}x = w^{T}x - x^{T}x

Socos⁡(αnew)<cos⁡(α)→So \cos (\alpha _{\mathrm{new}}) < \cos (\alpha ) \to bigger angle. w turns away from x. ✔

(Small note: the slides write “∝ cos⁡α+xTx\cos \alpha + x^{T}x”. Strictly, it is wTxw^{T}x that grows by xTxx^{T}x; the cosine also has ∥wnew∥\Vert w_{\mathrm{new}}\Vert in the denominator. The direction of the change is what matters, and the idea is right.)

4. Worked run on OR (numbers)

Start with w=[0,0,0]w = [0, 0, 0], go through the points in the order (0,0), (0,1), (1,0), (1,1), again and again. Each x is written as [1,x1,x2][1, x_{1}, x_{2}].

# Point Label wTxw^{T}x Action New w
1 (0,0) 0 00 (≥ 0, wrong) w−x=[0,0,0]−[1,0,0]w - x = [0,0,0] - [1,0,0] [−1, 0, 0]
2 (0,1) 1 −1-1 (< 0, wrong) w+x=[−1,0,0]+[1,0,1]w + x = [-1,0,0] + [1,0,1] [0, 0, 1]
3 (1,0) 1 0(≥0,ok)0 (\ge 0, ok) none [0, 0, 1]
4 (1,1) 1 1 (ok) none [0, 0, 1]
5 (0,0) 0 0 (wrong) w−xw - x [−1, 0, 1]
6 (0,1) 1 0 (ok) none [−1, 0, 1]
7 (1,0) 1 −1 (wrong) w+x=[−1,0,1]+[1,1,0]w + x = [-1,0,1] + [1,1,0] [0, 1, 1]
8 (1,1) 1 2 (ok) none [0, 1, 1]
9 (0,0) 0 0 (wrong) w−xw - x [−1, 1, 1]
10–16 rest of this pass, then one full pass (0,0): −1, (0,1): 0, (1,0): 0, (1,1): 1 none — all correct [−1, 1, 1]

Final: w0=−1,w1=1,w2=1w_{0} = -1, w_{1} = 1, w_{2} = 1, i.e. fire when x1+x2≥1x_{1} + x_{2} \ge 1 — exactly OR. Only 5 corrections were needed.

Lecture example — the toy run In the lecture’s animation, w starts pointing the wrong way (all positive points at more than 90°). Picking p1,p2p_{1}, p_{2} gives w+xw + x corrections; n1n_{1} gives aw−xa w - x correction; n3n_{3} and n2n_{2} need nothing; p3p_{3} gives one more w+xw + x. After that, a full pass over all points makes no change → the algorithm has converged.

5. Proof of convergence (why it must stop)

Definition — absolutely linearly separable: sets P and N in n dimensions are absolutely linearly separable if there are n+1n + 1 numbers w0,…,wnw_{0}, \ldots , w_{n} such that every point in P has ∑i=1nwixi\sum _{i=1}^{n} w_{i}x_{i} >> w0w_{0} and every point in N has ∑i=1nwixi<w0\sum _{i=1}^{n} w_{i}x_{i} < w_{0}.

Proposition: if P and N are finite and linearly separable, the PLA updates w only a finite number of times. (Test the points one after another in cycles; after finitely many steps, w separates the two sets.)

Setup tricks:

  1. Ifx∈NIf x \in N, then −x behaves like a positive point (wTx<0w^{T}x < 0 ⇔ wT(−x)>0w^{T}(-x) > 0). So make one set P′=P∪N−P' = P ∪ N^{-} (N−=N^{-} = negated points of N). Now we only need wTp≥0w^{T}p \ge 0 for every p∈P′p \in P', and only one “if” in the algorithm.
  2. Normalise every p so that ∥p∥=1\Vert p\Vert = 1. This does not change the answer (if wT(p/∥p∥)≥0w^{T}(p/\Vert p\Vert ) \ge 0 then wTp≥0w^{T}p \ge 0).
  3. Let w* be a normalised solution. It exists because the data is separable — we just don’t know it.

Proof idea. LetβLet \beta be the angle between w* and the current w. Suppose by step t we have made k corrections (k≤t)(k \le t). Look at

cos⁡β=(w∗⋅wt+1)/∥wt+1∥(∥w∗∥=1)\cos \beta = (w* \cdot w_{t+1}) / \Vert w_{t+1}\Vert (\Vert w*\Vert = 1)

Numerator grows at least linearly. At a correction, wt+1=wt+piw_{t+1} = w_t + p_{i}, so w∗⋅wt+1=w∗⋅wt+w∗⋅pi≥w∗⋅wt+δw* \cdot w_{t+1} = w* \cdot w_t + w* \cdot p_{i} \ge w* \cdot w_t + \delta , where δ=min⁡\delta = \min over i of (w∗⋅pi)>0(w* \cdot p_{i}) > 0. Repeating for every correction: numerator ≥w∗⋅w0+kδ\ge w* \cdot w_{0} + k\delta .

Denominator grows slowly. ∥wt+1∥2=∥wt∥2+2wt⋅pi+∥pi∥2≤∥wt∥2+1\Vert w_{t+1}\Vert ^{2} = \Vert w_t\Vert ^{2} + 2 w_t \cdot p_{i} + \Vert p_{i}\Vert ^{2} \le \Vert w_t\Vert ^{2} + 1 (because we only correct when wt⋅pi≤0w_t \cdot p_{i} \le 0, and ∥pi∥2=1\Vert p_{i}\Vert ^{2} = 1). Repeating: ∥wt+1∥2≤∥w0∥2+k\Vert w_{t+1}\Vert ^{2} \le \Vert w_{0}\Vert ^{2} + k.

Put together:

cos⁡β≥(w∗⋅w0+kδ)/∥w0∥2+k\cos \beta \ge (w* \cdot w_{0} + k\delta ) / \sqrt{\Vert w_{0}\Vert ^{2} + k}

The top grows like k, the bottom only like k\sqrt{k}. So the right side grows like k\sqrt{k} and would become bigger than 1 for large k. But cos⁡β≤1\cos \beta \le 1 always. So k cannot keep growing — there is a maximum number of corrections, and the algorithm converges.

(The slide writes the denominator as ∥w0∥2+k\Vert w_{0}\Vert ^{2} + k; it should be under a square root, since the denominator of cos⁡β\cos \beta is ‖w‖, not ∥w∥2\Vert w\Vert ^{2}. The conclusion is the same.)

6. Our four questions — answered so far

Question Answer
Non-Boolean (real) inputs? Allowed in the perceptron ✔
Must we hand-code the threshold? No, it is learned as w0w_{0} ✔
Can some inputs matter more? Yes, through weights ✔
Functions that are not linearly separable? Not possible with one perceptron ✘ → need a network (next page)

In one line: PLA adds misclassified positive points to w and subtracts misclassified negative points; for linearly separable data it is guaranteed to stop after a finite number of corrections.

Exam words: binary classifier, Perceptron Learning Algorithm, convergence, dot product wTxw^{T}x, angle between vectors, normalisation, absolutely linearly separable, P′=PP' = P ∪ N−,δN^{-}, \delta , finite number of updates.

Quick check

  1. w=[1,−2,1]w = [1, -2, 1], positive point x=(1,1)x = (1, 1). What does PLA do?
x=[1,1,1]x = [1, 1, 1]. wTx=1−2+1=0≥0w^{T}x = 1 - 2 + 1 = 0 \ge 0, so it is classified as 1. It is a positive point → correct → no update.
  1. w=[1,−2,1]w = [1, -2, 1], negative point x=(0,1)x = (0, 1). What does PLA do?
x=[1,0,1]x = [1, 0, 1]. wTx=1+0+1=2≥0→w^{T}x = 1 + 0 + 1 = 2 \ge 0 \to says 1, but label is 0. Update w=w−x=[0,−2,0]w = w - x = [0, -2, 0].
  1. Why does w+xw + x reduce the angle between w and x?
(w+x)Tx=wTx+∥x∥2(w + x)^{T}x = w^{T}x + \Vert x\Vert ^{2}, which is bigger than wTxw^{T}x because ∥x∥2>0\Vert x\Vert ^{2} > 0. A larger dot product means a larger cosine, i.e. a smaller angle.
  1. Will PLA converge on XOR data?
No. The convergence proof needs linearly separable data. XOR is not linearly separable, so PLA keeps making corrections forever.
  1. In the proof, the numerator grows like ___ and the denominator like ___.
Numerator like k(kδ)k (k\delta ); denominator like k\sqrt{k}. Socos⁡βSo \cos \beta grows like k\sqrt{k}, which can’t pass 1 — hence k is bounded.
YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑