Equations · Hendrycks and Gimpel, “Gaussian Error Linear Units (GELUs)” (2016)
The GELU activation formula, explained
A smooth version of ReLU, used in BERT, GPT-2 and many Transformers since. Each input is multiplied by the probability that a standard normal variable is below it: large positive inputs pass almost unchanged, large negative ones are nearly zeroed, and the region around 0 is curved rather than kinked.
Term by term
One pre-activation value. GELU is applied elementwise, so for a vector or tensor it acts on each entry separately.
The cumulative distribution function of the standard normal : the probability . It rises smoothly from 0 to 1, passing at . A capital almost always means this CDF.
The input, scaled by a weight between 0 and 1 that depends on the input itself. ReLU uses a hard weight instead: 1 if , else 0.
A fast approximation of given in the paper. Some libraries use it (PyTorch's approximate="tanh"); others compute exactly with the error function. The two differ by less than .
Hyperbolic tangent · Square root · Pi · Superscript (power or label)
A small worked example
At : , so ; the tanh form gives too. At : . ReLU would give and . GELU is slightly negative for negative inputs, with a minimum of about near .
In code
import torch
import torch.nn.functional as F
x = torch.tensor([-1.0, 0.0, 1.0])
F.gelu(x) # exact: x * Phi(x)
F.gelu(x, approximate="tanh") # the tanh approximation
x * 0.5 * (1 + torch.erf(x / 2**0.5)) # the same thing by handEvery symbol in it
Other equations
- Scaled dot-product attention
- Softmax
- Cross-entropy loss
- Adam update
- Layer normalization
- RMSNorm
- The ELBO
- KL divergence
- Backpropagation through one layer
- LoRA
- Dropout
- Batch normalization
- Rotary position embedding (RoPE)
- Multi-head attention
- PPO clipped objective
- DPO loss
- Policy gradient (REINFORCE)
- Bellman equation
- Q-learning update
- InfoNCE contrastive loss
- Diffusion forward process
- DDPM simplified loss
- VAE reparameterization trick
- GAN minimax objective
- AdamW
- Cosine learning-rate schedule
- Perplexity
- Logistic regression
- Normal equation (linear regression)
- Singular value decomposition (SVD)
- Principal component analysis (PCA)