Skip to content
Lambdian

The perceptron

The original learning machine: a linear threshold unit that updates only when it makes a mistake.

Animated explanation

The idea

The perceptron predicts +1+1 if w⊤x+b>0\mathbf{w}^\top\mathbf{x} + b > 0 and −1-1 otherwise. Its learning rule is almost childishly simple: go through the examples; whenever one is misclassified, add it to the weights (for a positive) or subtract it (for a negative). Correct predictions change nothing.

Each update tilts the boundary towards classifying that point correctly. If some line separates the classes perfectly, the perceptron is guaranteed to find one in a finite number of updates. If no such line exists, it never settles. That limitation, and the fix (multiple layers), is the origin story of neural networks.

The convergence theorem

Suppose a unit vector w∗\mathbf{w}^\ast separates the data with margin γ\gamma (every yi w∗⊤xi≥γy_i\,\mathbf{w}^{\ast\top}\mathbf{x}_i \ge \gamma) and every ∥xi∥≤R\lVert\mathbf{x}_i\rVert \le R. Then the perceptron makes at most (R/γ)2(R/\gamma)^2 mistakes, regardless of the dimension or the order of the data. Each update increases w⊤w∗\mathbf{w}^\top\mathbf{w}^\ast by at least γ\gamma while ∥w∥2\lVert\mathbf{w}\rVert^2 grows by at most R2R^2; both bounds together cap the number of updates.

Limits and fixes

  • On non-separable data it cycles forever. The pocket algorithm keeps the best weights seen; the averaged perceptron averages all weight vectors and generalises much better.
  • It finds a separating line, not the best one. The SVM picks the line with the largest margin.
  • One layer cannot represent XOR. Two layers with smooth activations can: the multi-layer perceptron.

The algorithm, step by step

  1. 1
    Encode labels as y∈{−1,+1}y \in \{-1, +1\}; set w=0\mathbf{w} = 0, b=0b = 0.
  2. 2
    For each epoch, visit the examples (shuffled).
  3. 3
    If yi(w⊤xi+b)≤0y_i(\mathbf{w}^\top\mathbf{x}_i + b) \le 0: w←w+yixi\mathbf{w} \leftarrow \mathbf{w} + y_i\mathbf{x}_i, b←b+yib \leftarrow b + y_i.
  4. 4
    Stop after an epoch with no mistakes (or after a maximum number of epochs).

The maths

Prediction

y^=sign⁡(w⊤x+b)\hat y = \operatorname{sign}(\mathbf{w}^\top\mathbf{x} + b)

Update on a mistake

w←w+yixi,b←b+yi\mathbf{w} \leftarrow \mathbf{w} + y_i\mathbf{x}_i,\qquad b \leftarrow b + y_i

Mistake bound

k≤R2/γ2k \le R^2/\gamma^2

Strengths and weaknesses

Strengths

  • Extremely simple and fast; online (one example at a time)
  • Guaranteed to converge on separable data
  • Historically the building block of neural networks

Weaknesses

  • Never converges on non-separable data
  • No probabilities; arbitrary separating line (no margin)
  • Linear boundaries only

Reach for it when…

Teaching, streaming problems with near-separable data, or as the averaged perceptron for fast linear text classification.

Check your understanding

Question 1

Q1A positive example (y=+1y = +1) is misclassified. The perceptron…

Question 2

Q2Which dataset can a single perceptron never classify perfectly?

Question 3

Q3What does the mistake bound (R/γ)2(R/\gamma)^2 depend on?

Lambdian Lab

Train a perceptron and count its mistakes

Labels are ±1.

  • fit_perceptron(X, y, epochs): cycle through the data in order; return (w, b, mistakes) where mistakes is the total number of updates. Stop early after a clean epoch.
  • predict(X, w, b) → ±1 (treat a score of exactly 0 as −1).
  • On XOR the perceptron cannot reach 100% accuracy: show it.
Open the labPython + NumPy + scikit-learn runs in your browser. Sign-in keeps your code and attempts.