A neural network is layers of simple sums: multiply each input by a weight, add them up, pass the result on. Between the layers sits one small extra step called the activation function. It looks like a detail, but without it, a network of any size can only ever draw a straight line. The activation is the bend that lets a network learn shapes. Here you’ll train a tiny network with and without one, then see why the choice of bend decides whether a deep network can learn at all.
Why sums alone go nowhere#
One neuron multiplies its inputs by its weights, adds them up with a constant, and passes the total through its activation function. Take the activation away and every layer does just “multiply, then add”.
The trouble is that “multiply, then add” twice is still just “multiply, then add”. Double a number and add 1, then triple the result and take away 2: that’s the same as multiplying by 6 and adding 1. However many layers you stack, they collapse into one. A straight line in, a straight line out.
Predict first
A network with 8 neurons and no activation function is trained on a wavy curve. What’s the most complicated shape it can draw?
With no activation, the best the network can do is the straight line through the middle, off by 0.37 or 0.38 on average, with 1 neuron or 8. Now pick ReLU, short for rectified linear unit. It’s about as simple as a bend can be: negative numbers become 0, positive numbers pass through unchanged. So each neuron’s piece is a hinge, flat on one side and sloped on the other, and the network’s output is a line with a corner wherever a hinge bends.
On the first random start, 1 neuron gives one corner and is off by 0.325. Two neurons, two corners: 0.115. Six: 0.019, close enough to look like the curve. Sigmoid gives each neuron a smooth S-shaped step instead of a corner, and the steps add up to a smooth curve. Each neuron adds one bend; with enough of them, a network can trace almost any curve. That’s a proven result, called the universal approximation theorem, though it doesn’t say how many neurons you’ll need, or whether training will find the right weights.
The common ones#
- ReLU: 0 for negatives, unchanged for positives. Cheap to compute and the default inside networks since the early 2010s. In the 2012 AlexNet paper, a network with ReLU reached the same training error six times faster than the same network with tanh.
- GELU: a smooth ReLU that lets a little through below zero. BERT used it “following OpenAI GPT”, and it’s common in transformers. Many recent language models, Llama among them, use SwiGLU, a gated relative.
- Sigmoid: squashes any number to between 0 and 1. The classic choice for decades, and still the right one at the output for a yes-or-no probability.
- Tanh: the same S shape, stretched to run from −1 to 1.
- Softmax: turns a whole row of scores into probabilities that add up to 1, at the output, for picking one of several options. It’s the step that turns a language model’s scores into next-word odds.
So why did sigmoid lose its job inside networks? The answer only shows up when the network gets deep.
Deep networks and the fading signal#
Training works backwards. The network makes a prediction, measures its error, and sends a correction back through the layers, telling each weight which way to move. That’s backpropagation. At every layer on the way back, the correction gets multiplied by the slope of the activation function at that neuron. Sigmoid’s slope is never more than 0.25, and far less when its input is large.
Predict first
In a 10-layer network with sigmoid activations, how strong is the learning signal that reaches the first layer, compared with the one reaching the last?
With sigmoid and 10 layers, the first layer gets a signal 4.4 million times weaker than the last; at 20 layers, 15 trillion times. Training would tune the last few layers and leave the early ones close to where they started. This is the vanishing gradient problem, and for years it was a big part of why deep networks were so hard to train.
Switch to tanh or ReLU and the bars stay level: the slope in the middle of tanh is 1, and ReLU’s is exactly 1 for every positive input. But switch the starting weights to “too small” and even ReLU fades, to 536 times weaker at 10 layers and 8.6 million at 20, because every layer now shrinks the signal a little as well. The fix is to scale the starting weights to the activation: He initialisation for ReLU, Xavier (Glorot) initialisation for tanh and sigmoid. The bend and the starting weights work together to keep the signal alive all the way down.
Choosing one#
Inside the network, use ReLU or GELU. In practice the architecture you pick usually decides for you. The choice that’s really yours is at the output, and it follows from what you’re predicting:
- a number, like a price or a temperature: no activation at all;
- yes or no, like spam or not: sigmoid, for a probability between 0 and 1;
- one of several, like which of 10 digits: softmax.
In code#
The same measurement as the figure, in PyTorch (pip install torch): 10 layers, 64 neurons wide, with starting weights scaled to each activation.
import torch
import torch.nn as nn
def signal_ratio(act, layers=10, width=64):
torch.manual_seed(0)
blocks = []
for _ in range(layers):
linear = nn.Linear(width, width)
if act is nn.ReLU:
nn.init.kaiming_normal_(linear.weight, nonlinearity="relu") # He
else:
nn.init.xavier_normal_(linear.weight) # Xavier (Glorot)
nn.init.zeros_(linear.bias)
blocks += [linear, act()]
net = nn.Sequential(*blocks, nn.Linear(width, 1))
net(torch.randn(256, width)).pow(2).mean().backward()
first = net[0].weight.grad.abs().mean()
last = net[2 * (layers - 1)].weight.grad.abs().mean()
return (first / last).item()
for act in (nn.Sigmoid, nn.Tanh, nn.ReLU):
print(f"{act.__name__:8} {signal_ratio(act):.1e}")
# Sigmoid 1.4e-07
# Tanh 3.4e-01
# ReLU 4.6e-01Sigmoid’s first layer gets about a ten-millionth of the last layer’s signal; tanh and ReLU get a third to a half. Swap in nn.GELU or nn.SiLU to try others.
What to remember
- Without an activation function, any number of layers collapses into one straight line; the activation is the bend.
- Each neuron adds one bent piece, and the pieces add up to the network’s curve. ReLU’s pieces are hinges.
- Training signals are multiplied by the activation’s slope at every layer: sigmoid starves deep networks, while ReLU with well-scaled starting weights keeps them learning.