Search

Search papers and lessons

Gaussian Error Linear Unit (GELU)

Level 3 · 9 min read

GELU multiplies each input by the probability that a standard Gaussian draw falls below it, a smooth gate that passes large inputs, suppresses negative ones and is used in many transformers.

On this page

  • Colour = topic mix
  • Probability
  • Discrete math
  • Neural network basics
  • Calculus
  • Linear algebra
  • ReadReady to readNot yet

TL;DR

GELU is an Activation Function for neural networks. It multiplies each input xx by the probability that a standard Gaussian draw is below xx. Large positive inputs pass through almost unchanged, large negative inputs go to nearly zero, and the change in between is smooth. It is the activation used in many Transformer language models.

Builds on: Activation Function, Gaussian (Normal) Distribution · Leads to: Transformer

Where it's used

Language models. The feed-forward blocks in transformer models such as BERT, released in 2018, use GELU between their two linear layers. Later large language models in the GPT family, such as GPT-3, also used it, which is one reason it became a default in that design.

Vision transformers. Image models that split a picture into patches and process the patches with transformer blocks reuse the same block design, and so the same GELU.

Research on activations. Architecture papers often compare GELU with Rectified Linear Unit (ReLU) and with swish-style gates on the same tasks. The differences are usually small, so GELU is chosen mostly because the models that already work with it are known to train well.

How it works

A neuron takes a weighted sum xx and passes it through an activation. GELU's idea is a gate: it decides how much of xx to keep, and the decision depends on how large xx is compared with the typical noise in a standard Gaussian.

flowchart LR
  X[Input x] --> P[Find the probability that a standard Gaussian is below x]
  P --> M[Multiply x by that probability]
  M --> Y[Output]
  1. Take the input x=1x = 1. Look up the probability that a standard Gaussian draw ZZ is at most 1: P(Z≤1)≈0.841P(Z \le 1) \approx 0.841. Check: most draws fall below 1, so the gate is mostly open.

  2. Multiply: 1×0.841=0.8411 \times 0.841 = 0.841. Check: the output is slightly less than the input, as the gate is not fully open.

  3. Take the input x=−1x = -1. The probability that Z≤−1Z \le -1 is about 0.1590.159. Multiply: −1×0.159=−0.159-1 \times 0.159 = -0.159. Check: the output is negative, but small. It is not zero, unlike Rectified Linear Unit (ReLU).

  4. Take the input x=0x = 0. The probability that Z≤0Z \le 0 is exactly 12\tfrac{1}{2}, by symmetry. Multiply: 0×12=00 \times \tfrac{1}{2} = 0. Check: the output is zero at zero.

  5. Take the input x=3x = 3. The probability that Z≤3Z \le 3 is about 0.99870.9987. Multiply: 3×0.9987≈2.9963 \times 0.9987 \approx 2.996. Check: for large inputs the output is almost the input.

  6. Name the idea. GELU keeps an input in proportion to the chance that a standard Gaussian draw falls below it. This gives a smooth curve that is near the identity for positive inputs, near zero for very negative inputs, and dips slightly below zero in between.

The plot shows GELU next to ReLU. ReLU has a sharp corner at zero. GELU bends smoothly through it.

−3−2−101230123input xoutput
  • GELU (tanh approximation)
  • ReLU
GELU and ReLU agree for large positive inputs. Near zero, GELU is smooth and dips slightly below zero for negative inputs.

The math

Definition: GELU

Let ZZ be a standard Gaussian random variable, with mean 0 and variance 1. Let Φ(x)=P(Z≤x)\Phi(x) = P(Z \le x) be its cumulative distribution function. Then GELU⁡(x)=x Φ(x).\operatorname{GELU}(x) = x\,\Phi(x).

Here Φ(x)\Phi(x) takes values between 0 and 1, and it is increasing in xx. The function can also be written with the error function, erf⁡\operatorname{erf}, a standard special function that is the integral of a bell-shaped curve: Φ(x)=12(1+erf⁡(x/2))\Phi(x) = \tfrac{1}{2}\left(1 + \operatorname{erf}(x/\sqrt{2})\right). A common fast approximation, used in many libraries, replaces that integral with a hyperbolic tangent (tanh): GELU⁡(x)≈12 x(1+tanh⁡ ⁣(2π (x+0.044715 x3))).\operatorname{GELU}(x) \approx \tfrac{1}{2}\, x \left(1 + \tanh\!\left(\sqrt{\tfrac{2}{\pi}}\,\bigl(x + 0.044715\, x^3\bigr)\right)\right).

