“Neural network” makes it sound like a brain. It’s much humbler than that: a pile of small calculations, each one a weighted sum, with dials that get turned a little at a time until the answers come out right. Every network, from a spam filter to ChatGPT, is built from the same small piece. Here you’ll build one up, piece by piece, and train it in your browser.
One neuron draws one line#
A neuron takes some numbers in, multiplies each by a weight, adds them up, adds one more number called the bias, and squashes the total into a value between 0 and 1. With two inputs, x and y, that’s:
output = σ(w₁·x + w₂·y + b)
The σ (sigmoid) just squashes: big totals come out near 1, very negative ones near 0. Read the output as “how sure am I this is a green point?”. Below are 60 points, green and blue. The neuron starts with bad weights. Fix them by hand.
Wherever the weighted sum is exactly zero, the output is exactly 0.5. That’s the dashed line, and it’s always a straight line. A single neuron can only ever split the plane in two with one straight cut. The weights turn the line, the bias slides it. “Let it learn” uses gradient descent to find good dials for you. In a moment it gets 59 of the 60 points right; the last one sits right at the edge of the line, and tipping it over would take many more small steps.
Where one line isn’t enough#
Now a pattern called XOR: green where x and y have the same sign, blue where they differ. Top right and bottom left are green; the other two corners are blue.
Predict first
Can a single neuron, one straight line, ever get XOR right?
With no hidden neurons, it stalls at 59%. Now set Hidden neurons to 4 and train again. In a few dozen steps it gets every point right.
Hidden layers: many lines, combined#
A hidden layer is a row of neurons between the input and the output. Each hidden neuron draws its own line. The output neuron then combines their answers, so the network can carve out corners, bands and curves that no single line could. Try the circle: no line can separate the inside from the outside, but four hidden neurons can fence it in.
There’s a catch, though, and it’s the reason every network needs an activation function, the little bend each hidden neuron applies to its sum.
Predict first
Eight hidden neurons, but the activation set to “None”, so each hidden neuron passes its sum straight on. Can it learn XOR?
Which activation?#
Any bend works in principle, but a few are used almost everywhere. Pick one to highlight it:
The usual rule: ReLU or GELU inside the network, and an output that matches the question. That means a sigmoid for yes or no, a softmax (sigmoid’s big sibling, which shares 100% among many choices) for picking one of several, and nothing at all when the answer is a plain number like a price.
How it learns: backpropagation#
Training works the way our gradient descent explainer shows. Measure how wrong the network is with a loss (here, cross-entropy: it’s large when the network is confidently wrong), work out which way each weight should move to lower it, and nudge every weight a little. Repeat.
The clever part is working out the direction for every weight at once. Backpropagation does it by starting from the error at the output and passing blame backwards through the network, layer by layer, using the chain rule from calculus. Each weight learns how much it contributed to the mistake. That’s all “learning” means here: millions of tiny nudges, each in the direction that makes the error smaller.
One honest wrinkle: training can get stuck in a poor arrangement. With 4 hidden neurons on XOR, about 2 random starts in 5 stall short of 100%; press reset a few times and you’ll meet one. With 8, it almost always gets there. Bigger networks get stuck far less often, one reason real networks are so large.
In code#
The whole idea, from scratch, in NumPy: a network with 8 hidden neurons learning XOR.
import numpy as np
rng = np.random.default_rng(0)
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=float)
y = np.array([[0], [1], [1], [0]], dtype=float) # XOR
W1, b1 = rng.normal(0, 1, (2, 8)), np.zeros(8) # 2 inputs -> 8 hidden
W2, b2 = rng.normal(0, 1, (8, 1)), np.zeros(1) # 8 hidden -> 1 output
sigmoid = lambda z: 1 / (1 + np.exp(-z))
for step in range(5000):
h = np.tanh(X @ W1 + b1) # forward: hidden layer
p = sigmoid(h @ W2 + b2) # forward: output
d2 = (p - y) / len(X) # backward: output error
d1 = (d2 @ W2.T) * (1 - h ** 2) # backward: hidden error (chain rule)
W2 -= 1.0 * h.T @ d2; b2 -= 1.0 * d2.sum(0)
W1 -= 1.0 * X.T @ d1; b1 -= 1.0 * d1.sum(0)
print(np.round(p.ravel(), 2)) # [0. 1. 1. 0.]PyTorch and other libraries do the backward lines for you with loss.backward(), for networks with billions of weights. The idea doesn’t change.
What to remember
- A neuron is a weighted sum plus a bias, squashed. On its own it can only draw one straight line.
- Hidden layers combine many lines into curves and corners, but only with an activation function between them; without one, every layer collapses into one.
- Training is gradient descent: backpropagation works out how each weight contributed to the error, and every weight gets nudged to reduce it.