LESSON 5.01 · 6 STAGES · 3 CONTROLS · 3 CHECKS
The perceptron
Watch one neuron draw a line and move it after each mistake.
EXPLAINER · 5.01 · PERCEPTRON
01
Stage 1 of 6 · Weighted sum
The perceptron multiplies each input by a weight and adds a bias. Point 3 gets z = −0.20.
Explain it simply
Each point has two inputs, its position x₁ and x₂. The perceptron multiplies each input by a weight. Then it adds the bias, one more number that shifts the result. For point 3, the total z is −0.20.
A neuron with inputs 0.6 and −0.4, weights −0.50 and 1.00, and bias 0.50. The sum z is −0.20.
inputs: dot y = 1, square y = 0weights w and bias bsum z, output ŷ and the lineerror e and wrong pointsthe line before
Toy: 12 points · 2 inputs · 3 parameters (w₁, w₂, b) · at most 10 passes
HOVER OR TAP A SHAPE TO SEE ITS NUMBERS
separable
Pick the points the perceptron learns.
1
How far each mistake moves the weights.
1 to 12
The order of the points in each pass.
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
Each input times its weight, plus the bias, gives the sum z.
Show the code ▾Hide the code ▴ · Try it yourself · Python 3
# The perceptron on the 12 points of the diagram (data: separable).
points = [ # (x1, x2, y): y = 1 is a dot, y = 0 a square
(1.0, 0.5, 1), # point 1
(0.4, 0.8, 1), # point 2
(0.6, -0.4, 1), # point 3
(1.3, 0.3, 1), # point 4
(0.2, 1.0, 1), # point 5
(-0.4, 0.9, 1), # point 6
(-1.3, -0.3, 0), # point 7
(-1.2, 0.2, 0), # point 8
(-1.1, 0.0, 0), # point 9
(-1.1, -0.6, 0), # point 10
(-0.1, 0.0, 0), # point 11
(-1.1, -0.2, 0), # point 12
]
eta = 1 # the learning rate
w1, w2, b = -0.5, 1.0, 0.5 # the poor first guess
def step(z):
return 1 if z >= 0 else 0
for p in range(1, 11):
order = range(1, 13) # points 1 to 12
mistakes = 0
for k in order:
x1, x2, y = points[k - 1]
z = w1 * x1 + w2 * x2 + b # the weighted sum
e = y - step(z) # the error: 1, 0 or -1
if e != 0:
w1 += eta * e * x1
w2 += eta * e * x2
b += eta * e
mistakes += 1
print(f"pass {p}: {mistakes} mistakes")
if mistakes == 0:
break
print(f"w1 = {w1:.2f}, w2 = {w2:.2f}, b = {b:.2f}")
# It prints:
# pass 1: 3 mistakes
# pass 2: 2 mistakes
# pass 3: 2 mistakes
# pass 4: 2 mistakes
# pass 5: 2 mistakes
# pass 6: 0 mistakes
# w1 = 2.30, w2 = 1.90, b = -0.50
Copy it into a file and run it.
Common mix-ups
MYTHEnough passes always find a line with no mistakes.
FIXOnly if one straight line can split the classes. On XOR data, every one of the 10 passes makes mistakes.
MYTHThe perceptron finds the best line.
FIXIt stops at the first line with no mistakes. That line passes only 0.04 from point 3. A shuffled order ends elsewhere.
Predict, then reveal
0 / 3 REVEALEDQ01With overlapping data, does training stop before pass 10?
1 · GUESS2 · TRY IT IN THE DIAGRAM
Q02On separable data with η = 0.1, does training need more passes or fewer?
1 · GUESS2 · TRY IT IN THE DIAGRAM
Q03On separable data, does a shuffled point order change the final line?
1 · GUESS2 · TRY IT IN THE DIAGRAM
Takeaways
- A perceptron outputs 1 when its weighted sum is at least 0.
- Each mistake adds η·e·x to the weights, so the line moves.
- It stops only if one straight line splits the classes.