The GELU activation function — the Gaussian Error Linear Unit — is the activation behind modern transformers such as BERT and GPT. It is a smooth, probabilistic gate that weights each input by how likely it is to be positive, giving slightly better accuracy than ReLU in deep models. This guide covers the GELU activation function’s formula, why transformers prefer it, GELU versus ReLU, and Python code.

Try it: interactive GELU plot
The blue curve is the GELU activation function; the orange dashed curve is its derivative. Notice the gentle dip below zero — GELU is smooth everywhere, unlike ReLU’s sharp kink.
The GELU activation function formula
The GELU activation function — Gaussian Error Linear Unit — multiplies the input by Φ(x), the cumulative distribution function of the standard normal. Intuitively, instead of a hard on/off gate like ReLU, GELU weights each input by the probability that it is greater than a random Gaussian, giving a smooth, probabilistic gate.
Why transformers use GELU
GELU vs ReLU
ReLU has a sharp corner at zero and kills all negatives; the GELU activation function curves smoothly through the origin and lets slightly-negative inputs pass with a small weight. GELU costs more to compute, but in large models the accuracy gain is usually worth it. On small networks, ReLU is still a fine, cheaper default.
GELU activation function in Python
import numpy as np
def gelu(x):
return 0.5 * x * (1 + np.tanh(
np.sqrt(2/np.pi) * (x + 0.044715 * x**3)))
print(gelu(np.array([-2.0, 0.0, 2.0])))
# [-0.045 0. 1.955]PyTorch provides it directly as torch.nn.GELU(). See the original GELU paper.
Compute cost in practice
The exact definition uses the Gaussian error function, but frameworks ship a fast tanh-based approximation that is accurate to a few decimal places and much cheaper to evaluate on a GPU. For inference on edge devices the extra cost over a simple rectifier can still matter, so some deployed models train with the smooth curve and then swap to a lighter unit for serving. On large accelerators the difference is usually negligible next to the cost of the attention layers around it, which is why nearly every large language model simply uses the smooth version everywhere. A practical habit is to match whatever the architecture you are fine-tuning was originally trained with, since mixing activations between pre-training and fine-tuning can subtly shift the distributions each layer expects.
Related activation functions
- Activation Functions — the complete guide (hub).
- ReLU — the simpler baseline.
- Swish — a similar smooth gate.
- ELU — another smooth option.