Perceptron

Rosenblatt's perceptron learning live, one sample per step: every mistake-driven update, the step output, the path through weight space, Novikoff's bound — and why XOR defeats it.

Updated

  • The artificial neuron: weighted sum and threshold
  • Learning only from mistakes
  • Novikoff's convergence bound (R/γ)²
  • Weight space and constraint half-spaces
  • Why a single neuron cannot learn XOR
update 1update 2update 3update 4update 5w ← w + η·y·x
Preparing the lab…

Intuition

One neuron that learns from its mistakes

The perceptron looks at one sample and computes w1x1+w2x2+bw_1x_1 + w_2x_2 + b: above 0 it says +1, otherwise −1. If it is right, nothing changes; if it is wrong, the boundary is nudged towards that sample. It learns only from mistakes.

The three small labs below really look at the samples one at a time in your browser. Blue points are +1, orange points −1, the background is the current weights' verdict, the thick line is the boundary, red rings mark points currently misclassified and the black ring is the sample just seen. All three use the same linearly separable data.

First samplePreparing…
First 5 samplesPreparing…
ConvergedPreparing…

At the start w=0w = 0, every point scores 0, so the first sample is always "wrong" and triggers the first update: 60% accuracy. After 5 samples there have been 3 updates and accuracy is 81.7%. After 14 updates in all, a whole round of 60 samples passes without a mistake and the perceptron stops: 108 samples seen, 100% accuracy.

Math

The learning rule

Labels y∈{−1,+1}y \in \{-1, +1\}, weights w=(w1,w2)w = (w_1, w_2), bias bb. For each sample (x,y)(x, y):

if  y (w⋅x+b)≤0:w←w+η y x,b←b+η y\text{if}\; y\,(w\cdot x + b) \le 0:\qquad w \leftarrow w + \eta\, y\, x,\quad b \leftarrow b + \eta\, y

A score of exactly 0 counts as wrong (as in scikit-learn). Why does this help? After the update the sample's score becomes

y (w′⋅x+b′)=y (w⋅x+b)+η (∥x∥2+1)y\,(w'\cdot x + b') = y\,(w\cdot x + b) + \eta\,(\|x\|^2 + 1)

The extra term is always positive: the sample has moved one step closer to being classified correctly.

Starting from w=0w = 0, the learning rate η\eta only scales the weights up or down; the boundary and the number of mistakes do not change. With η=0.1\eta = 0.1 there are again 14 updates, and the final weights are exactly one tenth of those with η=1\eta = 1.

Novikoff's theorem: separable means convergent

Fold the bias in by writing x~=(x1,x2,1)\tilde x = (x_1, x_2, 1). If some unit vector uu gives every sample y u⋅x~≥γ>0y\, u\cdot\tilde x \ge \gamma > 0 (a margin of γ\gamma), and ∥x~∥≤R\|\tilde x\| \le R, then starting from w=0w = 0 the number of perceptron updates satisfies

k≤(Rγ)2k \le \left(\frac{R}{\gamma}\right)^2

The proof takes two steps: each update increases w⋅uw\cdot u by at least γ\gamma, while ∥w∥2\|w\|^2 grows by at most R2R^2; squeezing the two together gives the bound.

DataγRBoundActual updatesRounds
Separable0.1896.991368141
Narrow margin0.08726.806084293

The bound is usually loose, but it guarantees convergence; the narrower the margin, the larger the bound, and the more updates are needed in practice. γ is solved exactly from the hard-margin problem (that is, an SVM).

When the data are inseparable: pocket and averaging

On inseparable data some sample is always wrong, so the weights keep being pushed around. Two common fixes:

  • pocket algorithm (Gallant, 1990): after each update, if the new weights have higher training accuracy, put them "in the pocket", and output the pocket weights at the end;
  • averaged perceptron: output the average of the weights over every step — smoother, and an approximation to Freund and Schapire's voted perceptron.

