← All posts

The smallest brain you can build: a perceptron in Python

One input, weight and bias, training epochs, and a linear decision boundary — neural network fundamentals without frameworks.

Contents

In brief

A perceptron is the simplest ML model: one input, a yes/no output, formula output = 1 when (w · x + b) > 0. The ranpara.net article explains the mechanism from scratch in Python — no TensorFlow or PyTorch.

What happened

The perceptron multiplies input x by weight w, adds bias b, and compares to zero. The result is binary — the foundation for larger networks.

Training adjusts w and b based on prediction errors. An epoch is one full pass over training data; over several epochs the decision boundary shifts toward separable classes.

Normalization matters: features on different scales let one dominate and break learning. Scaling inputs to comparable ranges balances their contribution.

The fundamental limit: one perceptron draws only a linear decision boundary. Problems like XOR are impossible. Stacking perceptrons into layers builds networks that approximate nonlinear functions.

Why it matters

Understanding the perceptron bridges "neural network magic" and math. Weights, bias, threshold, gradient descent in complex models — same logic, scaled up. For developers consuming LLM APIs, basic ML helps judge where models are reliable.

A from-scratch Python implementation is dozens of lines: epoch loop, weight updates on error, optional boundary visualization.

In practice

  1. Start with 2D data — easier to plot the separating line.
  2. Normalize inputs before training.
  3. Log error per epoch — if it never drops, data may be linearly inseparable.
  4. For XOR-like tasks, add at least one hidden layer (MLP).
  5. Compare with sklearn.linear_model.Perceptron to verify.
  6. Use as a stepping stone before backpropagation.

Takeaway

The perceptron is not a toy — it is the building block of modern networks. The article shows how simple rules produce learning behavior. Code and explanations are in the original.