TL;DR
An activation function is a fixed, usually simple, bend applied to a neuron's weighted sum before the result is passed on. It is what lets a network draw curved boundaries and fit curved functions. Without it, every layer is just a matrix multiply, and a hundred layers do no more than one.
The common choices are the sigmoid, the hyperbolic tangent, and the rectified linear unit (ReLU). Each one bends the line in a different place and by a different amount.
Where you'll see it: after almost every hidden layer of a feedforward or convolutional network, in the gate of an LSTM, and in the squashing step that turns a network's last layer into probabilities.
Intuition
A straight line cannot fold
Picture a neuron as a small decision maker. It takes some inputs, multiplies each by a weight, adds them up with a bias, and gets one number. That number is a straight-line (linear) function of the inputs. Stack two of those neurons and the output is still a straight-line function of the original inputs, because a line fed into a line is a line.
The problem is that most real patterns are not straight. Whether a loan is risky, whether a pixel belongs to an edge, whether a sound is a vowel: none of these change in a steady proportion to their inputs. They have thresholds, saturation and folds. An activation function inserts the bend. After it, the next layer sees a shape that a single linear map could not have produced, and stacking layers starts to add real power.
Think of it as a dimmer switch versus a plain wire. A wire passes the signal unchanged. A dimmer responds differently at low and high settings. The activation function is the dimmer that each neuron places after its sum.
What a good bend needs
Three properties matter in practice:
- Nonlinear, so that depth adds expressive power.
- Easy to compute, because it runs billions of times per training step.
- Has a useful derivative, because training (see Backpropagation) pushes error signals backwards through it using its Derivative, and a derivative that is always near zero stops learning.
The rest of this lesson is about the tension between these, especially the third.
The math
The neuron
A neuron with inputs , weights and bias computes first a pre-activation and then an output:
Here is the weighted sum, is the output, and is the activation function. A layer applies the same elementwise to a vector of sums ( is a matrix whose rows are the weight vectors of the layer's neurons): . The symbol is reused here for the activation; it is not the logistic function unless we say so.
Why linearity collapses
Take two layers without any activation between them:
Substitute the first into the second:
The result is one matrix and one bias. This holds for any number of layers, so depth buys nothing. Inserting a nonlinear between the layers breaks the substitution, and the composition cannot in general be written as a single affine map.
The three standard choices
Sigmoid squashes any real number into the open interval :
Its largest derivative is , and the derivative shrinks towards zero as grows.
Hyperbolic tangent is a rescaled sigmoid that maps into :
It is zero-centred, which helps optimisation a little, and its largest derivative is at .
Rectified linear unit (ReLU) keeps positive values and clips negative ones to zero:
At exactly the derivative is undefined. Implementations pick 0 or 1, and this does not matter in practice.
- sigmoid
- tanh
- ReLU
Derivatives and the vanishing gradient
Backpropagation multiplies local derivatives along a path. For a chain of sigmoid layers, the gradient reaching the first layer contains a product of factors of the form . Since everywhere, that product is at most . Tanh is at most but is only at zero, so saturated tanh units also send almost no gradient back. ReLU's local derivative is exactly on its active side, so the product does not shrink from that factor alone. This is the core reason ReLU replaced sigmoid in deep hidden layers.
Worked examples
Inputs , weights , bias .
- Weighted sum: .
- ReLU: .
- Sigmoid: .
- Tanh: .
- Same sum, three outputs. Because is small, sigmoid and tanh are both close to their centre point and nearly linear here. ReLU is exactly linear for but has a kink at , right next to this input, and that kink is what lets it carve sharp regions.
Ten sigmoid layers, and suppose each neuron happens to sit at its best point, , where .
- Gradient factor per layer: .
- After ten layers the product is .
- So the first layer receives a learning signal about a millionth the size of the last one.
- (This ignores the weights, which also scale the signal at each layer; the point is the activation's contribution.) Now suppose a neuron drifts to . Then and . One saturated neuron cuts the signal by a factor of about 100 on its own.
- The same ten layers of ReLUs with positive inputs multiply to . This is the arithmetic behind the switch to ReLU.
A hidden layer has 3 neurons with pre-activations , and we use ReLU.
- Outputs: .
- The first neuron is switched off for this input, so it contributes nothing to the next layer and receives no gradient from this example.
- The other two pass their values through unchanged.
- Interpretation: each neuron acts as a gate that is either open (a linear function of its input) or closed (zero). A network of many such gates is piecewise linear, and it is the pattern of open and closed gates that makes the overall function curved.
Tradeoffs and failure modes
No activation is best everywhere. The choice depends on where the unit sits and what the training signal needs.
| Situation | Problem | Better choice |
|---|---|---|
| Deep hidden layers | Sigmoid and tanh saturate, so gradients vanish | ReLU or a variant |
| Neurons stuck at zero | "Dying ReLU": a unit with negative pre-activation for every input gets no gradient and never recovers | Leaky ReLU, , or GELU |
| Output of a binary classifier | Needs a probability in | Sigmoid on the final layer |
| Output over several classes | Needs probabilities that sum to 1 | Softmax (see Softmax) |
| Recurrent gates (LSTM, GRU) | Need a value between 0 and 1 to act as a gate | Sigmoid for the gate, tanh for the content |
| Transformers and modern large models | Smooth curvature helps optimisation | GELU or SiLU |
Practical warnings:
- Sigmoid in a hidden layer is rarely the right default. It is fine for gates and outputs, but it makes deep training slow.
- Initialisation matters. Weight scales must keep pre-activations in the range where the chosen function has a useful slope. This is why initialisation schemes are tied to the activation.
- Watch for dead units. Count how many ReLU outputs are zero across a batch. If a large fraction are always zero, the learning rate or initialisation is usually to blame.
History
The first mathematical neuron, proposed by Warren McCulloch and Walter Pitts in 1943, used a hard threshold: it fired (output 1) or did not (output 0). That is a step activation, which is nonlinear but has zero derivative almost everywhere, so it cannot be trained by gradient descent. Frank Rosenblatt's perceptron in the late 1950s kept the step and learned only one layer.
Smooth activations came with training by backpropagation. The sigmoid was the usual choice through the 1980s and 1990s, partly because its smooth derivative made the method work and partly because it was already used as a model of firing rates. David Rumelhart, Geoffrey Hinton and Ronald Williams popularised backpropagation with sigmoid units in 1986.
ReLU was used in earlier work, but it became standard around 2010 to 2012. Vinod Nair and Geoffrey Hinton showed its advantages in 2010, and the deep convolutional network that won the ImageNet competition in 2012 used it heavily. Its speed and its non-vanishing gradient on the active side made very deep networks trainable in practice. Variants such as leaky ReLU, and smoother ones such as GELU, followed.
Check yourself
QuestionWhy does a network with no activation functions, however many layers it has, behave like a single layer?Show answer
Composing affine maps gives an affine map. Two layers simplify to , which is one matrix and one bias. Repeating this for any number of layers gives the same form, so the extra depth adds no expressive power.
QuestionA neuron has . What are its ReLU output and its ReLU derivative, and what does a zero derivative mean for training on this example?Show answer
The output is . The derivative is 0 because is negative. No error signal flows back through this neuron for this input, so its incoming weights do not change from this example.
QuestionCompute and for the sigmoid. Why is the largest value the derivative ever takes?Show answer
, and . The derivative is largest when is closest to , and that happens at . Moving away from zero pushes towards 0 or 1, which makes the product smaller.
QuestionYou train a 20-layer network with sigmoid hidden units, and the early layers barely change. You inspect the activations and most sit near 0 or 1. What is happening, and what two changes would you try?Show answer
The units are saturated, so their local derivatives are close to zero, and the product of these derivatives across 20 layers is tiny. This is the vanishing gradient problem. Two reasonable changes are to switch hidden units to ReLU (or a variant such as leaky ReLU or GELU), and to use an initialisation scheme that keeps pre-activations in the non-saturated range.
Sources
- Goodfellow, Bengio, Courville, Deep Learning, §6.3 (Hidden Units): https://www.deeplearningbook.org/
- Activation function (Wikipedia): https://en.wikipedia.org/wiki/Activation_function
- Rectifier (neural networks) (Wikipedia): https://en.wikipedia.org/wiki/Rectifier_(neural_networks)
- Sigmoid function (Wikipedia): https://en.wikipedia.org/wiki/Sigmoid_function