The Perceptron
What is a perceptron?
A perceptron is the simplest possible neural network: a single artificial neuron. It's the foundational building block of deep learning.
A perceptron is a binary linear classifier. It takes several inputs, weighs how important each one is, and outputs a single yes/no decision: 0 or 1.
Why this matters
Every neural network, from a tiny classifier to a large language model, is built from units that work like a perceptron: weigh the inputs, add a bias, apply an activation. Understanding one neuron makes the rest of deep learning much easier to follow.
The parts of a perceptron
A perceptron has four components:
- Inputs (): The features, or numeric data, fed into the model.
- Weights (): One per input. They set how much each input influences the decision. Larger weights mean more influence.
- Bias (): An adjustable offset that shifts the decision boundary. It controls how easily the perceptron "fires," and lets it output 1 even when every input is 0.
- Activation function: A threshold function that turns the weighted sum into a 0 or 1. The classic choice is the step function (also called the Heaviside step function).
The math
The perceptron computes a weighted sum of its inputs and adds the bias:
Then the step function decides the output:
Example: an AND gate
Let's see a perceptron model a logical AND gate, which outputs 1 only when both inputs are 1.
The truth table
| Output | ||
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Choosing weights and bias
Set the weights to and , and the bias to . The weighted sum becomes:
Checking every case
-
(both false): . Since , the output is 0.
-
(only true): . Since , the output is 0.
-
(only true): . Since , the output is 0.
-
(both true): . Since , the output is 1.
The perceptron reproduces the AND gate perfectly. Geometrically, it draws a straight line that separates the point from the other three.
Because a perceptron can only draw a single straight line, it can't learn problems that aren't linearly separable. The classic example is XOR (output 1 when exactly one input is 1). Solving problems like that requires multiple layers of neurons.
Learning the weights
Above, we chose the weights by hand. A real perceptron learns them from examples using the perceptron learning rule: whenever it gets an example wrong, nudge each weight in the direction that would have made the answer more correct.
- is the learning rate, which controls the size of each nudge.
- is the error: the true label minus the prediction. It's 0 when the prediction is correct, so correct answers cause no change.
Implementing a perceptron in Python
Here's a perceptron with prediction and the learning rule, using NumPy:
import numpy as np
class Perceptron:
def __init__(self, input_dim, lr=0.1):
self.weights = np.zeros(input_dim)
self.bias = 0.0
self.lr = lr
def activation(self, z):
return 1 if z >= 0 else 0
def predict(self, x):
z = np.dot(x, self.weights) + self.bias
return self.activation(z)
def train(self, X, y, epochs=10):
for _ in range(epochs):
for xi, target in zip(X, y):
error = target - self.predict(xi)
# Perceptron learning rule: only update when wrong
if error != 0:
self.weights += self.lr * error * xi
self.bias += self.lr * error
# Train it on the AND gate
X_and = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y_and = np.array([0, 0, 0, 1])
perceptron = Perceptron(input_dim=2)
perceptron.train(X_and, y_and, epochs=10)
print('Learned weights:', perceptron.weights)
print('Learned bias:', perceptron.bias)
print('Predictions:', [perceptron.predict(x) for x in X_and])
The final line should print Predictions: [0, 0, 0, 1], matching the AND truth table. The learned weights won't match our hand-picked ones exactly; any line that separates the points works.
Summary
- A perceptron is a single artificial neuron: weighted sum, plus bias, through a step function.
- It's a binary linear classifier, so it can only separate classes with a straight line.
- It learns by nudging its weights whenever it makes a mistake.
- Stacking perceptrons into layers is what gives neural networks their power.