Activation functions
Last updated
Between every two layers of a network sits an activation function, applied to each neuron's weighted sum. Lesson 5.6 showed that without one, any number of layers collapses into a single straight line. This lesson meets the five activations you'll see most, works out their derivatives, which backpropagation will need, and finds the flat regions where a neuron stops learning.
By the end of this lesson you can
- Explain why a network needs a nonlinear activation between its layers
- Write sigmoid, tanh, ReLU, leaky ReLU and GELU, and the derivatives of the first four, in NumPy
- Say what saturation is, and why it slows learning
- Choose a sensible activation for a hidden layer
Without one, layers collapse into one
Lesson 5.6 took the steps out of the XOR network and found it had become one linear layer. The same happens to any network without activations. Two layers compute , and because matrix products can be regrouped (lesson 1.4),
which is one layer, with weight matrix :
The same numbers, from a single matrix. A hundred linear layers would still be one, and one layer draws only straight boundaries. So between layers, each neuron applies a nonlinear function to its weighted sum: . That's the activation function. It's what lets the next layer see the data bent into a new shape, as the XOR fold did.
Five activations
Almost any nonlinear function would do in principle. In practice, these five cover nearly every network you'll meet:
- Sigmoid, : squeezes to (lesson 3.9). Still used for outputs that are probabilities, and inside LSTM gates (Stage 9).
- tanh, : squeezes to , centred on 0. Common in older networks and in recurrent ones.
- ReLU, : the rectified linear unit. The default for hidden layers since about 2011; simple and quick, and it doesn't flatten out for positive .
- Leaky ReLU, for and otherwise: ReLU with a small slope on the left.
- GELU, , where is the standard normal distribution's cumulative probability (lesson 1.10): a smooth ReLU, used in the transformers of Stage 10.
Pick each one and slide along:
Output between 0 and 1. Its slope is never more than 0.25, and flat on both sides. At z = 1: f(z) = 0.731, f′(z) = 0.197.
Their derivatives
Training needs the slope of every activation, because backpropagation (lesson 6.4) multiplies by at every neuron. Lesson 1.7 found the sigmoid's; the others are as short:
The last one uses the indicator : 1 when the statement in the brackets is true, 0 when it's false.
In words: the sigmoid's slope is its output times one minus its output; tanh's is one minus its output squared; ReLU's is 1 where it's on and 0 where it's off. Both the sigmoid's and tanh's can be worked out from the output alone, which saves recomputing anything. ReLU has a corner at exactly , where it has no derivative; code picks 0 there, and it hardly ever matters.
Check each formula by nudging (lesson 1.6), as you'll check every gradient from now on:
The formulas and the nudges agree to about , which is rounding.
Saturation
Look at the sigmoid's slope as grows:
At the sigmoid's slope is 0.25, its largest. At it's 0.0066, and at , 0.000045. tanh flattens even faster. A neuron whose weighted sum lands far out on a flat part is saturated: changing its weights a little hardly changes its output, so the gradient that reaches its weights is tiny, and it barely learns. In the gallery, those regions are shaded.
ReLU never saturates for positive : its slope there is always exactly 1. For negative , its slope is 0, and a unit whose sum is negative for every example gets no gradient at all and stops learning for good: a dead ReLU. Leaky ReLU's 0.01 slope is there to stop that.
Wrong way: sigmoid in every layer of a deep network
Backpropagation multiplies one slope per layer (lesson 1.7's chain rule, lesson 6.4 in full). With sigmoids, every one of those slopes is at most 0.25. Put 8 layers in a row and see what reaches the bottom:
Each sigmoid slope at z = 0.5 is 0.235, so after 8 layers the gradient reaching layer 1 is 0.2358 = 9.3e−6. The first layers hear almost nothing: the gradient has vanished.
Now train it. Two networks of 8 hidden layers of 32 units on scikit-learn's handwritten digits, one with sigmoids (scikit-learn calls it "logistic") and one with ReLU, the same everything else, for 21 passes over the data:
The sigmoid network gets 10.1% right: chance, for ten digits. Its first layer's weights moved by 0.0000066 on average, about 600 times less than its last layer's: the gradient vanished on the way down. The ReLU network's first layer moved about 12,000 times more, and it's learning, if slowly (32.3%; Stage 7 makes it fast).
This is the vanishing gradient problem, and it held deep networks back for years. Lesson 7.5 measures it layer by layer and fixes it; for now, the rule is: ReLU (or a relative) in hidden layers, sigmoid only where an output must be a probability.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Goodfellow, Bengio and Courville, Deep Learning, section 6.3: Hidden Units: ReLU, its variants, sigmoid and tanh, and when to use each
- Hendrycks and Gimpel, Gaussian Error Linear Units (GELUs), 2016: GELU, z times the normal distribution’s Φ(z)
- Prince, Understanding Deep Learning, section 3.1.1: Rectified linear unit: ReLU and the shallow networks it builds
- scikit-learn: MLPClassifier: activation='logistic', 'tanh' or 'relu', and partial_fit