1. Home
  2. AI & Machine Learning
  3. Perceptron

Perceptron

The simplest neural network: a single neuron that learns a straight line to separate two classes by fixing its mistakes one at a time.

Interactive 3DBeginner12 min readAI/MLUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · Left arrowRight arrow step

What's happening

The perceptron is the simplest neural network: one neuron that learns a straight line to split two classes. Press Train.

Pseudocode

  1. w ← small starting weights, b ← 0
  2. repeat for each epoch:
  3. for each training point (x, y): // y = +1 or −1
  4. ŷ ← +1 if w·x + b ≥ 0 else −1
  5. if ŷ ≠ y: // mistake
  6. w ← w + η·y·x
  7. b ← b + η·y
  8. until an epoch has no mistakes
Read this model as text · 7 steps

One run of the model above, written out step by step. Useful with a screen reader, on a slow connection, or for quick revision. The values may differ from the run on your screen.

  1. The perceptron is the simplest neural network: one neuron that learns a straight line to split two classes. Press Train.
  2. A perceptron draws a line w·x + b = 0 and predicts +1 on one side, −1 on the other. The starting line is a guess and gets 4 of 14 points wrong.
  3. Epoch 1: this point is −1 but w·x + b = 0.39 predicts +1. A mistake, so the line must move.
  4. Update: w ← w + η·y·x and b ← b + η·y. The line is pulled towards the point, now 1 mistakes in total.
  5. Epoch 1: this point is −1 but w·x + b = 0.79 predicts +1. A mistake, so the line must move.
  6. Update: w ← w + η·y·x and b ← b + η·y. The line is pulled towards the point, now 0 mistakes in total.
  7. A full pass with no mistakes: the line separates the two classes after 2 updates. For separable data the perceptron convergence theorem guarantees this always happens.

Try this in the 3D model

  • Press Train and count how many updates it takes. Does the line stop moving once all points are right?
  • Change the learning rate to 0.1 and train on the same data. Does it still converge, and how many updates?
  • Press New data a few times. Does training always finish? Why?
  • Watch the white arrow w. Which way does it point relative to the +1 points?

One neuron, one line

A perceptron takes some numbers (features), multiplies each by a weight, adds a bias and checks the sign:

ŷ = +1  if  w₁x₁ + w₂x₂ + b ≥ 0
    −1  otherwise

Geometrically, w·x + b = 0 is a straight decision line. Points on one side are class +1 and the rest class −1. The weight vector w is perpendicular to the line and points towards the +1 side.

Learning from mistakes

Start with arbitrary weights and sweep through the training points. Whenever a point is classified wrongly, nudge the line towards it:

for each point (x, y):
    if sign(w·x + b) ≠ y:
        w ← w + η · y · x
        b ← b + η · y

η is the learning rate. For a +1 point that was predicted −1 the update adds the point to w, which raises its score; a −1 point is subtracted. Repeat for several epochs until a full pass makes no mistakes.

Does it always work?

If the classes can be separated by a straight line (they are linearly separable), the perceptron convergence theorem says the algorithm stops after a finite number of updates. If they cannot be separated, it keeps wandering forever. The classic failure is XOR: no single line separates (0,0), (1,1) from (0,1), (1,0). That limitation motivated multi-layer networks (see neural networks).

Model Output Learns by
Perceptron Hard class (+1 or −1) fixing mistakes
Logistic regression Probability gradient descent on log loss
SVM Hard class with the widest margin maximising the margin

Code

def train_perceptron(points, labels, lr=1.0, epochs=30):
    w, b = [0.0, 0.0], 0.0
    for _ in range(epochs):
        mistakes = 0
        for (x1, x2), y in zip(points, labels):
            pred = 1 if w[0] * x1 + w[1] * x2 + b >= 0 else -1
            if pred != y:
                w[0] += lr * y * x1
                w[1] += lr * y * x2
                b += lr * y
                mistakes += 1
        if mistakes == 0:
            break
    return w, b

Common mistakes

  • Updating on every point instead of only on mistakes.
  • Forgetting the bias b. Without it the line must pass through the origin.
  • Expecting convergence on data that is not linearly separable.
  • Using labels 0/1 with the formula w + η·y·x. This version needs labels +1/−1.

Complexity at a glance

Case / operationTimeWhy
One predictionO(d)A dot product over d features.
One epochO(n · d)n points, d features each.
Mistakes before convergence≤ (R/γ)²Novikoff's bound for separable data with margin γ and radius R.
Extra spaceO(d) weights

Quick check

Test yourself — pick an answer to see if you got it.

1. What does a perceptron compute?

2. When are the weights updated?

3. What is the update for a misclassified point (x, y)?

4. Which dataset can a single perceptron NOT learn?

Saved in this browser.
Spotted a mistake or a bug in the 3D model?

Report a mistake

in Perceptron. Thank you — every report makes the lesson better for the next reader.

We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.