If you know , you already know most of what a neuron does. This writeup builds the rest from there, one idea at a time, with figures you can poke at. By the end you'll know exactly why every neural network has a "non-linearity" in it, and what goes wrong without one.
Prefer to watch? There's a video version on YouTube.
Can you draw one line?
Here's a puzzle. Four points sit on the corners of a square. Two are filled: and . Two are rings: and .
Your job: draw one straight line that puts all the filled dots on one side and all the rings on the other.
Not separated· one side has 2 of 4 points
Go on, try every angle. It can't be done. Whichever way you tilt the line, at least one point ends up on the wrong side.
This pattern is called XOR ("exclusive or": the answer is 1 when exactly one input is 1). It's tiny, but it's famous, because it shows the limit of straight lines.
No single straight line can do this. Neither can a neural network without non-linearity. Let's see why.
What a neuron computes
A neuron takes some numbers in, multiplies each one by a weight, adds them up, and adds one more number called the bias:
A concrete example. With inputs , , , weights , , and bias :
The weights say how much each input matters. The bias shifts the result up or down. In short, this is : a weighted sum plus a bias.
With a single input, that's literally , the equation of a line. Play with it:
The weight tilts the line. The bias slides it up and down. But no matter what you do, it stays perfectly straight.
Stacking linear layers collapses into one
"Fine," you might say, "one neuron is a line. But a neural network has many layers. Surely stacking them gives you something more powerful?"
Let's test that with real numbers. Take two layers:
- Layer 1:
- Layer 2:
Now substitute the first into the second:
- The first layer: a weighted input plus a bias.
Two layers went in. One line came out: . You could have written that as a single layer from the start.
The same thing happens with layers of any width. With weight matrices and bias vectors :
Multiplying two matrices gives another matrix, so the whole thing is one layer with weights and bias . Try stacking more:
3 layers = still 1 line
Ten layers, still one line. Without something extra, depth is an illusion: a 100-layer linear network is exactly as powerful as a 1-layer one.
Solving XOR with ReLU
The "something extra" can be astonishingly simple. Meet ReLU (rectified linear unit):
That's it: if is positive, keep it; if it's negative, replace it with 0. So and .
Now a tiny network with two hidden neurons:
Run all four XOR points through it:
| Input | |||
|---|---|---|---|
| 0 | 0 | 0 | |
| 1 | 0 | 1 | |
| 1 | 0 | 1 | |
| 2 | 1 | 0 |
Exactly XOR. Look closely at the first row. For , the second neuron computes , and ReLU clips it to 0. That single clip is what stops the layers collapsing: without it, would be , and the whole network would simplify back into one line.
Here's what's happening geometrically. The hidden layer moves the points to new positions , and there, one straight line separates them:
| x₁, x₂ | h₁ = ReLU(x₁ + x₂) | h₂ = ReLU(x₁ + x₂ − 1) | y = h₁ − 2h₂ |
|---|---|---|---|
| (0, 0) | 0 | 0(−1 clipped to 0) | 0 |
| (1, 1) | 2 | 1 | 0 |
| (0, 1) | 1 | 0 | 1 |
| (1, 0) | 1 | 0 | 1 |
The network didn't learn to draw a curved boundary. It learned to rearrange the points so a straight boundary works.
ReLU as a hinge
To see what ReLU really adds, look at one neuron on its own: .
It's flat (zero) until , then it rises in a straight line with slope . Think of a hinge: a door that's closed up to a point, then swings open.
Two numbers control the hinge:
- says where the bend is.
- says how much the slope changes at the bend.
One hinge isn't much. But you can add hinges together, and that's where it gets interesting.
Adding hinges to build a curve
Let's build a curve that no straight line could ever match: , between 0 and 3.
The plan: approximate it with straight pieces, one piece per unit of . The slope of between 0 and 1 is about 1, between 1 and 2 about 3, and between 2 and 3 about 5. So we need a line whose slope is 1, then 3, then 5.
Each hinge adds to the slope from its bend onwards. So:
- A hinge at 0 with weight 1 gives slope 1.
- A hinge at 1 with weight 2 bumps the slope from 1 to 3.
- A hinge at 2 with weight 2 bumps it from 3 to 5.
Each output weight is the change in slope at that neuron's bend. The network:
How close does it get?
| Network | True | Error | |
|---|---|---|---|
| 0.5 | 0.5 | 0.25 | 0.25 |
| 1 | 1 | 1 | 0 |
| 1.5 | 2.5 | 2.25 | 0.25 |
| 2 | 4 | 4 | 0 |
| 2.5 | 6.5 | 6.25 | 0.25 |
| 3 | 9 | 9 | 0 |
Exact at the bends, and never more than 0.25 off in between. Run the network yourself; the table under the plot does the forward pass for whatever you pick:
| neuron | h = ReLU(x − bend) | × weight | = contribution |
|---|---|---|---|
| h1 | ReLU(1.50 − 0) = 1.50 | × 1 | 1.50 |
| h2 | ReLU(1.50 − 1) = 0.50 | × 2 | 1.00 |
| h3 | ReLU(1.50 − 2) = 0.00 | × 2 | 0.00 |
| sum (network output) | 2.500 | ||
| true x² | 2.250 | ||
| error | 0.250 | ||
Max error vs number of neurons
Each doubling of neurons halves the spacing between bends and divides the error by four.
Switch to 6 neurons, with bends at 0, 0.5, 1, 1.5, 2 and 2.5 and output weights 0.5, 1, 1, 1, 1, 1. The maximum error drops to about 0.0625. Halving the spacing between bends quarters the error, because each straight piece is shorter and hugs the curve more tightly.
Universal approximation
Nothing in that recipe was special to . To copy any smooth curve, put bends close together and set each weight to the change in slope there. With enough neurons you can get as close as you like to essentially any continuous curve. This is the idea behind the universal approximation theorem.
Curves that go down need the slope to decrease, so some weights become negative. Try and watch the count of negative weights, or draw your own curve:
6 neurons · 2 negative weights · max error 0.134
Notice the step function: no finite number of straight pieces can make a perfect vertical jump, but more neurons make the ramp steeper and steeper.
The geometric view: folding space
There's another way to picture all this, with more than one input. Think of the inputs as a sheet of graph paper.
A linear layer can stretch, squash, rotate and shift the sheet, but straight lines stay straight and parallel lines stay parallel. A ReLU takes every point with a negative coordinate and pushes it onto the axis: it folds the paper.
The input space: a square grid.
Stack a few of these stretch-and-fold steps and you can crumple the input space into almost any shape, which is how a deep network pulls apart classes that started out tangled, like our XOR points.
Choosing an activation function
ReLU isn't the only option. These are the common ones:
| Activation | Formula | Notes |
|---|---|---|
| ReLU | Simple, fast, the default in most networks | |
| Leaky ReLU | if , else | Keeps a small slope for negatives, so neurons can't "die" |
| GELU | A smooth ReLU, used in transformers like GPT and BERT | |
| Sigmoid | Squashes to (0, 1); common for probabilities at the output | |
| tanh | Squashes to (−1, 1) |
Any good activation needs three things:
- It must be non-linear. looks like a function, but it's linear, so stacked layers still collapse into one.
- It should be differentiable almost everywhere, because training follows the slope (the gradient) downhill. ReLU's single corner at 0 doesn't cause trouble in practice.
- It should keep gradients healthy. If the slope is nearly zero, learning stalls.
That third point is the vanishing gradient problem. Turn on the derivative and look at sigmoid and tanh far from the middle:
Their slopes flatten to almost nothing. Multiply many tiny slopes together across many layers, and the signal that tells early layers how to improve fades away. ReLU's slope is exactly 1 for every positive input, which is a big reason it took over.
Summary
- A neuron computes a weighted sum plus a bias: a straight line (or flat plane).
- Stacking linear layers always collapses into a single linear layer: .
- That's why no linear network, however deep, can solve XOR.
- A non-linearity like ReLU clips values, and that clipping blocks the collapse.
- Each ReLU neuron is a hinge; adding hinges builds any curve, with error shrinking as bends get closer.
- Linear layers stretch and rotate space; non-linearities bend and fold it.