Rosenblatt’s perceptron
Last updated
McCulloch and Pitts's units could compute, but someone had to set their weights by hand. In 1958 Frank Rosenblatt showed how a unit could find its own weights from examples, with a rule so simple it fits on one line: when you get an example wrong, nudge the weights towards the right answer. This lesson writes that rule in NumPy, trains it on two penguin species, and finds the one kind of data where it never stops.
By the end of this lesson you can
- Write the perceptron's prediction as the sign of a weighted sum plus a bias
- Apply the perceptron learning rule, and explain why it moves the line the right way
- Train a perceptron in NumPy until an epoch with no mistakes
- Recognise data that isn't linearly separable, where the perceptron never settles
The Mark I Perceptron
Frank Rosenblatt was a psychologist at the Cornell Aeronautical Laboratory. In 1958 he published the perceptron: a McCulloch–Pitts unit (lesson 5.1) whose weights weren't set by hand, but learned from examples, with a rule that changed them a little every time the unit got an example wrong.
He first ran it as a program on an IBM 704, a room-sized computer. Then he built it as a machine, the Mark I Perceptron: a camera made of a 20 × 20 grid of light sensors, 400 inputs, wired to units whose weights were potentiometers (volume knobs) turned by electric motors as it learned. Shown cards marked with shapes or letters, it learned to tell them apart.
It caused a sensation. Newspapers reported a machine that would soon walk, talk and be conscious of its own existence. It wouldn't, and the gap between those claims and what one layer of units can do is part of the story of lesson 5.6. But the learning rule was real, and it works.
Weighted sum and step
Two changes from lesson 5.1 make the maths tidier. The inputs can be any numbers, not just 0 and 1. And the threshold moves to the other side of the inequality, as a bias : "fire if " is the same as "fire if ". Then the bias is just one more number to learn, like a weight.
With the two classes labelled and , the perceptron predicts the sign of the weighted sum:
That's a step function: +1 on one side of the line and −1 on the other. With two features the boundary is a line; with three, a plane; with more, a hyperplane. It's the same boundary as logistic regression's (lesson 3.9), but with no probability: just a side.
The learning rule: nudge on mistakes only
Start with every weight and the bias at 0. Go through the examples one at a time. For each:
- If the prediction is right, change nothing.
- If it's wrong, move the weights towards the example: add to the weights and to the bias, where (eta) is the learning rate.
An example is wrong when and have different signs, that is when . (Exactly 0, on the line, counts as wrong, which is what gets the weights moving from all zeros.)
Why does the nudge help? After it, the same example's score is
because . The second term is always positive, so the example has moved towards the right side of the line. It may not be right yet, and the nudge may spoil other examples, but each step pushes the line the right way for the example in front of it.
One pass through all the examples is an epoch. Shuffle the order each epoch (lesson 3.6), and stop when an epoch makes no mistakes. Watch the line:
Epoch 1, 0 updates so far. The line gets 274 of 274 wrong.
On Adelie against Gentoo, "Next mistake" shows the line jumping, the point that caused it ringed. After a few updates the mistakes stop: an epoch passes with nothing wrong, and the perceptron is done.
Training on two penguin species
The same in NumPy: Adelie (−1) against Gentoo (+1), by bill depth and flipper length, both scaled (lesson 3.7):
274 penguins. The first epoch makes 3 mistakes, and the second none, so it stops. The weights are for bill depth and for flipper length, with a bias of : a Gentoo is a penguin with a shallow bill and long flippers. Every penguin is now on the right side:
100% on the training data. Notice what the perceptron didn't do: it stopped at the first line with no mistakes, not the best one. A line that only just clears the nearest Gentoo is as good as one in the middle of the gap, as far as the rule is concerned. The SVM (lesson 4.7) chooses the line with the widest gap, which is why it usually does better on new data.
Wrong way: data that isn't separable
Now Adelie against Chinstrap, by bill length and depth. In the figure in step 3, pick "Adelie against Chinstrap" and press "Run an epoch" a few times. The same in NumPy, for 20 epochs:
13 mistakes in the first epoch, then 8, 11, 7, 10, … and never 0. A few Adelies and Chinstraps overlap, so no line gets them all right, and there's always a mistake to nudge for. The perceptron never converges: the line keeps jumping as long as you let it run. Where it ends up depends on when you stop:
96.8% after 20 epochs, 97.3% after 21, 96.8% after 22, 97.7% after 23. Each stop gives a different line, and nothing in the rule says which is best.
The fixes:
- Stop after a fixed number of epochs, as above, and accept a line that depends on when you stopped.
- Keep the best weights seen: Gallant's "pocket algorithm" (1990) keeps a copy of the weights that got the most examples right so far.
- Use a rule that settles: Adaline (lesson 5.4) and logistic regression move by an amount that shrinks as they get closer, so they converge to one answer even when the classes overlap.
Real data almost always overlaps, which is why the plain perceptron is rarely used today. Its rule, though, is the ancestor of every training rule in this course.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Rosenblatt, The perceptron: a probabilistic model for information storage and organization in the brain, Psychological Review 65(6), 1958: the perceptron and its learning rule
- Block, The perceptron: a model for brain functioning. I, Reviews of Modern Physics 34, 1962: the perceptron explained for physicists, with the Mark I's design
- Gallant, Perceptron-based learning algorithms, IEEE Transactions on Neural Networks 1(2), 1990: the pocket algorithm: keeping the best weights on data that isn't separable
- scikit-learn: Perceptron: the same rule, in a library (lesson 5.5)