TExcelTAD
Home › Math for Deep Learning & LLMs › The Perceptron

The Perceptron and Linear Units

The perceptron (Rosenblatt, 1958) is the ancestor of every neural network: a weighted sum, a threshold, and a learning rule that nudges the weights after each mistake. Its successes and its famous failure (it cannot learn XOR) explain why networks need hidden layers and smooth activations.

The model

A perceptron computes a score and thresholds it:

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

The decision boundary wTx+b=0\mathbf{w}^T\mathbf{x} + b = 0 is a hyperplane with normal vector w\mathbf{w} (Separating Hyperplanes). Every modern neuron keeps the weighted sum and swaps the sign function for a smooth activation.

The learning rule

With labels y∈{−1,+1}y \in \{-1, +1\}, loop over the data and update only on mistakes (y(wTx+b)≤0y(\mathbf{w}^T\mathbf{x} + b) \le 0):

w←w+y x,b←b+y\mathbf{w} \leftarrow \mathbf{w} + y\,\mathbf{x}, \qquad b \leftarrow b + y

This is stochastic (sub)gradient descent on the perceptron loss max⁡(0,−y(wTx+b))\max(0, -y(\mathbf{w}^T\mathbf{x} + b)) with step size 1. The convergence theorem says: if the data is separable with margin γ\gamma and ∥x∥≤R\lVert\mathbf{x}\rVert \le R, the perceptron makes at most (R/γ)2(R/\gamma)^2 mistakes, no matter the order of the data.

python · try it yourself
import numpy as np

rng = np.random.default_rng(0)
X = rng.normal(size=(100, 2))
y = np.where(X[:, 0] + 2 * X[:, 1] > 0.5, 1, -1)
w, b, mistakes = np.zeros(2), 0.0, 0
for epoch in range(100):
    errs = 0
    for xi, yi in zip(X, y):
        if yi * (w @ xi + b) <= 0:
            w, b = w + yi * xi, b + yi
            errs += 1
    mistakes += errs
    if errs == 0:
        break
print(f"converged after {epoch + 1} epochs, {mistakes} updates in total")
print("w =", np.round(w, 3), " b =", b)
print("training accuracy:", np.mean(np.sign(X @ w + b) == y))
converged after 4 epochs, 21 updates in total
w = [2.639 4.821]  b = -1.0
training accuracy: 1.0

The XOR limitation

XOR outputs +1+1 for (0,1)(0, 1) and (1,0)(1, 0) and −1-1 for (0,0)(0, 0) and (1,1)(1, 1). No line separates them: summing the conditions w2+b>0w_2 + b > 0 and w1+b>0w_1 + b > 0 gives w1+w2+2b>0w_1 + w_2 + 2b > 0, while b≤0b \le 0 and w1+w2+b≤0w_1 + w_2 + b \le 0 give w1+w2+2b≤0w_1 + w_2 + 2b \le 0, a contradiction. The perceptron never stops making mistakes.

python · try it yourself
import numpy as np

X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=float)
y = np.array([-1, 1, 1, -1])
w, b = np.zeros(2), 0.0
for epoch in range(1000):
    errs = 0
    for xi, yi in zip(X, y):
        if yi * (w @ xi + b) <= 0:
            w, b = w + yi * xi, b + yi; errs += 1
print("mistakes in the last epoch:", errs)
print("best possible accuracy for any line: 0.75; perceptron accuracy now:", np.mean(np.sign(X @ w + b) == y))
mistakes in the last epoch: 4
best possible accuracy for any line: 0.75; perceptron accuracy now: 0.0

Minsky and Papert's 1969 analysis of such limits slowed the field for a decade. The fix, a hidden layer, is in XOR and Why Depth Helps.

From perceptron to neuron

Replacing sign⁡\operatorname{sign} with the sigmoid and the perceptron loss with cross-entropy gives logistic regression, a single neuron trained by gradient descent. It has a gradient everywhere, gives probabilities, and converges even when the data is not separable.

In software

  • sklearn.linear_model.Perceptron implements the rule above.
  • nn.Linear is the weighted sum of a layer of perceptrons; the threshold is now a separate activation module.
  • Linear probes (a single linear layer trained on frozen features) are used to test what a network's representations encode.

Exercises

Exercise 1: One update

w=(1,−1)\mathbf{w} = (1, -1), b=0b = 0, and the example x=(1,2)\mathbf{x} = (1, 2) has label +1+1. Is it a mistake? If so, give the new weights.

Answer

Yes; w=(2,1)\mathbf{w} = (2, 1), b=1b = 1.

Solution

wTx+b=1−2=−1\mathbf{w}^T\mathbf{x} + b = 1 - 2 = -1, so y⋅score=−1≤0y \cdot \text{score} = -1 \le 0. Update: w+x=(2,1)\mathbf{w} + \mathbf{x} = (2, 1), b=0+1b = 0 + 1.

Exercise 2: Mistake bound

The data has ∥x∥≤5\lVert\mathbf{x}\rVert \le 5 and margin γ=0.5\gamma = 0.5. Bound the number of perceptron mistakes.

Answer

At most 100.

Solution

(R/γ)2=(5/0.5)2=100(R/\gamma)^2 = (5/0.5)^2 = 100.

Exercise 3: AND is learnable (code)

Train a perceptron on AND (output +1+1 only for (1,1)(1, 1)) and print the weights and predictions.

Answer

It converges to weights that separate (1,1)(1, 1) from the rest.

Solution
python · try it yourself
import numpy as np
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=float); y = np.array([-1, -1, -1, 1])
w, b = np.zeros(2), 0.0
for _ in range(20):
    for xi, yi in zip(X, y):
        if yi * (w @ xi + b) <= 0:
            w, b = w + yi * xi, b + yi
print(w, b, np.sign(X @ w + b))
[3. 2.] -4.0 [-1. -1. -1.  1.]

Test yourself

The perceptron updates its weights…

  1. after every example
  2. only on misclassified examples
  3. only at the end of training
  4. never

Why can a single perceptron not learn XOR?

  1. Too few training examples
  2. XOR is not linearly separable
  3. The learning rate is too small
  4. XOR has three classes

For separable data with margin γ\gamma and radius RR, the perceptron makes at most…

  1. R/γR/\gamma mistakes
  2. (R/γ)2(R/\gamma)^2 mistakes
  3. infinitely many mistakes
  4. one mistake