Machine Learning from Scratch · Neural Networks
One Neuron Learns: The Perceptron
Watch a single artificial neuron learn the AND rule from four examples: weighted sum, fire or not, measure the mistake, nudge the weights.
neuron.py
# Teach one neuron the AND rule: output 1 only when both inputs are 1.
x1s = [0, 0, 1, 1]
x2s = [0, 1, 0, 1]
ys = [0, 0, 0, 1]
w1, w2, b = 1.0, 1.0, -0.5 # these starting weights make it do OR, not AND
lr = 0.25
for epoch in range(2):
for x1, x2, y in zip(x1s, x2s, ys):
z = w1 * x1 + w2 * x2 + b
out = 1 if z > 0 else 0
error = y - out
w1, w2, b = w1 + lr * error * x1, w2 + lr * error * x2, b + lr * error
print("epoch", epoch, "weights:", w1, w2, b)
Step by step
- Four examples, one for each pair of inputs that are either 0 or 1.
x1s holds the first input of each. x2s holds the second input. Each example is a point on the chart: (0, 0), (0, 1), (1, 0) and (1, 1).- The answers we want. AND gives 1 only when both inputs are 1, so only (1, 1) wants a 1. On the chart, points that want 0 are red and the one that wants 1 is amber.
- The neuron's three knobs: a weight for each input and a bias. In the neuron picture, both connections are blue and thick (weight 1). The shaded area on the chart is where this neuron says 1: it covers three of the four points, so right now it computes OR, not AND.
- The learning rate: every correction will move a weight by 0.25.
- Two rounds (epochs). In each round the neuron sees all four examples, one at a time.
- The first example: inputs 0 and 0, and the answer should be 0. The star on the chart marks the example being looked at.
- The weighted sum: 1 × 0 + 1 × 0 + (−0.5) = −0.5. With both inputs at 0, only the bias counts.
- −0.5 is not above 0, so the neuron outputs 0.
- The error is what we wanted minus what we got: 0 − 0 = 0. Right!
- Each weight changes by learning rate × error × its input. The error is 0, so nothing changes: a neuron only learns from its mistakes.
- Next example: inputs 0 and 1. AND says the answer should be 0.
- 1 × 0 + 1 × 1 + (−0.5) = 0.5.
- 0.5 is above 0, so the neuron fires: it outputs 1.
- Wanted 0, got 1: the error is 0 − 1 = −1. A mistake: this is the OR behaviour we need to get rid of.
- The correction:
w2 drops from 1 to 0.75 and b from −0.5 to −0.75. w1 stays at 1, because its input was 0 and played no part in the mistake. Watch the boundary on the chart move away from (0, 1). - Next: inputs 1 and 0. The answer should be 0.
- 1 × 1 + 0.75 × 0 + (−0.75) = 0.25.
- Still above 0, so the neuron says 1 again.
- Another mistake: error −1.
- This time
w1 drops to 0.75 (its input was 1) and b to −1. w2 stays, since its input was 0. Now (1, 0) is outside the shaded area too. - The last example: inputs 1 and 1. The answer should be 1.
- 0.75 × 1 + 0.75 × 1 + (−1) = 0.5.
- Above 0: the neuron says 1.
- Wanted 1, got 1: the error is 0. Right!
- Every change is multiplied by the error, and the error is 0, so the weights stay exactly as they are.
- All four examples seen: the first round is over.
- Two mistakes in this round, two corrections. The weights are now 0.75, 0.75 and −1.
- The second round: the same four examples with the new weights.
- Inputs 0 and 0, wanted 0.
- Only the bias: −1.
- Below 0: output 0.
- Right: error 0.
- Every change is multiplied by the error, and the error is 0, so the weights stay exactly as they are.
- Inputs 0 and 1, wanted 0. In round 1 this one was wrong.
- 0.75 × 1 + (−1) = −0.25. Below 0 now.
- Output 0: the correction worked.
- Right: error 0.
- Every change is multiplied by the error, and the error is 0, so the weights stay exactly as they are.
- Inputs 1 and 0, wanted 0. Also wrong in round 1.
- 0.75 × 1 + (−1) = −0.25.
- Output 0.
- Right: error 0.
- Every change is multiplied by the error, and the error is 0, so the weights stay exactly as they are.
- Inputs 1 and 1, wanted 1.
- 0.75 + 0.75 + (−1) = 0.5.
- Above 0: output 1.
- Right: error 0.
- Every change is multiplied by the error, and the error is 0, so the weights stay exactly as they are.
- The second round is over.
- Same weights as before the round: there was nothing to correct.
- Training ends. The neuron has learned AND: the shaded area now holds only (1, 1), and the dashed boundary separates it from the other three points. Every neural network is built from units like this one.