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:
The decision boundary is a hyperplane with normal vector (Separating Hyperplanes). Every modern neuron keeps the weighted sum and swaps the sign function for a smooth activation.
The learning rule
With labels , loop over the data and update only on mistakes ():
This is stochastic (sub)gradient descent on the perceptron loss with step size 1. The convergence theorem says: if the data is separable with margin and , the perceptron makes at most mistakes, no matter the order of the data.
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 for and and for and . No line separates them: summing the conditions and gives , while and give , a contradiction. The perceptron never stops making mistakes.
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 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.Perceptronimplements the rule above.nn.Linearis 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
, , and the example has label . Is it a mistake? If so, give the new weights.
Answer
Yes; , .
Solution
, so . Update: , .
Exercise 2: Mistake bound
The data has and margin . Bound the number of perceptron mistakes.
Answer
At most 100.
Solution
.
Exercise 3: AND is learnable (code)
Train a perceptron on AND (output only for ) and print the weights and predictions.
Answer
It converges to weights that separate from the rest.
Solution
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…
- after every example
- only on misclassified examples
- only at the end of training
- never
Why can a single perceptron not learn XOR?
- Too few training examples
- XOR is not linearly separable
- The learning rate is too small
- XOR has three classes
For separable data with margin and radius , the perceptron makes at most…
- mistakes
- mistakes
- infinitely many mistakes
- one mistake