GELU, short for Gaussian error linear unit, is an activation function for neural networks defined as x multiplied by Φ(x), where Φ is the cumulative distribution function of the standard normal distribution. It weights each input by how likely it is to exceed a random Gaussian threshold, producing a smooth curve that behaves like the identity for large positive values and approaches zero for large negative values.
Proposed in 2016, GELU became the default nonlinearity in many transformer models, including BERT, the GPT-2 and GPT-3 family and the Vision Transformer. In the broader field of artificial intelligence, it is a standard reference point when comparing ReLU, Swish and gated variants such as SwiGLU.
How GELU transforms an input
GELU combines a deterministic version of stochastic regularization with a linear pass-through. Its authors motivated it by imagining a neuron whose input is multiplied by 0 or 1 at random, with the probability of keeping it equal to Φ(x). Taking the expected value of that random gate gives x·Φ(x), so larger inputs are almost always kept, while strongly negative inputs are almost always suppressed.
Unlike ReLU, the curve is smooth and non-monotonic. It dips slightly below zero, reaching a minimum of about -0.17 near x = -0.75, and then rises back toward zero as inputs become more negative. The table below gives representative values that show this shape.
| Input x | GELU(x) | ReLU(x) |
|---|---|---|
| -3.0 | About -0.004 | 0 |
| -1.0 | About -0.159 | 0 |
| -0.75 | About -0.170 | 0 |
| 0.0 | 0 | 0 |
| 1.0 | About 0.841 | 1.0 |
| 3.0 | About 2.996 | 3.0 |
Because the function has a nonzero gradient for negative inputs, units do not become permanently inactive in the way that dead ReLU units can. The smooth derivative also gives optimizers a better-conditioned loss surface, which many practitioners associate with more stable training of deep transformer stacks.
Exact form and common approximations
The exact GELU uses the error function: GELU(x) = 0.5·x·(1 + erf(x / √2)). Early hardware and frameworks computed erf slowly, so two closed-form approximations became popular, and both remain in wide use for compatibility with pretrained checkpoints.
| Variant | Formula | Typical use |
|---|---|---|
| Exact (erf) | 0.5x(1 + erf(x/√2)) | Default in modern frameworks |
| Tanh approximation | 0.5x(1 + tanh(√(2/π)(x + 0.044715x³))) | Original GPT-2 and BERT code |
| Sigmoid approximation | x · sigmoid(1.702x) | Fast kernels, rough estimates |
The tanh form deviates from the exact function by less than about 0.001 across typical input ranges, which is negligible for training but can matter for reproducibility. Deep learning libraries expose a switch for this choice, for example approximate="tanh" in PyTorch, and the setting should match the one used in pretraining when loading published weights.
GELU compared with other activation functions
GELU sits between simple piecewise functions and learned gating mechanisms. It is closely related to Swish, also called SiLU, which is x·σ(x); the sigmoid approximation of GELU is in fact a scaled Swish. The comparison below summarizes the practical differences among common choices in modern architectures.
| Function | Definition | Main property | Where it appears |
|---|---|---|---|
| ReLU | max(0, x) | Cheap, sparse, can die | Convolutional networks |
| Leaky ReLU | max(αx, x) | Small negative slope | GANs, detection models |
| GELU | x·Φ(x) | Smooth, probabilistic gate | BERT, GPT-2, ViT |
| Swish (SiLU) | x·σ(x) | Smooth, close to GELU | EfficientNet, some LLMs |
| SwiGLU | Swish(xW)·(xV) | Learned gate, extra matrix | Llama-style LLMs |
Empirical differences between GELU and Swish are usually small and depend on the architecture and training recipe. Gated linear unit variants, including GEGLU, which uses GELU inside a gate, often improve transformer feedforward layers further but add a third weight matrix, so designers typically shrink the hidden width to keep the parameter count comparable.
Using GELU in practice
In transformers, GELU typically appears inside the feedforward sublayer, between the expansion projection and the contraction projection, often with a hidden width of four times the model dimension. It is rarely used in attention scores or output layers, where softmax or linear outputs are standard.
- Initialization: schemes designed for ReLU, such as He initialization, work acceptably with GELU in most networks.
- Normalization: GELU is usually paired with layer normalization or RMSNorm placed before the feedforward block.
- Performance: GELU costs more than ReLU per element, but fused GPU kernels make the overhead minor relative to matrix multiplication.
- Quantization: the smooth negative region needs care in low-precision inference, where lookup tables or polynomial fits are common.
Pitfalls and misconceptions
Most problems with GELU arise from mismatched implementations or exaggerated expectations rather than from the function itself. The table below lists frequent issues observed when training or deploying models that use it.
| Symptom | Likely cause | Fix |
|---|---|---|
| Small output drift after conversion | Exact and tanh forms mixed | Use the pretraining variant |
| Slow inference on edge devices | Unfused erf computation | Use a fused or approximate kernel |
| Accuracy loss after quantization | Negative dip poorly represented | Calibrate or use higher precision |
| No gain over ReLU | Small or shallow network | Keep ReLU if it is cheaper |
A common misconception is that GELU randomly drops units during inference. It does not: the stochastic interpretation only motivates the formula, and the function itself is fully deterministic. Another is that GELU alone explains transformer success; in reality, it is one of many design choices, and gated alternatives have since replaced it in many large language models.
In everyday language
A neural network is made of many small units that pass numbers along. After each unit does its math, an activation function decides how much of the result to pass on. GELU is one such decision rule.
GELU works like a soft gatekeeper. Big positive numbers pass through almost unchanged. Big negative numbers are mostly blocked. Numbers near zero are partly let through. It has no hard on-off switch, so the network can learn gently and steadily. That smoothness is why many modern language and image models use it.
Seen in the real world
An engineering team fine-tunes a BERT-style text classifier to route customer support tickets into twelve categories. The pretrained checkpoint uses GELU in every feedforward layer, implemented with the tanh approximation from the original release.
When the team exports the model to a new inference runtime, accuracy drops slightly, from about 91.8 percent to 91.3 percent on the validation set. Investigation shows that the runtime uses the exact erf form of GELU, so every layer produces marginally different values that compound through twelve layers.
After switching the runtime to the tanh variant, the outputs match the training framework and the original accuracy returns. The team adds an activation check to its deployment tests so that future conversions catch the mismatch automatically.