AI Compass
Compass

Activation functions

ReLU, GELU, SiLU, and why sigmoid made deep networks unusable for twenty years: the function that decides gradient flow.

·2 min read·By Fachredaktion Technik
DETAIL
3 sections

The idea

After every weighted sum sits a small function that reshapes the result. It is why a network can do more than draw a straight line.

The simplest and today's standard: everything negative becomes zero, everything positive stays. It is called ReLU.

The usual functions

FunctionShapeUsed today for
ReLU0 if negative, identity otherwiseDefault in CNNs and networks generally
GELULike ReLU but smoothTransformers
SiLU / SwishLike GELU, shaped slightly differentlyModern vision models, transformers
SigmoidSquashes to 0 to 1Output only, binary classification
TanhSquashes to −1 to 1Rare, in recurrent networks
SoftmaxVector to probabilitiesAlways at a multi-class output

Why sigmoid fails

import numpy as np

def sigmoid(x): return 1 / (1 + np.exp(-x))
def sigmoid_grad(x): s = sigmoid(x); return s * (1 - s)

for x in [-6, -2, 0, 2, 6]:
    print(f"x={x:3d}  sigma={sigmoid(x):.4f}  derivative={sigmoid_grad(x):.4f}")
# x = 0 gives the largest derivative: 0.25.
# Over 20 layers: 0.25^20 = 9e-13. The gradient is gone.

The maximum derivative of the sigmoid is 0.25. Every layer therefore damps the gradient by at least a factor of four. ReLU has derivative exactly one in the positive range and damps not at all there.

Avoiding dead neurons

  • Use He initialisation, not Xavier, when using ReLU.
  • Do not set the learning rate too high; an oversized step can push a neuron permanently negative.
  • Monitor the zero fraction per layer. Above 90 percent is a warning sign.
  • If the problem persists, switch to GELU or leaky ReLU, which keep a small gradient in the negative range.

The formulas

The four most important activations

ReLU(x) = max(0, x) GELU(x) = x · Φ(x) ≈ 0.5·x·(1 + tanh(√(2/π)·(x + 0.044715·x³))) SiLU(x) = x · σ(x) softmax(z)ᵢ = exp(zᵢ) / Σⱼ exp(zⱼ)

ReLU clips, GELU and SiLU do the same smoothly, softmax normalises a whole vector.

x
the input value
Φ
the standard normal cumulative distribution function
σ
the logistic function

Why GELU won in transformers

Three properties acting together:

  • Smooth. The derivative is continuous everywhere, stabilising optimisation with adaptive methods.
  • Non-monotonic. For slightly negative inputs GELU is slightly negative rather than zero. That preserves a small gradient and prevents dead units.
  • Self-gating. The factor Φ(x) reads as a probability of letting the input through: a deterministic variant of dropout.

The measured difference from ReLU is small, usually under one point of perplexity, but it is consistent and costs nothing.

Gated linear units

Modern language models mostly no longer use a plain activation in the feed-forward block but a gate:

SwiGLU

SwiGLU(x) = ( SiLU(x·W) ⊙ (x·V) ) · W₂

One branch is activated, the other stays linear, and the two are multiplied element by element.

W, V, W₂
three weight matrices instead of two
element-wise product

That costs a third matrix, which is why the intermediate width is usually reduced from 4h to 8h/3 to keep the parameter count constant. The measured gain justifies the trade in practically every current model.

The numerical point about softmax

Softmax is never computed alone but always together with the logarithm for cross-entropy. log_softmax is numerically stable; log(softmax(x)) is not, because small probabilities underflow to zero first. See Numbers in a computer.

Related courses and sources

ToolFreeEN

TensorFlow Playground

A neural network in the browser with sliders for layers, activation and learning rate. You see in seconds what each knob does.

For anyone who wants to see what learning rate, layers and activation actually do.

Was this page helpful?
Activation functions