Activation Functions
The activation is the only nonlinear part of a layer, and the reason depth adds expressive power. We catalog the standard hidden units (sigmoid, tanh, ReLU and its descendants, plus GELU, softplus, swish and maxout), derive each unit's derivative in full, make the vanishing-gradient problem quantitative with the chain-rule product, work numeric examples, and explain why the saturating units gave way to ReLU and why ReLU's own dead-unit failure gave way to Leaky/PReLU/ELU/GELU.
╌╌╌╌
A layer is an affine map followed by a pointwise nonlinearity, . The affine part carries all the parameters; the nonlinearity supplies the entire nonlinear effect: strip it out and a deep stack collapses to a single linear map. The choice of therefore matters: it sets how gradients flow back through depth, whether units saturate, and how fast the network trains. This lesson catalogs the standard choices and their derivatives.
To make the requirement precise, suppose two layers with the identity: . The composite is one affine map with weight and bias , and no amount of stacking escapes the affine class. A single nonlinear between the two multiplications is what lets a network represent a function that no single affine map can. Everything below is about which to pick and what each choice costs in the backward pass.
The catalog
Every hidden unit in common use is one scalar function and its derivative. The derivative column matters most: during the backward pass the gradient at a unit is multiplied by , so the size of controls how much signal passes through. The following table is the reference the rest of the lesson expands.
| name | range | saturates? | notes | ||
|---|---|---|---|---|---|
| sigmoid | both tails | ; not zero-centered | |||
| tanh | both tails | zero-centered; | |||
| ReLU | left only | cheap; can die | |||
| Leaky ReLU | no | keeps a trickle | |||
| PReLU | as Leaky, learned | no | a trained parameter | ||
| ELU | if else | if else | left soft | smooth; pushes mean toward | |
| GELU | left soft | standard-normal CDF | |||
| Softplus | no | smooth ReLU; | |||
| Swish / SiLU | left soft | self-gated; non-monotone | |||
| Maxout | of the winning piece | no | learns its own shape; params |
Here is the logistic sigmoid, and are the standard-normal CDF and PDF, and is a small positive constant (or a learned parameter for PReLU). Two families fall out of the table: the saturating units (sigmoid, tanh) whose derivative vanishes at both extremes, and the non-saturating rectified units (ReLU onward) whose derivative stays bounded away from zero on at least the positive half-line.
Every derivative, derived
Backpropagation never needs the activation itself in the backward pass, only its derivative. Each derivative below takes one or two lines to derive.
Sigmoid
Write and differentiate by the chain rule:
The first factor is ; the second is , since . Hence
Two consequences read straight off this form. The derivative is expressed in the output , so a forward pass that cached needs no re-exp in the backward pass, only . And because , the product is a downward parabola in capped at its vertex, which is the source of the ceiling derived below.
Tanh
The tanh derivative follows the same pattern. With , the quotient rule gives numerator over , and expanding the two squares,
so
Since , tanh is a rescaled, recentered sigmoid; the zero-centering lifts its peak derivative to (at ) versus the sigmoid's , and makes tanh the better of the two saturating choices.
ReLU
ReLU is differentiable everywhere except the kink at , and its derivative is just the indicator of the active region:
with the value at taken by convention to be (frameworks pick or ; the measure-zero choice is irrelevant in practice). This step-function derivative, a hard gate of or , is the source of both ReLU's strength and its dead-unit pathology.
Leaky ReLU and PReLU
Leaky ReLU replaces the flat left branch with a shallow line of slope ,
The derivative is now on the entire negative half-line rather than , so a unit sitting in the negative region still receives an -scaled gradient. PReLU is identical except is a learned parameter (one per channel), so the negative slope adapts during training rather than being fixed at .
ELU
ELU keeps the identity for but replaces the kink with a smooth exponential that saturates gently toward on the left:
The left derivative is strictly positive and continuous, and at both branches agree ( from the right, from the left; with the common choice the derivative is continuous too). Because the negative branch bottoms out at instead of , ELU can output negative values, which pulls each layer's mean activation toward zero and keeps the next layer's pre-activations nearer the high-gradient region.
Softplus
Softplus is a smooth ReLU; its derivative is the sigmoid, which is why it appears whenever a strictly positive, differentiable output is wanted:
As , (it hugs the ReLU line); as it decays to smoothly. Its derivative never reaches or exactly, so it has no dead region but also no perfectly flat gradient of .
GELU and swish/SiLU
GELU gates the input by the probability that a standard normal falls below it, with the standard-normal CDF and its density. The product rule gives
Swish (a.k.a. SiLU) is the same idea with a logistic gate, , so
Both are smooth, both are the identity in the large- limit, and both dip slightly below zero for moderately negative before returning to — a non-monotone shape that the empirical record favors in deep Transformers. Their minimum sits near for swish (value ) and near for GELU (value ).
Maxout
Maxout is the outlier: it is not a fixed scalar shape but a piecewise-linear unit that takes the max over affine pieces,
The gradient flows entirely through the winning piece, so maxout has no flat region at all as long as one piece is increasing; with and it recovers ReLU exactly. The cost is the parameters, which is why it is rarely the default.
The rectified family shares one shape below, all identity on the right and differing only in how they treat the negative side: hard zero (ReLU), a shallow line (Leaky/PReLU), or a smooth curve that dips and returns (ELU, GELU, swish).
The vanishing-gradient problem, made quantitative
The backward pass through an -layer network multiplies one Jacobian factor per layer. Along a single path of units the gradient that reaches layer carries a product of activation derivatives,
so the magnitude of the gradient at depth scales like the product of the per-layer derivatives (times the weights). If each factor is below , the product shrinks geometrically with depth, and the deep layers stop receiving signal.2
The sigmoid is the worst case: its derivative is small even at its maximum. Maximizing over : set and differentiate , giving , so the peak is at , i.e. :
So a sigmoid contributes at most per layer, and far less in the tails. Work the tail out numerically: at , , and . The tanh is better (it peaks at ) but still vanishes in the tails, with .
| depth | (sigmoid peak) | (tanh peak) | (ReLU active) |
|---|---|---|---|
Ten sigmoid layers, each at their best point, already attenuate the gradient by a factor of , and the realistic factor is worse because most units sit off their peak. This is why deep sigmoid/tanh stacks were nearly untrainable before ReLU, and why initialization that keeps pre-activations near (where is largest) matters so much; see initialization.3
The product-of-derivatives view makes the depth penalty geometric, not additive. The next figure shows the same gradient magnitude collapsing layer by layer as it propagates backward through a saturating stack.
ReLU and the dead-unit problem
ReLU sidesteps vanishing gradients on its active side outright: for the derivative is exactly , so the product across active layers is , with no attenuation at all. That single property, more than any other, made very deep networks trainable.
The flat left half causes a different failure. A unit whose pre-activation is negative for every training example outputs and has derivative , so it receives no gradient and never updates: it is dead.
The mechanism is worth tracing through the chain rule. The gradient on a weight of a ReLU unit is
Once on every example, the factor is for every one, so regardless of the upstream gradient or the inputs. Nothing in the update can move or , so the unit cannot recover: the failure is irreversible.
The fixes all do the same thing: give the negative side a nonzero slope so a stuck unit still receives a gradient and can recover.
| unit | negative branch | derivative at | recovers? |
|---|---|---|---|
| ReLU | no — gradient is | ||
| Leaky ReLU | , | yes — small constant slope | |
| PReLU | , learned | yes — slope adapts per channel | |
| ELU | yes — smooth, mean toward | ||
| GELU | yes — smooth, near- slope |
Leaky ReLU and PReLU keep a literal straight line of slope on the left so everywhere; a dead-looking unit still receives an -scaled gradient and can recover. ELU and GELU replace the kink with a smooth curve whose negative-side derivative is small but strictly positive, which additionally pushes the activation mean toward zero, a self-normalizing effect that helps the next layer's pre-activations stay near the high-gradient region.4
Output units, not hidden units
The hidden-layer rules above optimize for gradient flow. The output unit is chosen instead to match the target's range and the loss, and the saturating units that fail in hidden layers become the right choice at the output, where they pair with a log-loss that cancels the saturation. The output choice is tied to the loss; the table below is the standard pairing.
| task | output activation | range | paired loss |
|---|---|---|---|
| regression | none (linear) | mean squared error | |
| binary classification | sigmoid | binary cross-entropy | |
| multiclass | softmax | simplex, sums to | categorical cross-entropy |
| count / nonnegative | softplus or exp | Poisson / MSE |
The cancellation is worth seeing once. With softmax output and cross-entropy loss against a one-hot target , the gradient with respect to the logits collapses to
The exponential of the softmax and the logarithm of the loss cancel, leaving a gradient that is just the prediction error, bounded and never vanishing even when saturates near or . Pairing the saturating output with the wrong loss (say squared error on a sigmoid) reintroduces a factor and the saturation stalls learning — the reason the pairing in the table is not optional.
Which activation to use where
Defaults, in the order you should reach for them:
| layer / task | first choice | why | fall back to |
|---|---|---|---|
| hidden (MLP, CNN) | ReLU | cheap, no right-side saturation | Leaky ReLU / ELU if units die |
| hidden (Transformers) | GELU | smooth, strong empirically | Swish / SiLU |
| hidden, self-normalizing | SELU / ELU | drives mean toward | — |
| output, regression | linear | unbounded targets | — |
| output, binary | sigmoid | gives a probability | — |
| output, multiclass | softmax | distribution over classes | — |
| recurrent gates | sigmoid + tanh | gating needs ; state needs | — |
Activations after ReLU
Goodfellow's catalog predates the activations that now dominate large models. Two later developments matter, and both were settled empirically rather than from first principles.
GELU and swish were found empirically. Hendrycks & Gimpel's Gaussian Error Linear Unit (2016) motivated as multiplying the input by the probability a standard normal falls below it — a smooth, stochastic-looking gate rather than a hard ReLU switch. Around the same time Ramachandran, Zoph & Le (2017) ran an automated search over candidate activation formulas and the winner, , they named Swish; it is the SiLU of the table with a learnable or fixed gain . Neither unit is dramatically better than ReLU on small networks, but on deep Transformers the smoothness and the small negative dip consistently shave a little off the loss, which is why GELU became the default in BERT and GPT-family models.
Gated linear units reshaped the Transformer's MLP. Dauphin et al. (2017) introduced the gated linear unit (GLU), which splits a projection into two halves and lets one gate the other, . Shazeer (2020) swapped the sigmoid gate for a swish gate and found SwiGLU, , improved Transformer feed-forward blocks enough that it is now standard in models such as LLaMA and PaLM. The lesson's scalar-activation view still holds — the nonlinearity is where the depth comes from — but the modern feed-forward block gates two linear projections against each other rather than applying one pointwise .
A quantitative footnote on the vanishing-gradient section: the modern answer is not
only use ReLU
but normalize.
Batch normalization (Ioffe & Szegedy, 2015)
and layer normalization (Ba, Kiros & Hinton, 2016) rescale each layer's
pre-activations to keep them near the high-gradient region, so even saturating
units stay trainable at depth. Normalization and residual connections together are
what keep deep stacks trainable despite the arithmetic above.7
Takeaways
- The activation is the only nonlinearity in a layer; its derivative is what backpropagation multiplies, so the size of governs gradient flow.
- peaks at and peaks at ; both saturate in the tails, and a product of small factors vanishes geometrically with depth — the vanishing-gradient problem.
- ReLU has derivative : no right-side saturation, so deep stacks train, but a unit driven entirely negative is dead ( forever).
- Leaky ReLU / PReLU / ELU / GELU / swish give the negative side a nonzero slope so no unit can be permanently frozen; ELU and GELU additionally center the mean; softplus and maxout round out the smooth and piecewise-linear ends.
- Hidden layers optimize for gradient flow (ReLU/GELU); output units match the target's range and loss (linear / sigmoid / softmax), where saturation is desired and cancelled by cross-entropy.
Footnotes
- Goodfellow, Deep Learning, §8.2.5 / §10.7 — the vanishing- and exploding-gradient problem: backprop multiplies per-layer Jacobians, so derivatives below shrink the gradient geometrically with depth. ↩
- Goodfellow, Deep Learning, §6.3.2 / §8.4 — saturation and initialization: careful weight initialization buys precisely this, pre-activations near , where is largest. ↩
- Chollet, Deep Learning with Python, Ch. 4 — practical guidance on choosing activations: ReLU as the hidden-layer default, sigmoid/softmax at the output, and the failure modes that motivate the alternatives. ↩
- Goodfellow, Deep Learning, §6.2.2 — Output Units: sigmoid and softmax paired with the negative log-likelihood, so the loss's undoes the unit's and the saturation that would otherwise stall learning. ↩
- Stevens et al., Deep Learning with PyTorch, Ch. 6 — activations in practice:
nn.Tanhandnn.ReLUin a fitted network, and the effect of the choice on the training curve. ↩ - Primary sources: Hendrycks & Gimpel,
Gaussian Error Linear Units (GELUs)
(2016); Ramachandran, Zoph & Le,Searching for Activation Functions
(2017) for Swish/SiLU; Dauphin et al.,Language Modeling with Gated Convolutional Networks
(2017) for the GLU; Shazeer,GLU Variants Improve Transformer
(2020) for SwiGLU; Ioffe & Szegedy,Batch Normalization
(2015) and Ba, Kiros & Hinton,Layer Normalization
(2016) for keeping pre-activations in the high-gradient region. ↩
╌╌ END ╌╌