FFN Activation Functions › GELU: The Smooth Alternative
Language AI HandbookAll chapters FFN Activation Functions › GELU: The Smooth AlternativeOne section · Sections in this chapter4 / 15 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Hendrycks and Gimpel introduced the Gaussian Error Linear Unit (GELU) in 2016, though it didn't gain widespread adoption until BERT popularized it in 2018. GELU addresses ReLU's sharp corner by introducing smooth, probabilistic gating.
The intuition behind GELU is elegant: instead of deterministically zeroing negative values, gate each input by its probability of being positive under a standard normal distribution. Inputs that are clearly positive (large positive values) pass through nearly unchanged. Inputs that are clearly negative (large negative values) are nearly zeroed. Inputs near zero, where classification is uncertain, are partially attenuated.
Think of GELU as a soft decision boundary instead of a hard one. Whereas ReLU says "if you're negative, you're blocked, full stop," GELU says "if you're probably negative, you're mostly blocked, but we'll keep a little signal proportional to how uncertain we are." This probabilistic framing connects activation functions to Bayesian reasoning: the gate is literally a probability estimate.
The mathematical formulation follows this intuition:
where:
- : the input value to the activation function
- : the cumulative distribution function (CDF) of the standard normal distribution, representing the probability that a standard normal random variable is less than or equal to
The CDF itself is computed using the error function:
where:
- : the probability that a standard normal random variable takes a value less than or equal to
- : the Gauss error function, a special function that arises in probability and statistics
- : a scaling factor that converts from the standard normal to the error function's parameterization
The CDF ranges from 0 to 1. For large positive , , so . For large negative , , so . The transition is smooth, governed by the bell curve of the normal distribution.
The key insight here is the choice of the normal distribution as the gating mechanism. By using , we are essentially asking: "if I draw a random variable from a standard normal, how likely is it to be less than this input?" For a pre-activation value of 0, the answer is 50%, so GELU attenuates zero-valued inputs by half. For a pre-activation value of 2, the answer is about 97.7%, so GELU passes almost all of that signal. This creates a natural, data-independent scale for "how much" to pass each value.
An activation function that gates inputs by their Gaussian CDF values, creating a smooth, non-monotonic function that approximates stochastic regularization. GELU is the standard activation for encoder-style transformers like BERT and RoBERTa.
from scipy.special import erf
def gelu_exact(x):
"""GELU activation using exact computation."""
return x * 0.5 * (1 + erf(x / np.sqrt(2)))
def gelu_approximate(x):
"""GELU approximation using tanh (faster, commonly used)."""
return 0.5 * x * (1 + np.tanh(np.sqrt(2 / np.pi) * (x + 0.044715 * x**3)))
def gelu_derivative(x):
"""Derivative of GELU (exact)."""
phi = 0.5 * (1 + erf(x / np.sqrt(2)))
pdf = np.exp(-0.5 * x**2) / np.sqrt(2 * np.pi)
return phi + x * pdfGELU has a distinctive shape: it's nearly linear for positive values, smoothly transitions through zero, and has a slight dip into negative territory before approaching zero asymptotically. This dip is unique. For small negative inputs (around ), GELU outputs slightly negative values. This non-monotonic behavior distinguishes it from ReLU variants.
The derivative of GELU, which determines gradient flow during backpropagation, is:
where:
- : the standard normal CDF (as defined above)
- : the standard normal probability density function (PDF)
- The first term contributes the "identity-like" gradient for positive inputs
- The second term creates a smooth transition region and ensures the derivative is continuous everywhere
Notice that this derivative is never exactly zero except in the limit as . For any finite input, GELU provides some gradient signal, addressing the dying-neuron problem that afflicts ReLU. The gradient is small for very negative inputs, but it is nonzero. This means GELU neurons can, in principle, recover from temporarily negative pre-activations during training.


Worked Example: Tracing a Value Through GELU
To build concrete intuition, let's trace a single pre-activation value through the GELU computation step by step. Suppose the FFN's first layer produces a pre-activation of for a particular neuron.
Step 1: Compute the CDF value at . The standard normal CDF at 1.0 is . This says: "there is an 84.1% probability that a standard normal variable is at most 1.0."
Step 2: Multiply the input by this CDF value:
The neuron passes about 84% of its signal. Now suppose the pre-activation is . The CDF value is , so:
The neuron passes about 16% of its (negative) signal, attenuating it substantially but not zeroing it. Compare this to ReLU, which would output 0 for and 1.0 for . GELU's output is softer in both cases: positive activations are attenuated slightly, negative activations are attenuated heavily but remain nonzero.
This soft gating behavior, controlled by the Gaussian CDF, is what makes GELU particularly well-suited to the pre-activation distributions that arise in transformer FFNs. Transformer pre-activations after LayerNorm are approximately normally distributed. GELU's gating function is calibrated to the same normal distribution, making the soft-gating particularly well-aligned with the actual statistical properties of the data it operates on.
GELU Approximations
Computing the exact error function is expensive. In practice, transformers use fast approximations. The most common is the tanh approximation:
where:
- : the input value
- : the hyperbolic tangent function, which outputs values in the range
- : a scaling constant derived from properties of the normal distribution
- : an empirically-fitted coefficient that improves the approximation accuracy
- : a polynomial that approximates the argument to the error function
This formula looks complex, but it avoids the expensive erf computation while matching GELU closely. The constants were determined by fitting to the exact GELU curve, minimizing the maximum error across typical input ranges.
The reason this approximation works so well is that and are closely related functions. Both are odd functions (symmetric about the origin), both map the real line to , and both have an S-shaped curve. The polynomial inside the call adjusts the rate of transition to closely match the CDF shape. The result is an approximation that differs from the exact GELU by at most about 0.004 across the full input range.
Let's compare the exact and approximate versions:
# Compare exact and approximate GELU
x_test = np.linspace(-4, 4, 1000)
gelu_exact_vals = gelu_exact(x_test)
gelu_approx_vals = gelu_approximate(x_test)
max_error = np.max(np.abs(gelu_exact_vals - gelu_approx_vals))
mean_error = np.mean(np.abs(gelu_exact_vals - gelu_approx_vals))GELU approximation accuracy: Maximum absolute error: 0.000473 Mean absolute error: 0.000196
The approximation error is negligible for practical purposes. Most deep learning frameworks offer both versions, with the tanh approximation being faster on hardware that lacks specialized erf instructions.

Why GELU Became Standard for Encoders
GELU's adoption in BERT (2018) was a turning point in natural language processing. Before BERT, most language model pretraining used simpler activations. Devlin et al. chose GELU for BERT partly based on empirical results and partly based on the theoretical appeal of smooth, probabilistic gating. The success of BERT then cemented GELU as the activation of choice for the encoder-style transformer community, including RoBERTa and ALBERT, as well as ELECTRA and virtually every BERT successor.
The connection between GELU and dropout is worth understanding. Hendrycks and Gimpel's original GELU paper framed the activation as an approximation to a network where each neuron's activation is randomly zeroed (like dropout) with probability . From this perspective, GELU implements a kind of "stochastic depth" within each neuron's activation: high-activation neurons are nearly always kept; low-activation neurons are frequently zeroed out. This connection to regularization may partly explain why GELU trains more stably than ReLU at large scale.
In practice, the difference between GELU and a well-tuned ReLU model often comes down to training dynamics in the first few thousand steps. GELU's smooth gradients allow larger learning rates and more aggressive optimization without the risk of mass neuron death that can afflict ReLU networks. Once training is stable, both activations often achieve similar final performance. But for large models trained on large datasets, where training stability matters, GELU's smooth behavior provides meaningful advantages.