Activation Explorer
Visualize how different activation functions transform input values into output signals within a neural network.
Overview
Why depth needs a bend
Two linear layers in a row compute W2(W1x) = (W2W1)x, which is just another single matrix. A hundred stacked linear layers still only draw a straight boundary. Inserting a non-linear function between them is what lets a network represent curves, corners and disjoint regions.
Activation Functions in DL: A Practical Guide
Without a non-linearity between layers, stacking them is pointless: any chain of linear maps collapses into a single linear map. Activations are what make depth mean something.
The four you will actually meet
- Sigmoid — 1 / (1 + e−x), output in (0, 1). Reads as a probability, which is why it survives at the output of binary classifiers.
- Tanh — output in (−1, 1) and centred on zero, which tends to train better than sigmoid in hidden layers.
- ReLU — max(0, x). Gradient is exactly 1 for positive input and exactly 0 otherwise. Cheap, and the default for hidden layers.
- Leaky ReLU — max(0.01x, x). Keeps a small gradient alive on the negative side.
Numbers at three inputs
Definitions blur together; a table of actual outputs does not. For x = −2, 0 and 3:
| Function | x = −2 | x = 0 | x = 3 |
|---|---|---|---|
| Sigmoid | 0.12 | 0.50 | 0.95 |
| Tanh | −0.96 | 0.00 | 1.00 |
| ReLU | 0.00 | 0.00 | 3.00 |
| Leaky ReLU | −0.02 | 0.00 | 3.00 |
| GELU | −0.05 | 0.00 | 2.996 |
Three things fall out of that table. Sigmoid and tanh both flatten at the extremes, so their derivatives there are near zero. ReLU discards the negative side entirely and passes the positive side through untouched. And GELU behaves almost identically to ReLU for large inputs while staying smooth near zero.
The derivatives are what decide trainability:
| Function | Maximum derivative | Derivative at the extremes |
|---|---|---|
| Sigmoid | 0.25 | → 0 both ends |
| Tanh | 1.00 | → 0 both ends |
| ReLU | 1.00 | 1 (positive), 0 (negative) |
Sigmoid's ceiling of 0.25 is the reason deep sigmoid networks do not train: ten layers multiply to 0.25¹⁰ ≈ one in a million, and the early layers stop receiving a usable signal.
The dying ReLU problem
ReLU outputs zero for any negative input, and its derivative there is also zero. If a neuron’s weights are pushed far enough negative that it outputs zero for every training example, it receives no gradient — and with no gradient it can never recover. The neuron is permanently dead.
This is not rare. A large learning rate can kill a substantial fraction of a layer in a single update, and dead neurons are invisible in the loss curve; the network simply has less capacity than you think it does.
Leaky ReLU fixes it by giving negative inputs a small slope, typically 0.01, so the gradient is never exactly zero and a neuron can always climb back. ELU and GELU do the same with a smooth curve, and GELU is the standard choice in transformers.Which one to use where
- Hidden layers, general default — ReLU. Cheap, no vanishing gradient in the positive region, and works.
- Hidden layers, if neurons are dying — Leaky ReLU or ELU.
- Transformers — GELU, essentially universally.
- Recurrent networks — tanh inside the cell, because bounded outputs keep the recurrent state from growing without limit.
- Anywhere deep, with sigmoid — avoid. Its maximum derivative of 0.25 shrinks the gradient by at least a factor of four per layer.
Output activations are a separate decision
Hidden-layer activations exist to introduce non-linearity. Output activations exist to put the prediction in the right range, and the choice is dictated by the task rather than by preference:
- Regression — no activation. The output must be unbounded.
- Binary classification — sigmoid, giving one probability in [0, 1].
- Multi-class, one label — softmax, giving probabilities across classes that sum to 1.
- Multi-label — sigmoid on each output independently, since several labels can be true at once and the outputs should not sum to 1.
One practical warning: frameworks usually fold the output activation into the loss for numerical stability, so CrossEntropyLoss in PyTorch expects raw logits and applies softmax itself. Adding your own softmax before it applies the function twice and quietly degrades training.
Choosing one
| Where | Use |
|---|---|
| Hidden layers, convolutional networks | ReLU |
| Hidden layers, transformers | GELU or SiLU |
| Hidden layers, recurrent networks | Tanh (inside the gates) |
| Output, binary classification | Sigmoid |
| Output, multi-class | Softmax |
| Output, multi-label | Sigmoid per label |
| Output, unbounded regression | None |
The multi-label row is worth stating plainly: softmax forces the outputs to sum to 1, which is wrong when several labels can be true at once. Using it there is a modelling error, not a preference.
And "output: none" is deliberate. Frameworks apply softmax or sigmoid inside the loss function for numerical stability, so adding it in the model as well applies it twice — a real bug that trains slowly and silently.
The dying ReLU problem, and its fixes
A ReLU unit whose pre-activation is always negative outputs zero, has a derivative of zero, receives no gradient, and never recovers. It is dead for the rest of training.
A network can lose a substantial fraction of its units this way, usually after a learning rate large enough to push weights into a bad region.
The responses, in order of usefulness:
- He initialisation — scale initial weights by √(2/fan_in). It is designed for ReLU; Xavier initialisation, designed for tanh, leaves activations shrinking through depth.
- A lower learning rate. A sudden collapse in the fraction of active units is the signature.
- Batch normalisation before the activation, which keeps pre-activations centred so about half the units are active.
- Leaky ReLU (0.01x on the negative side), which keeps a small gradient alive.
Diagnose it by logging the fraction of zero activations per layer. Around 50% is healthy; 90% and rising is a problem.
Try this above
- Drag Input Value (x) to around 6 and look at sigmoid and tanh. Both are pinned near their ceiling and the curve is nearly flat — that flatness is saturation.
- Bring x back toward 0. Both functions are at their steepest here, which is where they learn fastest.
- Set x to −3 and note ReLU outputs exactly 0, not a small number.
What usually goes wrong
Sigmoid in deep hidden layers. Its derivative peaks at 0.25, so gradients shrink by at least 4× per layer. Five layers of sigmoid multiplies the gradient by roughly 0.255 ≈ 0.001 in the best case — the early layers barely move. This is the vanishing gradient problem, and it is why ReLU became standard.Dying ReLU. A neuron pushed firmly negative outputs 0 and has zero gradient, so nothing ever pulls it back. It is dead for the rest of training. Leaky ReLU exists precisely to leave a small escape route.
In one line
ReLU in the hidden layers by default; sigmoid or softmax only at the output, where the bounded range is the point.
The smooth modern variants
GELU multiplies the input by the probability that a standard normal variable is below it — effectively a soft, probabilistic gate. It is smooth everywhere, allows small negative values through, and is the standard in BERT, GPT and most transformers.
SiLU (Swish) is x · sigmoid(x) — very similar in shape, slightly cheaper, and common in EfficientNet and modern YOLO variants.
Mish, ELU, SELU each have theoretical arguments and appear occasionally. None has displaced the three above.
Is the difference worth caring about? For convolutional networks, usually not — ReLU is faster and within noise of the alternatives. For transformers, GELU is the convention and the small gain appears real. The bigger wins are elsewhere: architecture, data, learning rate, normalisation.
| Function | Smooth? | Cost | Typical use |
|---|---|---|---|
| ReLU | No (corner at 0) | Cheapest | CNNs, general default |
| Leaky ReLU | No | Cheap | When units are dying |
| GELU | Yes | Moderate | Transformers |
| SiLU | Yes | Moderate | Modern vision models |
| Tanh | Yes | Moderate | RNN gates, bounded outputs |
| Sigmoid | Yes | Moderate | Binary output, gates |
Where they sit in a block
The conventional order in a convolutional network is Conv → BatchNorm → ReLU, with normalisation before the activation so the input to ReLU stays centred.
In transformers the arrangement differs: LayerNorm → Attention → residual, then LayerNorm → FFN(GELU) → residual. The pre-normalisation variant, with normalisation before each sublayer rather than after, is what makes very deep transformers trainable.
A detail worth knowing: when batch normalisation follows a convolution, the convolution's bias is redundant, because normalisation subtracts the mean anyway. That is why bias=False appears on convolutions in ResNet-style code.
Six activations, and the one property they exist for
Without a non-linear activation a hundred layers collapse into one matrix. This proves that, then compares what each function does to a gradient.
Questions people ask
Why not use a linear activation? Because stacked linear layers collapse into one matrix, so the network could only ever draw a straight boundary.
Is ReLU differentiable at zero? Strictly no — there is a corner. Frameworks define it as 0 by convention, and it causes no practical trouble.
Should I use sigmoid in hidden layers? No. Its derivative ceiling of 0.25 causes vanishing gradients. It survives only at outputs.
Is tanh better than sigmoid? In hidden layers, yes — it is zero-centred and its derivative reaches 1. Both are still worse than ReLU for depth.
Do different layers need different activations? Rarely. One for the hidden layers and one for the output covers almost every architecture.
What about learnable activations? PReLU learns the negative slope, and it helps marginally at the cost of extra parameters. Not usually worth the complexity.
Recap in one screen
- Activations are what stop stacked layers collapsing into a single linear map.
- Sigmoid's derivative peaks at 0.25, which is why deep sigmoid networks do not train.
- ReLU passes a derivative of exactly 1 on the positive side — that is what makes depth work.
- Its cost is dead units; He initialisation, normalisation and a sensible learning rate prevent most of them.
- GELU and SiLU are the smooth modern choices, standard in transformers and recent vision models.
- Leave the output layer unactivated and let the loss function apply softmax or sigmoid.
Check yourself
0 of 3Answer without scrolling back up.
Stack 100 linear layers with no activation between them. What can the result represent?
A chain of matrix multiplies collapses into one matrix. Without a non-linearity, depth buys literally nothing.
A ReLU unit outputs zero for every input in your dataset and its gradient never recovers. This is called:
ReLU's gradient is exactly zero on the negative side, so a unit pushed fully negative can never be updated back. Leaky ReLU exists to keep a small gradient alive there.
Why do deep sigmoid networks train so badly?
Sigmoid's derivative peaks at 0.25 and falls toward zero at the tails. Chain a dozen of those together and the gradient reaching the early layers is effectively zero.
Activation Functions in DL
Two linear layers in a row compute W2(W1x) = (W2W1)x, which is just another single matrix. A hundred stacked linear layers still only draw a straight boundary. Inserting a non-linear function between them is what lets a network represent curves, corners and disjoint regions.
Further reading
- Gaussian Error Linear Units (GELUs)Hendrycks & Gimpel, 2016