Why the Update Works: Dot Products and Angles

The geometry behind add and subtract, then a full worked run of the algorithm on AND.

Deep Learning- Fundamentals to Advanced Concepts

The update rule was stated without a reason. Let us get one, using a little linear algebra.

The weighted sum is a dot product

With weights w=(w0,…,wn)w = (w_0, \dots, w_n) and an example x=(1,x1,…,xn)x = (1, x_1, \dots, x_n), the weighted sum is the dot product

w⋅x=∑i=0nwixiw \cdot x = \sum_{i=0}^{n} w_i x_i

So the perceptron rule is just: output 1 if w⋅x≥0w \cdot x \ge 0, output 0 if w⋅x<0w \cdot x < 0.

Angles decide the side

The dot product is also w⋅x=∥w∥ ∥x∥cos⁡αw \cdot x = \|w\|\,\|x\|\cos\alpha, where α\alpha is the angle between the two vectors. Since lengths are positive, the sign of w⋅xw \cdot x is the sign of cos⁡α\cos\alpha. Therefore:

  • w⋅x>0w \cdot x > 0 means α<90∘\alpha < 90^\circ: the example is on the positive side,
  • w⋅x<0w \cdot x < 0 means α>90∘\alpha > 90^\circ: the example is on the negative side,
  • w⋅x=0w \cdot x = 0 means α=90∘\alpha = 90^\circ: the example lies on the boundary.

The last line says something about the boundary w⋅x=0w \cdot x = 0: every point on it is perpendicular to ww. So the weight vector ww is orthogonal to the decision boundary, pointing into the positive side.

A vertical decision boundary with the weight vector w pointing right, positive points at acute angles to w and negative points at obtuse angles
w is perpendicular to the boundary. Positives sit within 90 degrees of it, negatives beyond.

So learning has a clear target: make every positive example within 90∘90^\circ of ww and every negative example beyond 90∘90^\circ.

What the update does

Missed positive (w⋅x<0w \cdot x < 0, but it should be at least 0). We set wnew=w+xw_{\text{new}} = w + x. Then

wnew⋅x=w⋅x+x⋅x=w⋅x+∥x∥2w_{\text{new}} \cdot x = w \cdot x + x \cdot x = w \cdot x + \|x\|^2

The score for this example rises by ∥x∥2\|x\|^2, which is always positive. The example has moved toward the correct side. Geometrically, adding xx tilts ww toward xx, so the angle between them shrinks.

Accepted negative (w⋅x≥0w \cdot x \ge 0, but it should be negative). We set wnew=w−xw_{\text{new}} = w - x, so

wnew⋅x=w⋅x−∥x∥2w_{\text{new}} \cdot x = w \cdot x - \|x\|^2

and the score falls by ∥x∥2\|x\|^2, pushing the example toward the negative side.

One update may not be enough

A single step need not fully fix the example, because the score may still be on the wrong side. It only moves it in the right direction. The algorithm keeps cycling through the data, so later steps finish the job.

There is a catch. Fixing one example can hurt another that was already correct, since the same ww serves them all. Nothing so far says the algorithm cannot go round in circles. The next lesson shows that it cannot, when the data is separable.

A full run on AND

Let us run the algorithm by hand. Write each example with its leading 1, so AND has the points (1,0,0)(1,0,0), (1,0,1)(1,0,1), (1,1,0)(1,1,0) with label 0 and (1,1,1)(1,1,1) with label 1. Start from w=(0,0,0)w = (0, 0, 0) and go through the examples in this order every pass. Note that with w=0w = 0, every score is 00, which counts as 1.

Pass 1

ExampleLabelScore w⋅xw \cdot xActionNew ww
(1,0,0)00 (predicts 1)subtract(−1, 0, 0)
(1,0,1)0−1 (predicts 0)none(−1, 0, 0)
(1,1,0)0−1 (predicts 0)none(−1, 0, 0)
(1,1,1)1−1 (predicts 0)add(0, 1, 1)

Pass 2

ExampleLabelScoreActionNew ww
(1,0,0)00 (predicts 1)subtract(−1, 1, 1)
(1,0,1)00 (predicts 1)subtract(−2, 1, 0)
(1,1,0)0−1 (predicts 0)none(−2, 1, 0)
(1,1,1)1−1 (predicts 0)add(−1, 2, 1)

Passes 3, 4 and 5 continue the same way. After pass 5 the weights are w=(−3,2,1)w = (-3, 2, 1), and pass 6 makes no corrections, so the algorithm has converged. Check it:

InputScore with (−3,2,1)(-3, 2, 1)OutputAND
(0,0)−300
(0,1)−200
(1,0)−100
(1,1)011

All four are right. The boundary −3+2x1+x2=0-3 + 2x_1 + x_2 = 0 is a different line from the textbook one, but it separates the classes just as well. There are many valid answers, and the algorithm stops at the first one it finds.

Try it yourself
Perceptron learning simulator →

Watch the weight vector turn after each mistake, and check that the angles end up on the correct sides.

MediumGeometry

Why is the weight vector perpendicular to the decision boundary?

MediumPLAMath

Show that adding a positive example x to w increases that example's score.

EasyPLA

Why might the algorithm take different numbers of passes from different random starting weights?