After 60 rounds:

DataClassicPocketAveraged
Separable100%100%96.67%
Noisy labels (about 8% flipped)90.00%91.67%90.00%
XOR51.67%75.00%25.00%

On the noisy data the classic perceptron makes 794 updates in 60 rounds and still errs a dozen-odd times per round. On XOR the averaged perceptron reaches only 25%: the weights circle round, and their average actually points the wrong way. Even on separable data the averaged perceptron falls short of 100%, because it also counts the poor early weights.

Noisy, classicPreparing…
Noisy, pocketPreparing…
XOR, pocketPreparing…

How it is computed, with real numbers

Once the lab above has loaded, this panel works through the same formulas with its current numbers.

Experiment

Follow the lesson

Course

Lesson 1: learning from mistakes

5 steps · each one: set up and run → look → answer

  1. 1. The first sample

    Default separable data, one step. Look at "How it is computed" below.

    With w = 0, why does the first sample trigger an update?
  2. 2. One full round

    Go through one round (60 samples) and open "Mistakes per epoch".

    The first round made 14 updates. Why does the second make none?
  3. 3. Narrow margin

    Switch to "Narrow margin" and run to convergence.

    Why does it need 29 updates and 3 rounds (the separable data needed 14)?
  4. 4. XOR

    Switch to "XOR" and run 600 steps.

    Why does the boundary keep turning?
  5. 5. Into the pocket

    Set the output to "Pocket" and run all 60 rounds.

    The pocket gives 75%. Why can't it do better?

Try these settings

You can also drag data points in the lab: drag a point into the other class and separability and the bound are recomputed at once.

Exercises

Exercises

Exercise 1: a good answer in the noise

On the noisy-label data the classic perceptron reaches only 90% after 60 rounds. Change the output to reach at least 91.5% training accuracy.

Hint

The "Pocket" output: 91.67%.

  • Rounds ≥ 60
  • Training accuracy ≥ 91.5%

Exercise 2: fewer detours

On the separable data, the fixed order needs 14 updates to classify everything correctly. Without changing the data, get every point right with at most 12 updates.

Hint

The learning rate is no help (starting from 0 it does not change the number of updates). Try the sample order: shuffling every round needs only 11.

  • Training accuracy ≥ 100.0%
  • Updates ≤ 12

Quiz

When does the perceptron change its weights?
Starting from w = 0, what happens if you change the learning rate η from 1 to 0.1?
When does Novikoff's bound (R/γ)² get larger?

Further reading

Frequently asked questions

What is the difference between a perceptron and logistic regression?

Both draw a straight decision boundary. The perceptron outputs ±1 through a step function, updates only on mistakes and gives no probabilities; logistic regression outputs probabilities through a sigmoid and minimises cross-entropy by gradient descent, with every sample contributing. On separable data the perceptron stops at any feasible line, while unregularised logistic regression keeps growing its weights.

Does the perceptron always converge?

On linearly separable data, yes, with at most (R/γ)² updates (Novikoff's theorem). On inseparable data it never converges and the weights keep swinging; use the pocket algorithm, the averaged perceptron or a limit on the number of rounds.

Why can't a single-layer perceptron solve XOR?

XOR's two classes sit crosswise, and no straight line separates them. Minsky and Papert's 1969 book Perceptrons analysed this kind of limitation of single-layer perceptrons systematically. Adding a hidden layer (a multi-layer perceptron) solves it.

How is the perceptron related to the support vector machine?

Both look for a line separating two classes. The perceptron stops at the first one it finds; the support vector machine finds the one with the largest margin. The γ this page uses for Novikoff's bound is exactly the hard-margin SVM solution.

How does the perceptron relate to modern neural networks?

Every neuron in a modern network is still "weighted sum plus non-linearity"; the step function has been replaced by differentiable ones such as ReLU or sigmoid, and backpropagation trains many layers at once. The perceptron is where it all started.

Further reading