Step 2 in symbols: the gate value

For x=1x = 1, we need Φ(1)≈0.8413\Phi(1) \approx 0.8413, so GELU⁡(1)=1×0.8413=0.8413\operatorname{GELU}(1) = 1 \times 0.8413 = 0.8413. For x=−1x = -1, Φ(−1)=1−Φ(1)≈0.1587\Phi(-1) = 1 - \Phi(1) \approx 0.1587, so GELU⁡(−1)=−0.1587\operatorname{GELU}(-1) = -0.1587. The identity Φ(−x)=1−Φ(x)\Phi(-x) = 1 - \Phi(x) holds because the Gaussian is symmetric about zero.

Step 4 in symbols: the value at zero

By symmetry Φ(0)=12\Phi(0) = \tfrac{1}{2}, so GELU⁡(0)=0⋅12=0\operatorname{GELU}(0) = 0 \cdot \tfrac{1}{2} = 0.

Step 5 in symbols: the limits

As x→+∞x \to +\infty, Φ(x)→1\Phi(x) \to 1, so GELU⁡(x)≈x\operatorname{GELU}(x) \approx x. As x→−∞x \to -\infty, Φ(x)→0\Phi(x) \to 0, so GELU⁡(x)→0\operatorname{GELU}(x) \to 0, though it stays negative for finite negative inputs.

How the slope behaves

Training uses the slope of the activation. By the product rule, with φ(x)=Φ′(x)=12πe−x2/2\varphi(x) = \Phi'(x) = \frac{1}{\sqrt{2\pi}} e^{-x^2/2} the density of ZZ, ddxGELU⁡(x)=Φ(x)+x φ(x).\frac{d}{dx}\operatorname{GELU}(x) = \Phi(x) + x\,\varphi(x). At x=0x = 0 the slope is Φ(0)=12\Phi(0) = \tfrac{1}{2}. Unlike ReLU, the slope is defined and smooth everywhere, which is one reason training can be steadier.

Worked examples

Example
1. Tiny case: two inputs

Compute GELU at x=1x = 1 and x=−1x = -1.

  1. Look up Φ(1)≈0.8413\Phi(1) \approx 0.8413. Multiply: 1×0.8413=0.84131 \times 0.8413 = 0.8413.
  2. Look up Φ(−1)≈0.1587\Phi(-1) \approx 0.1587. Multiply: −1×0.1587=−0.1587-1 \times 0.1587 = -0.1587.
  3. Compare with ReLU: ReLU gives 11 and 00.
  4. Interpretation: GELU is slightly below the identity for the positive input, and it is small and negative for the negative input, while ReLU cuts it to zero.
Example
2. Standard case: the tanh approximation against the exact value

Compute GELU at x=0.5x = 0.5 two ways.

  1. Exact: Φ(0.5)≈0.6915\Phi(0.5) \approx 0.6915. Multiply: 0.5×0.6915=0.34570.5 \times 0.6915 = 0.3457.
  2. Approximation, step a: x3=0.125x^3 = 0.125, so x+0.044715 x3=0.5+0.005589=0.505589x + 0.044715\,x^3 = 0.5 + 0.005589 = 0.505589.
  3. Step b: multiply by 2/π≈0.79788\sqrt{2/\pi} \approx 0.79788 to get 0.403400.40340.
  4. Step c: tanh⁡(0.40336)≈0.3828\tanh(0.40336) \approx 0.3828.
  5. Step d: 12×0.5×(1+0.3828)=0.25×1.3828=0.3457\tfrac{1}{2} \times 0.5 \times (1 + 0.3828) = 0.25 \times 1.3828 = 0.3457.
  6. Compare: ReLU at 0.5 gives 0.5. Both the exact and the approximate GELU give 0.3457, so the approximation is accurate enough for training.
  7. Interpretation: GELU keeps only part of a moderately positive input, because the gate is about 0.69 at 0.5.
Example
3. Realistic case: a hidden layer of a transformer

