Neural networks · lesson 2 of 10
Parts in this lesson
Anp.maximum(z, 0)ReLU, the default for hidden layers
Bnp.where(z > 0, z, 0.01 * z)leaky ReLU
C(z > 0).astype(float)ReLU's derivative: 1 or 0
Dsigmoid(z) * (1 - sigmoid(z))1.7: the sigmoid's derivative

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 (XW[1]⊤)W[2]⊤, and because matrix products can be regrouped (lesson 1.4),

(XW[1]⊤)W[2]⊤=X(W[2]W[1])⊤

which is one layer, with weight matrix W[2]W[1]:

activations.ipynb

The same numbers, from a single (2,4) 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 g to its weighted sum: a=g(z). 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, σ(z)=1/(1+e−z): squeezes to (0,1) (lesson 3.9). Still used for outputs that are probabilities, and inside LSTM gates (Stage 9).
  • tanh, tanh⁡z: squeezes to (−1,1), centred on 0. Common in older networks and in recurrent ones.
  • ReLU, max⁡(z,0): the rectified linear unit. The default for hidden layers since about 2011; simple and quick, and it doesn't flatten out for positive z.
  • Leaky ReLU, z for z>0 and 0.01z otherwise: ReLU with a small slope on the left.
  • GELU, zΦ(z), where Φ is the standard normal distribution's cumulative probability (lesson 1.10): a smooth ReLU, used in the transformers of Stage 10.
activations.ipynb

Pick each one and slide z along:

Activation galleryeach function, with its derivative beneath
11sigmoid(z)its derivativeshaded: derivative below 0.05 (saturated)−505

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 g′(z) at every neuron. Lesson 1.7 found the sigmoid's; the others are as short:

σ′(z)=σ(z)(1−σ(z))tanh⁡'(z)=1−tanh2⁡zReLU⁡'(z)=𝟙[z>0]

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 z=0, 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:

activations.ipynb

The formulas and the nudges agree to about 10−11, which is rounding.

Saturation

Look at the sigmoid's slope as z grows:

activations.ipynb

At z=0 the sigmoid's slope is 0.25, its largest. At z=5 it's 0.0066, and at z=10, 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 z: its slope there is always exactly 1. For negative z, 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:

Saturation chainwhat's left of a gradient of 1, layer by layer
110⁻³10⁻⁶10⁻⁹10⁻¹²12345678layer 1 (input side)layer 8 (output side)

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:

activations.ipynb

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

A network has five layers and no activation functions. What can it compute?
A sigmoid unit’s weighted sum is z = 8 for every example. What happens to its weights during training?
Which activation would you pick first for the hidden layers of a new network?

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.

All sources for Simple Machine Learning