ML Systems Lab Open interactive version →
Intermediate 29 min read activationsReLUGELUvanishing gradients

Activation Functions

Sigmoid, tanh, ReLU, Leaky ReLU, GELU, Swish — saturation and dying neurons

Backprop's own closing promised this handoff directly: ReLU stops the vanishing-gradient message from shrinking on the way down, but it trades one failure for another — the dead neuron. That's where this module actually starts: not re-deriving why depth stalls (Backprop already showed that), but working out exactly when a ReLU neuron goes silent forever, and what to do about it.

First, the reminder that motivates the swap in the first place. Build a 10-layer network with sigmoid hidden layers, train it for an hour, and the loss barely budges — the last couple of layers learn, the first eight sit frozen.

As you saw in Backprop, a sigmoid's slope tops out at 0.25, and multiplying that in at every layer shrinks the signal to about 0.25¹⁰ ≈ one in a million by layer 10 — the early layers get no usable gradient and never learn. Swap sigmoid for ReLU in every hidden layer, retrain from scratch, and the whole network comes alive: loss drops, every layer updates, it converges. ReLU's slope is a clean 1 for any active neuron, so it passes the backward signal through untouched — no shrinking factor to compound.

The activation function is the little non-linear squash applied after each layer — what lets a network bend space instead of only drawing straight lines, and, as just shown, what decides whether the learning signal survives the trip backward through the layers.


ReLU's own flaw: the dead neuron

Look again at the same slope that just fixed vanishing gradients: ReLU's slope is exactly 1 for positive inputs, and exactly 0 for negative ones. Vanishing gradients were about the signal getting small everywhere at once; the dead neuron is a different failure — the signal goes to *exactly, permanently* zero for one specific neuron. If a neuron's input lands negative for *every* training example — often because one too-large gradient step shoved its bias down — ReLU outputs 0, its own slope there is 0, and it receives zero gradient *forever*. It cannot recover on its own: a slope of zero means no future update ever nudges it back toward positive territory. This isn't rare in practice — a too-high learning rate can silently kill a large share of a network's ReLU neurons within the first epoch, invisible on the loss curve.

Leaky ReLU fixes this cheaply by giving negatives a tiny slope (0.01) instead of a flat zero, so a trickle of gradient always flows and a neuron can climb back to positive territory. GELU goes further with a smooth curve that never fully flatlines and softly gates each input by how positive it is — which is why BERT, GPT, and essentially every modern Transformer use it.


One rule that is not up for debate: the output activation

All of the above is about the *hidden* layers. The *output* activation is a correctness constraint, not a preference. For a probability (binary classification) you must use sigmoid [squashes any real number into the open interval (0,1)]; for a set of class probabilities, softmax [exponentiates each logit and divides by the sum of all the exponentials, so every output lands in (0,1) and the whole set sums to exactly 1]; for a plain number (regression), no activation at all. Putting a ReLU on the output of a classifier would let it emit nonsensical values — you match the output activation to what the answer is supposed to *be*, and that is a rule, not a tuning knob.


Softmax's own trap: confident on data it has never seen

Cross-entropy only stops penalising a prediction once the correct class's logit is far ahead of every other logit, so training keeps pushing the network to widen that gap for as long as it helps the loss. That habit doesn't switch off on an input the network is actually unsure about — an ambiguous test example still gets pushed through the same wide-gap machinery, so softmax reports 99% confidence on something that is genuinely closer to a coin flip. Two fixes, both applied without changing the architecture: temperature scaling — divide every logit by a constant T>1 before the softmax, which shrinks the gap and cools the reported confidence without changing which class wins — and label smoothing — during training, replace the one-hot target (1 for the correct class, 0 for every other) with a softened target (for example 0.9 on the correct class, the remaining 0.1 split across the rest), so cross-entropy stops rewarding an infinitely wide gap in the first place.

Key points

Takeaway

The history of activation functions is a sequence of gradient-flow fixes: sigmoid killed gradients through saturation, ReLU fixed saturation but introduced dead neurons, GELU eliminated both — and each step unlocked a new generation of viable network depth.

Recap

Check your understanding

Q1. ReLU has a 'dying ReLU' problem. Explain mechanistically what causes it and what Leaky ReLU does to fix it.

Q2. Why does GELU outperform ReLU in transformer architectures? What is its mathematical definition? Select the TWO correct statements.

Q3. Softmax output sums to 1 and is non-negative, so it is a valid probability distribution. However, neural networks trained with softmax are overconfident. Why?

Try it interactively

ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.

Open ML Systems Lab →