Visualized ML
Topics

A · One neuron

The Perceptron Learning Rule

Nudge the weights toward every example the perceptron gets wrong; if a straight line can separate the data, this always finds one.

Try it: A2 · Learning Rule

Intuition

A perceptron is a line that splits the plane. Picking that line by hand works for four points; for four thousand you want the perceptron to find it itself. The learning rule does this with the simplest possible idea: look at one example at a time, and whenever the perceptron gets it wrong, move the line a little so that this example lands closer to the right side.

If the perceptron says 0 but the answer was 1, the weighted sum was too small, so add the example's inputs to the weights: next time that example (and ones like it) scores higher. If it said 1 but the answer was 0, subtract them. If it was right, change nothing. Repeat over the data until a whole pass makes no mistakes.

The ringed point is the example being checked. When it is on the wrong side, the weights move toward it (or away from it) and the line jumps. Correct examples leave the line alone. After a few passes, no point is misclassified.

The math

Let the labels be \(y \in \{0, 1\}\) and the prediction \(\hat{y} = \operatorname{step}(\mathbf{w}^\top\mathbf{x} + b)\). For each example, with a learning rate \(\eta > 0\):

\[ \mathbf{w} \leftarrow \mathbf{w} + \eta\,(y - \hat{y})\,\mathbf{x}, \qquad b \leftarrow b + \eta\,(y - \hat{y}) \]

The factor \(y - \hat{y}\) is \(+1\) for a missed positive, \(-1\) for a false alarm, and \(0\) when the prediction is right, so the rule only acts on mistakes. With labels \(y \in \{-1, +1\}\) it reads even more simply: if \(y\,(\mathbf{w}^\top\mathbf{x} + b) \le 0\), then \(\mathbf{w} \leftarrow \mathbf{w} + y\,\mathbf{x}\).

Why the update helps

After an update on a missed positive, the example's new score is

\[ (\mathbf{w} + \eta\mathbf{x})^\top\mathbf{x} + (b + \eta) = \mathbf{w}^\top\mathbf{x} + b + \eta\,(\lVert\mathbf{x}\rVert^2 + 1) \]

It went up by a positive amount, so that example is now less wrong. The update never makes the current mistake worse, though it may disturb examples that were right; that is why it takes several passes.

The convergence theorem

Suppose some line separates the data with a margin: there is a unit vector \(\mathbf{w}^*\) with \(y_i\,\mathbf{w}^{*\top}\mathbf{x}_i \ge \gamma > 0\) for every example (labels \(\pm 1\), bias folded into \(\mathbf{w}\)), and every example has \(\lVert\mathbf{x}_i\rVert \le R\). Then the perceptron makes at most

\[ \left(\frac{R}{\gamma}\right)^2 \]

mistakes, whatever order it sees the data in. The proof is two lines of bookkeeping: each mistake grows \(\mathbf{w}^\top\mathbf{w}^*\) by at least \(\gamma\), while \(\lVert\mathbf{w}\rVert^2\) grows by at most \(R^2\); since a cosine can't exceed 1, the number of mistakes \(k\) must satisfy \(k\gamma \le R\sqrt{k}\). A wide margin means few mistakes; a hairline gap means many.

Worked example. Learn AND with \(\eta = 1\), starting from \(\mathbf{w} = (0, 0)\), \(b = 0\). Since \(\operatorname{step}(0) = 0\), the first three examples are right. On \((1, 1)\) with label 1 the perceptron says 0, so \(\mathbf{w} = (1, 1)\), \(b = 1\).

Second pass: \((0, 0)\) now scores \(1 > 0\), a false alarm, so \(b = 0\). Then \((0, 1)\) scores \(1\): false alarm, \(\mathbf{w} = (1, 0)\), \(b = -1\). \((1, 0)\) scores \(0\): right. \((1, 1)\) scores \(0\): missed, so \(\mathbf{w} = (2, 1)\), \(b = 0\). The passes continue like this until it settles on \(\mathbf{w} = (2, 1)\), \(b = -2\): only \((1, 1)\) scores above zero, and a full pass makes no mistakes.

When it fails

  • Data that no line separates. Then there is always a mistake to fix, and the weights never settle; on XOR the line cycles forever. The theorem only promises convergence when a separating line exists.
  • Which line? When many lines separate the data, the perceptron stops at the first one it finds, often one that grazes a point. A line with a wide margin generalises better; support vector machines choose that line on purpose.
  • No confidence. A step outputs 0 or 1, never "probably". Every mistake counts the same, however close it was.

Variants & what came next

  • The pocket algorithm keeps the best weights seen so far "in its pocket", so even on non-separable data it returns something sensible.
  • The averaged and voted perceptron average all the weight vectors along the way, which is far more stable and was a strong text classifier for years.
  • From rule to gradient. Replace the step with a sigmoid and the update becomes \(\mathbf{w} \leftarrow \mathbf{w} + \eta\,(y - \sigma(z))\,\mathbf{x}\): the same shape, but now it is gradient descent on cross-entropy, and every example nudges the weights, a little or a lot depending on how wrong it is. That is logistic regression.

History

Frank Rosenblatt introduced the rule with the perceptron in 1958 and reported it learning to tell apart simple shapes. Albert Novikoff proved the convergence theorem in 1962. The rule's limits on non-separable data, made famous by Minsky and Papert in 1969, were finally sidestepped by smooth neurons and gradient-based learning, but the rule itself lives on: the update "add the input, scaled by the error" is the core of almost every learning algorithm since.

Connections

Builds on

Further reading