Definition
The perceptron, introduced by Frank Rosenblatt in 1958, is the simplest neural network: one neuron that computes a weighted sum of its inputs and outputs 1 if the total passes a threshold, otherwise 0. It can only learn patterns separable by a straight line.
In simple words
Should you go to the cricket match? You weigh a few things: is it a weekend, are friends going, is it raining? Good reasons add points, and bad ones take them away. If the total passes your limit, you go. That's a perceptron.
Why it matters
It's where neural networks began. Its best-known limit, the inability to solve XOR, was highlighted by Minsky and Papert in 1969 and led to the idea of hidden layers. Every neuron in a modern network is a descendant of this one.
How it works
- Start with zero or random weights.
- For each example, make a prediction.
- If it's wrong, move the weights towards the right answer. If it's right, change nothing.
- Repeat until it stops making mistakes.
| x₁ | x₂ | AND | OR | XOR |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
A straight line can separate AND and OR, so a perceptron learns them. No single line can separate XOR, and so it fails. Add one hidden layer and the problem goes away.
The maths
- 0 when right, and +1 or −1 when wrong
- The learning rate
In code
def train(data, epochs=10, rate=0.1):
w = [0.0, 0.0]
b = 0.0
for _ in range(epochs):
for (x1, x2), y in data:
prediction = 1 if w[0]*x1 + w[1]*x2 + b > 0 else 0
error = y - prediction
w[0] += rate * error * x1
w[1] += rate * error * x2
b += rate * error
return w, b
def accuracy(data, w, b):
right = sum((1 if w[0]*x1 + w[1]*x2 + b > 0 else 0) == y for (x1, x2), y in data)
return right / len(data)
AND = [((0,0),0), ((0,1),0), ((1,0),0), ((1,1),1)]
XOR = [((0,0),0), ((0,1),1), ((1,0),1), ((1,1),0)]
print("AND accuracy:", accuracy(AND, *train(AND)))
print("XOR accuracy:", accuracy(XOR, *train(XOR, epochs=100)))Output
AND accuracy: 1.0 XOR accuracy: 0.5
Common mistakes
- Expecting it to learn any pattern. One perceptron only draws a straight line.
- Thinking XOR killed neural networks for good. It led to multi-layer networks, which solve it easily.
Check yourself
Summary
- One neuron with a threshold.
- It learns by correcting its mistakes.
- It can't solve XOR, which is why hidden layers exist.
Read it. Now try it yourself.
Open the interactive activityFinished reading?