LABS · 10  /  PERCEPTRON

The Line- Drawer.

The very first neural network, 1958, was one neuron with one job: draw a straight line between two kinds of points. It learns the only way it knows how — every time it puts a point on the wrong side, it nudges the line and tries again.

THE GIST · 20 SECONDS

A perceptron scores a point with w·x + b and calls it class +1 if positive, −1 if negative — a straight-line boundary. It learns by fixing mistakes: for each misclassified point, w += lr·label·x (and bias too), tilting the line toward that point's side. Repeat until zero mistakes. If the classes are separable, it's guaranteed to converge.

  • Weightsthe line's tilt (w)
  • Biasthe line's offset (b)
  • Updatenudge on a mistake
  • Separablea line can split them

Hit Step to fix the next misclassified point — or Play and watch the line swing until every point is on its own side.

UNDER THE HOOD

What you just played, written down

Learning by correcting mistakes, one at a time. It's the whole idea of a trainable neuron, stripped to its bones — and the limit that pointed the way to deep networks.

How the perceptron thinks — four moves

  1. Score the point. Compute w·x + b — a weighted sum of the features plus a bias.
  2. Predict a side. Positive score → class +1; negative → −1. That threshold is the boundary line.
  3. On a mistake, nudge. If wrong, push the weights toward the point's true class: w += lr·y·x, and b += lr·y.
  4. Repeat to zero mistakes. Sweep the data again and again; each pass fixes errors until none remain.
WHY THE NUDGE WORKS

Adding lr·y·x tilts the weight vector toward a misclassified positive point (or away from a negative one), moving the boundary so that point ends up on the correct side. Do it for every mistake and the line settles where all points agree.

The whole thing in code

def train(points, labels, lr=0.1, epochs=100):
    w = [0.0, 0.0]; b = 0.0
    for _ in range(epochs):
        errors = 0
        for x, y in zip(points, labels):
            score = w[0]*x[0] + w[1]*x[1] + b
            pred = 1 if score >= 0 else -1
            if pred != y:                 # a mistake
                w[0] += lr * y * x[0]      # nudge the weights
                w[1] += lr * y * x[1]
                b   += lr * y
                errors += 1
        if errors == 0:                   # converged
            break
    return w, b
BOUNDARYw·x+b=0a straight line
CONVERGESif separableguaranteed, finite steps
THE XOR WALL

A single perceptron can only draw one straight line, so it can't solve XOR — no line separates its classes. That 1969 realization stalled neural nets, until stacking perceptrons into layers brought non-linear boundaries.

⚠ Only linear boundaries

One perceptron draws a single hyperplane. If the classes aren't linearly separable, it never converges — it just cycles. The cure is depth: a multi-layer network.

↔ Its descendants

Add a smooth activation and gradient descent and you get logistic regression; stack many and train with backprop and you get a deep network. The perceptron is the seed.

★ Why it matters

It's the single neuron every modern network is built from — weights, a bias, a boundary, and mistake-driven learning. Understand one and the rest is repetition.

SOLVE IT YOURSELF

Solve it: one perceptron update

This one is yours to write — in Python or TypeScript, running for real in your browser. Given the weights, bias, a point, and its label, apply one perceptron update if it's misclassified. Edit the stub, hit Run (or ⌘/Ctrl + Enter), and watch the hidden tests. Stuck? the hints are below and Reveal solution is one click away.

YOUR TASK

Implement update(w, b, x, y, lr): compute the prediction (sign of w·x + b, +1 if ≥ 0 else −1). If it matches y, return (w, b) unchanged. Otherwise nudge: w += lr·y·x component-wise and b += lr·y, then return the new (w, b).

HINTS — 4 IDEAS
  1. Score = w[0]*x[0] + w[1]*x[1] + b; prediction is 1 if score >= 0 else -1.
  2. If prediction equals y, it's correct — return w, b as-is.
  3. Otherwise add lr*y*x[k] to each weight and lr*y to the bias.
  4. Return the updated weights and bias.
CPython · WebAssembly
QUICK CHECK

Did it stick?

FAQ

Perceptron, answered

What is a perceptron?

The simplest neural network — a single neuron that scores an input with w·x + b and predicts one of two classes by the sign. Geometrically it draws a straight-line boundary between the classes.

What is the learning rule?

On each misclassified point it nudges the weights toward the correct class: w += lr·y·x and b += lr·y, repeating until no point is misclassified.

Does it always converge?

Only if the data is linearly separable — then the convergence theorem guarantees a solution in finite steps. Otherwise it cycles forever.

What is the XOR problem?

A dataset a single perceptron can't solve because no straight line separates its classes. It exposed the perceptron's limit; stacking neurons into layers fixes it.

How does it relate to modern nets?

It's the single neuron deep networks are built from. Add non-linear activations and backprop across many layers and you get today's deep learning.

RESULT

Next → one perceptron draws one line; stack them into layers with non-linear activations and train with backprop, and you can carve any boundary — that's a deep neural network.

Finished this one? 0 / 12 Labs done

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Labs