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

  1. Four examples, one for each pair of inputs that are either 0 or 1. x1s holds the first input of each.
  2. x2s holds the second input. Each example is a point on the chart: (0, 0), (0, 1), (1, 0) and (1, 1).
  3. 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.
  4. 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.
  5. The learning rate: every correction will move a weight by 0.25.
  6. Two rounds (epochs). In each round the neuron sees all four examples, one at a time.
  7. The first example: inputs 0 and 0, and the answer should be 0. The star on the chart marks the example being looked at.
  8. The weighted sum: 1 × 0 + 1 × 0 + (−0.5) = −0.5. With both inputs at 0, only the bias counts.
  9. −0.5 is not above 0, so the neuron outputs 0.
  10. The error is what we wanted minus what we got: 0 − 0 = 0. Right!
  11. Each weight changes by learning rate × error × its input. The error is 0, so nothing changes: a neuron only learns from its mistakes.
  12. Next example: inputs 0 and 1. AND says the answer should be 0.
  13. 1 × 0 + 1 × 1 + (−0.5) = 0.5.
  14. 0.5 is above 0, so the neuron fires: it outputs 1.
  15. Wanted 0, got 1: the error is 0 − 1 = −1. A mistake: this is the OR behaviour we need to get rid of.
  16. 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).
  17. Next: inputs 1 and 0. The answer should be 0.
  18. 1 × 1 + 0.75 × 0 + (−0.75) = 0.25.
  19. Still above 0, so the neuron says 1 again.
  20. Another mistake: error −1.
  21. 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.
  22. The last example: inputs 1 and 1. The answer should be 1.
  23. 0.75 × 1 + 0.75 × 1 + (−1) = 0.5.
  24. Above 0: the neuron says 1.
  25. Wanted 1, got 1: the error is 0. Right!
  26. Every change is multiplied by the error, and the error is 0, so the weights stay exactly as they are.
  27. All four examples seen: the first round is over.
  28. Two mistakes in this round, two corrections. The weights are now 0.75, 0.75 and −1.
  29. The second round: the same four examples with the new weights.
  30. Inputs 0 and 0, wanted 0.
  31. Only the bias: −1.
  32. Below 0: output 0.
  33. Right: error 0.
  34. Every change is multiplied by the error, and the error is 0, so the weights stay exactly as they are.
  35. Inputs 0 and 1, wanted 0. In round 1 this one was wrong.
  36. 0.75 × 1 + (−1) = −0.25. Below 0 now.
  37. Output 0: the correction worked.
  38. Right: error 0.
  39. Every change is multiplied by the error, and the error is 0, so the weights stay exactly as they are.
  40. Inputs 1 and 0, wanted 0. Also wrong in round 1.
  41. 0.75 × 1 + (−1) = −0.25.
  42. Output 0.
  43. Right: error 0.
  44. Every change is multiplied by the error, and the error is 0, so the weights stay exactly as they are.
  45. Inputs 1 and 1, wanted 1.
  46. 0.75 + 0.75 + (−1) = 0.5.
  47. Above 0: output 1.
  48. Right: error 0.
  49. Every change is multiplied by the error, and the error is 0, so the weights stay exactly as they are.
  50. The second round is over.
  51. Same weights as before the round: there was nothing to correct.
  52. 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.