Skip to content
mlmentorship

Activation functions

ReLU, GELU, swish, sigmoid, tanh. What each does, why GELU/swish replaced ReLU in transformers, and when to use which.

Published · 6 min read ·Core ·Foundation

Visual quick review

Visual first · depth when needed

Compare where sigmoid, ReLU, and exact GELU flatten so the connection between output shape and gradient flow is visible.

Preparing the visual…

Summary

An activation function is a (usually) elementwise nonlinearity applied between linear layers in a neural network. Without it, stacking linear layers collapses to a single linear layer (no expressive power gain). The choice of activation shapes optimization, gradient flow, and final accuracy.

The standard family

ActivationFormulaRangeUse today
SigmoidOutput of binary classifier; gates in LSTMs/GRUs. Hidden layers: avoid (saturating gradients).
TanhRNN hidden (legacy); zero-centered version of sigmoid.
ReLUDefault for CNNs and MLPs; cheap, fast.
Leaky ReLU, Avoids “dying ReLU” by leaking negative values.
ELU for , for Smooth and zero-centered. Slightly slower than ReLU.
GELU where is standard normal CDFDefault in transformers (BERT, GPT-1/2/3).
Swish / SiLUDefault in modern decoder LLMs (Llama, Mistral).
SoftmaxsimplexOutput of multi-class classifier; not used in hidden layers.

Why ReLU won (then why GELU/swish replaced it)

ReLU (Nair & Hinton, 2010) helped mitigate vanishing gradients in deep networks:

  • Gradient is exactly 1 in the active region (no saturation).
  • Computationally trivial: a single .
  • Sparse activations (~half are zero): biological intuition + computational efficiency.

But ReLU has the dying ReLU problem: a neuron stuck at has gradient 0 forever and never recovers.

GELU (Hendrycks & Gimpel, 2016) and swish / SiLU (Ramachandran et al., 2017) are smooth alternatives to ReLU. They retain small negative outputs rather than setting the entire negative half-line to zero. Their derivatives can still equal zero at isolated points, but they do not have ReLU’s half-line with an identically zero derivative. Both are widely used in transformer feed-forward blocks.

Optimization intuition

Output shape determines where backpropagated gradients shrink or stop.

Sigmoid: two saturating tails

The slope approaches zero at both output limits.

Logistic sigmoid from negative four to positive four The sigmoid rises smoothly through one half at input zero and flattens toward output zero on the left and output one on the right. Both flat tails are labeled as saturation regions where the derivative approaches zero. left tail: σ′(x) → 0 saturated: σ′(x) → 0 σ(0) = 0.5 −404 00.51 input x output

ReLU vs exact GELU

A half-line of zero slope differs from one smooth shoulder.

ReLU and exact GELU from negative three to positive three ReLU is exactly zero with zero derivative for every negative input, then follows a straight line after zero. Exact GELU, x times the standard normal cumulative distribution, has small negative outputs, a shallow minimum near negative three quarters, passes through zero, and approaches the same positive linear trend. ReLU is solid and GELU is dashed. ReLU: dead region, f′(x) = 0 ReLU: solid GELU: dashed smooth negative shoulder both pass through (0, 0) −303 0123 input x output
Read it this way: sigmoid loses slope in both tails, while ReLU preserves slope for positive inputs but kills it for every negative input. Exact GELU replaces that dead half-line with a smooth, slightly negative shoulder, not with an everywhere-nonzero derivative.

In 2026:

  • CNNs / MLPs: still mostly ReLU.
  • Transformers: GELU (BERT-era) or SwiGLU (modern Llama-style decoders).
  • RNNs: tanh / sigmoid for gates (legacy); RNNs are largely deprecated for new work.

SwiGLU and gated activations

Modern decoder LLMs (Llama 1/2/3, Mistral, Qwen) use SwiGLU in the FFN:

Two parallel linear projections, one passed through swish, then elementwise product, then a third linear projection. Slightly more parameters per FFN block than the original design, but better training dynamics. GLU = “gated linear unit.” Now standard.

When to use which output activation

TaskOutput activation
Binary classificationSigmoid
Multi-class classificationSoftmax
Multi-label classificationSigmoid (independent binary heads)
Regression (unbounded)Identity (no activation)
Regression (bounded )Sigmoid
Probability over a discrete distributionSoftmax
Embedding outputIdentity, then L2-normalize

Common pitfalls

  • Putting ReLU on the output. A regression with non-negative range should use ReLU or softplus on output; for general regression, no activation.
  • Sigmoid in hidden layers of deep nets. Saturates → vanishing gradients → no learning past a few layers.
  • Picking exotic activations to chase 0.5% accuracy. The activation choice rarely matters compared to data, regularization, and architecture.
  • Forgetting GELU has exact and approximate forms. The CDF-based and tanh-based formulas are numerically close but not identical; check your framework when exact reproducibility matters.