Visualized ML
Topics

B · Neural networks

Activation Functions

The nonlinearity after each layer: without it a deep network collapses into one linear model, and its shape decides whether gradients survive.

Try it: B2 · Activation Functions

Intuition

Between any two layers of a deep network sits an activation function, applied to each neuron's weighted sum on its own. It has two jobs. First, it must be nonlinear, or the layers collapse into a single linear one and depth buys nothing. Second, it must let gradients through, because training sends a signal backwards through every activation, and each one multiplies that signal by its slope.

The sigmoid does the first job well and the second badly: its slope is at most 0.25 and almost zero at the ends. Ten sigmoid layers can shrink the gradient a million times. ReLU, which is simply "negative becomes zero", has slope exactly 1 for every positive input, so the signal passes through untouched. That one change made deep networks trainable.

Left: the three classic activations and, dashed, their slopes. Right: the gradient left after passing back through \(n\) layers (sigmoid at its steepest, tanh at \(z = 1\), ReLU on its active side), on a log scale. Sigmoid's shrinks four times per layer; ReLU's stays at 1.

The math

Name\(f(z)\)\(f'(z)\)Range
Sigmoid\(\dfrac{1}{1 + e^{-z}}\)\(f(z)(1 - f(z))\), at most \(\tfrac14\)\((0, 1)\)
Tanh\(\dfrac{e^{z} - e^{-z}}{e^{z} + e^{-z}}\)\(1 - f(z)^2\), at most 1\((-1, 1)\)
ReLU\(\max(0, z)\)1 if \(z > 0\), else 0\([0, \infty)\)
Leaky ReLU\(\max(\alpha z, z)\), e.g. \(\alpha = 0.01\)1 or \(\alpha\)\(\mathbb{R}\)
GELU\(z\,\Phi(z)\)smooth, slightly negative near −1\(\approx[-0.17, \infty)\)

Why slopes multiply

Backpropagation applies the chain rule layer by layer. The gradient that reaches layer 1 of an \(L\)-layer network contains a product of one factor per layer, each the activation's slope times a weight:

\[ \frac{\partial L}{\partial \mathbf{h}^{(1)}} \;\propto\; \prod_{l=2}^{L} W^{(l)\top} \operatorname{diag}\big(f'(\mathbf{z}^{(l)})\big) \]

If the slopes are all at most \(\tfrac14\) (sigmoid), that product shrinks like \(4^{-L}\) unless the weights are large, and then the activations saturate and the slopes get even smaller. This is the vanishing gradient problem. If the per-layer factor is above 1 instead, the product grows exponentially: exploding gradients. Good activations and good initialization keep the factor near 1.

Worked example. A gradient of 1 at the output of a 10-layer network, best case for each activation (all slopes at their maximum, weights 1): sigmoid gives \(0.25^{10} \approx 9.5 \times 10^{-7}\); tanh gives \(1^{10} = 1\) at \(z = 0\), but at a typical \(z = 1\) its slope is 0.42 and \(0.42^{10} \approx 0.0002\); ReLU gives exactly 1 for any positive \(z\).

Choosing one

  • ReLU is the default for hidden layers: cheap, and gradients don't vanish for active units. Its flaw is the dying ReLU: a neuron whose input is negative for every example has slope 0 everywhere and never learns again. A too-large learning rate can kill many at once.
  • Leaky ReLU, ELU keep a small slope for negative inputs, so neurons can recover.
  • GELU and SiLU (\(z\,\sigma(z)\)) are smooth ReLU-like curves used in transformers; they let small negative values through and tend to train slightly better.
  • Tanh is zero-centred, which helps the next layer, and is still used inside recurrent networks.
  • Output layers follow the task, not these rules: identity for regression, sigmoid for yes/no, softmax for classes.

What ReLU networks compute

A ReLU network is piecewise linear: each neuron's kink splits the input space, and within each piece the whole network is one linear function. With many neurons the pieces are tiny and the function looks smooth. The number of pieces can grow exponentially with depth, which is one concrete sense in which deeper networks are more expressive.

History

Sigmoid and tanh dominated from the 1980s backpropagation work until about 2010. Sepp Hochreiter's 1991 thesis analysed the vanishing gradient problem. Rectifiers appear in neuroscience-inspired models (Fukushima, 1969), but ReLU's breakthrough was Nair and Hinton (2010) and Glorot, Bordes and Bengio (2011), and AlexNet (2012) showed it trained several times faster than tanh. GELU was proposed by Hendrycks and Gimpel in 2016 and became standard with BERT and GPT.

Connections

Builds on

Leads to

Further reading