Activation Functions

2 min read

Activation functions introduce nonlinearity after the linear transformation in each neuron.

FunctionFormulaRangeNotes
Sigmoid11+ex\frac{1}{1+e^{-x}}(0,1)(0, 1)Historical; saturates → vanishing gradients
Tanhexexex+ex\frac{e^x - e^{-x}}{e^x + e^{-x}}(1,1)(-1, 1)Zero-centered but still saturates
ReLUmax(0,x)\max(0, x)[0,)[0, \infty)Default. Fast, no saturation for x>0x > 0. Dead neurons if x<0x < 0 always
Leaky ReLUmax(αx,x)\max(\alpha x, x)(,)(-\infty, \infty)Fixes dead neuron problem, α0.01\alpha \sim 0.01
GELUxΦ(x)x \cdot \Phi(x)(0.17,)\approx(-0.17, \infty)Smooth ReLU. Default in transformers (BERT, GPT)
SwiGLUSwish(xW)(xV)\text{Swish}(xW) \cdot (xV)Gated variant. Used in modern LLMs (LLaMA, PaLM)
Activation Functions

Why ReLU dominates:

  • Gradient is 1 for positive inputs → no vanishing gradient
  • Computationally trivial (just a threshold)
  • Sparse activation (many zeros) → efficient representation
  • Dying ReLU: if a pre-activation is negative for every input, it receives zero gradient forever and the unit goes dead. Leaky ReLU (αx\alpha x for x0x \leq 0) fixes this.

Gated activations (modern LLMs):

  • Swish (smooth, non-monotonic): Swish(x)=xσ(x)\text{Swish}(x) = x \cdot \sigma(x)
  • GLU — one projection produces content, another produces a gate: GLU(x)=xW1σ(xW2)\text{GLU}(x) = xW_1 \odot \sigma(xW_2)
  • SwiGLU plugs Swish into the GLU gate: SwiGLU(x)=(xW1)Swish(xW2)\text{SwiGLU}(x) = (xW_1) \odot \text{Swish}(xW_2)

Swish derivative reuses the sigmoid derivative: xSwish(x)=σ(x)+Swish(x)(1σ(x))\frac{\partial}{\partial x}\text{Swish}(x) = \sigma(x) + \text{Swish}(x)(1 - \sigma(x)).

Why non-linearities are essential: without them, stacked layers collapse to a single linear map (W1W2x=WxW_1 W_2 \mathbf{x} = W\mathbf{x}) — extra depth adds no representational power. With non-linearities, deep networks become universal function approximators.

Choosing:

  • CNNs → ReLU
  • Transformers → GELU or SwiGLU
  • Output layer: sigmoid (binary), softmax (multiclass), none (regression)

See also: The Artificial Neuron, Weight Initialization, Backpropagation

Linked from