A feed-forward layer produces the pre-activation vector (1.2,−0.4,2.5)(1.2, -0.4, 2.5). Apply GELU to each entry.

  1. For 1.21.2: Φ(1.2)≈0.8849\Phi(1.2) \approx 0.8849. Output 1.2×0.8849≈1.06191.2 \times 0.8849 \approx 1.0619.
  2. For −0.4-0.4: Φ(−0.4)≈0.3446\Phi(-0.4) \approx 0.3446. Output −0.4×0.3446≈−0.1378-0.4 \times 0.3446 \approx -0.1378.
  3. For 2.52.5: Φ(2.5)≈0.9938\Phi(2.5) \approx 0.9938. Output 2.5×0.9938≈2.48452.5 \times 0.9938 \approx 2.4845.
  4. The GELU output vector is (1.0619,−0.1378,2.4845)(1.0619, -0.1378, 2.4845).
  5. ReLU would give (1.2,0,2.5)(1.2, 0, 2.5). The two agree on the large entries, but GELU keeps a small negative value where ReLU sets zero.
  6. Interpretation: the small negative value carries a little signal to the next layer. A layer with many such entries passes slightly more information than one with ReLU.

Try it: a fourth entry is −2-2. What does GELU give? Answer: Φ(−2)≈0.0228\Phi(-2) \approx 0.0228, so the output is −2×0.0228=−0.0455-2 \times 0.0228 = -0.0455.

Common mistakes and tradeoffs

It is tempting to think GELU is just a smoother ReLU that is zero for negative inputs. In fact it is negative for small negative inputs, and its output dips below zero before rising back toward zero as the input becomes very negative. The sign of the output is part of how it behaves.

It is tempting to think GELU is always better than ReLU. In fact the gains depend on the model and the task, and they are often small. ReLU is cheaper to compute, because it needs only a comparison, while GELU needs a cumulative distribution or a tanh approximation.

It is tempting to think GELU is chosen for its statistical meaning. The Gaussian is a motivation, not a guarantee that the activation is optimal. Its value is that it works well in the models where it is used.

When to use it: in transformer blocks and other networks whose published designs already use it, where matching the design is worth more than the small cost of computation.

When not to use it: in small networks where computation cost matters more than the last point of accuracy, where ReLU does the job.

Alternatives: Rectified Linear Unit (ReLU) is the simplest, and parametric variants let the negative slope be learned. Sigmoid Function and Hyperbolic Tangent are older squashing functions, which suffer from the vanishing-gradient problem in deep stacks.

History

Before 2016, the most common activations were the sigmoid and the hyperbolic tangent, and ReLU had become standard in convolutional networks. Hendrycks and Gimpel proposed GELU in 2016 as a smooth gate that scales the input by a Gaussian probability, and reported gains over ReLU and other activations on several benchmarks.

GELU became widely used after the transformer models of 2018 onward, including BERT, which used it in its feed-forward blocks. Its use spread with the transformer design, and it is now common in language and vision models alike.

Recap

  • An activation function turns a weighted sum into a neuron's output, and without one a stack of layers is just a linear map.
  • GELU is xx times the standard Gaussian probability Φ(x)\Phi(x), which is the chance a Gaussian draw falls below xx.
  • It is near the identity for large positive inputs, near zero for large negative inputs, and slightly negative in between.
  • Its slope at zero is one half, and the slope is smooth everywhere, which is useful in training.
  • Read next: Transformer for the block design that most often uses GELU, and Rectified Linear Unit (ReLU) for the simpler activation it is often compared with.

Check yourself

QuestionRecall: what does Φ(x)\Phi(x) mean in the formula GELU⁡(x)=x Φ(x)\operatorname{GELU}(x) = x\,\Phi(x)?Show answer

Φ(x)\Phi(x) is the cumulative distribution function of a standard Gaussian: the probability that a standard Gaussian draw is at most xx. It is the gate that scales the input.

QuestionComputation: what is GELU of x=−1x = -1, using Φ(−1)≈0.1587\Phi(-1) \approx 0.1587?Show answer

Multiply −1-1 by 0.15870.1587, which gives −0.1587-0.1587. The output is small and negative.

QuestionWhy: why is the slope of GELU at zero useful for training, compared with ReLU?Show answer

GELU is smooth everywhere, and its slope at zero is one half, which is defined. ReLU has a corner at zero, where its slope jumps from 0 to 1. A smooth slope gives gradient-based training a well-defined direction at every input, so updates do not depend on which side of zero an input lies.

QuestionApplied: a designer replaces GELU with ReLU in a transformer's feed-forward block to save compute. Name one thing that changes in the output, and one thing that does not.Show answer

Changed: negative pre-activations now give exactly 0 instead of a small negative value, so the next layer receives less signal from those units. Unchanged: positive inputs still pass almost unchanged, since both functions are close to the identity for large positive input, so the layer's shape and the rest of the block are the same.

Sources