Activation Functions
The nonlinear core of every layer
Activations inject the nonlinearity that lets stacked layers express complex functions. Choice of activation governs gradient flow, saturation and training stability.
What you will learn
- Explain why nonlinearity is mandatory in stacked layers
- Compare sigmoid, tanh, ReLU and variants by gradient behavior
- Diagnose dead-ReLU and saturation from training signals
What is this?
An activation function applies a fixed nonlinear transform to a layer's pre-activation (linear) output: ReLU zeroes negatives, sigmoid squashes to (0,1), tanh to (-1,1). Without nonlinearity, any stack of layers collapses into a single linear transformation — the network could express nothing more than linear regression. Activations are what let depth mean something.
Why do you need it?
- Two stacked linear layers equal one linear layer — nonlinearity is the reason depth buys capacity.
- Sigmoid/tanh offer smooth, bounded outputs useful at the very end for probabilities.
- ReLU avoids saturation in most of its range, keeping gradients alive through many layers.
- Choosing badly (saturating activations deep) causes vanishing gradients and slow, stuck training.
Mental model
A dimmer switch vs a hard on/off switch
Sigmoid/tanh vs ReLU
A dimmer (sigmoid) is smooth but only changes output meaningfully in a narrow middle band — past the ends the 'wire' is saturated. A hard switch (ReLU) is blunt but always responsive: below cutoff it's off, above it passes everything through.
How it works
- A layer computes z = Wx + b; the activation then applies element-wise: a = f(z).
- ReLU: f(z) = max(0, z); linear for z>0, zero for z<=0 — cheap, no saturation above zero.
- Leaky ReLU: max(0.01z, z) keeps a small gradient for negatives so dead neurons leak.
- Sigmoid: 1/(1+e^-z), bounded (0,1); gradient maxes at 0.25 at z=0 and decays — a vanish risk in deep stacks.
- tanh: bounded (-1,1), zero-centered, gradient maxes at 1 — a common choice before ReLU took over.
Activation shapes and their gradient ranges.
In production
- Image classifiers default to ReLU in convolutional stacks with a batchnorm before each block.
- Output heads switch to sigmoid (binary logits) or softmax (multiclass) — the last activation is chosen for the loss.
- Transformers use GELU; RNN/LSTM gates use sigmoid+tanh because bounded outputs keep gate ranges stable.
Best practices
- Use ReLU-family activations in hidden layers; reserve sigmoid/tanh for output gates or final probabilities.
- Put normalization before the activation (pre-activation) for smoother gradients.
- Match the final activation to the loss (sigmoid+BCE, softmax+CE, none+MSE/linear head).
- If many ReLUs are dead, try leaky ReLU or fix initialization instead of adding more units.
Common mistakes
- Using sigmoid throughout hidden layers — deep stacks suffer vanishing gradients.
- Sigmoid-for-binary at the end but then passing through MSE and double-punishing confidence.
- Counting dead neurons due to a wrong init (too-large weights zero everything) and 'fixing' it with width.
- Assuming activations are interchangeable — GELU vs ReLU changes loss trajectories, not just speed.
Common issues
The model outputs only one class after trainingFinal sigmoid saturates and gradients vanish for the minority class
Use logits into BCEWithLogits (no final sigmoid) so the gradient stays finite near saturation.
Most hidden activations are exactly 0Dead ReLUs from bad initialization or high learning rate permanently zeroing units
Lower init scale (Kaiming), reduce LR, or switch to leaky ReLU.
Deep stack trains very slowlySaturating activations (sigmoid/tanh) without normalization
Swap to ReLU-family + batchnorm/groupnorm and re-init.
Debugging tips
- Print the fraction of negative pre-activations: >50-60% dead-zone activation suggests dying layers.
- Visualize activation histograms per layer after a few steps, not just loss curves.
- If loss is flat near 0.69 (ln2) on binary, the end activation likely saturates.
- A sudden NaN usually signals an activation like exp/log interaction, not the optimizer.
Interview checkpoint
Quick quiz
Which activation's gradient is upper-bounded by 0.25?
A chain of layers with no activation function will behave exactly like a single linear layer.