TL;DR
GELU is an Activation Function for neural networks. It multiplies each input by the probability that a standard Gaussian draw is below . 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 and passes it through an activation. GELU's idea is a gate: it decides how much of to keep, and the decision depends on how large 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]
-
Take the input . Look up the probability that a standard Gaussian draw is at most 1: . Check: most draws fall below 1, so the gate is mostly open.
-
Multiply: . Check: the output is slightly less than the input, as the gate is not fully open.
-
Take the input . The probability that is about . Multiply: . Check: the output is negative, but small. It is not zero, unlike Rectified Linear Unit (ReLU).
-
Take the input . The probability that is exactly , by symmetry. Multiply: . Check: the output is zero at zero.
-
Take the input . The probability that is about . Multiply: . Check: for large inputs the output is almost the input.
-
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.
- GELU (tanh approximation)
- ReLU
The math
Let be a standard Gaussian random variable, with mean 0 and variance 1. Let be its cumulative distribution function. Then
Here takes values between 0 and 1, and it is increasing in . The function can also be written with the error function, , a standard special function that is the integral of a bell-shaped curve: . A common fast approximation, used in many libraries, replaces that integral with a hyperbolic tangent (tanh):
Step 2 in symbols: the gate value
For , we need , so . For , , so . The identity holds because the Gaussian is symmetric about zero.
Step 4 in symbols: the value at zero
By symmetry , so .
Step 5 in symbols: the limits
As , , so . As , , so , though it stays negative for finite negative inputs.
How the slope behaves
Training uses the slope of the activation. By the product rule, with the density of , At the slope is . Unlike ReLU, the slope is defined and smooth everywhere, which is one reason training can be steadier.
Worked examples
Compute GELU at and .
- Look up . Multiply: .
- Look up . Multiply: .
- Compare with ReLU: ReLU gives and .
- 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.
Compute GELU at two ways.
- Exact: . Multiply: .
- Approximation, step a: , so .
- Step b: multiply by to get .
- Step c: .
- Step d: .
- 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.
- Interpretation: GELU keeps only part of a moderately positive input, because the gate is about 0.69 at 0.5.
A feed-forward layer produces the pre-activation vector . Apply GELU to each entry.
- For : . Output .
- For : . Output .
- For : . Output .
- The GELU output vector is .
- ReLU would give . The two agree on the large entries, but GELU keeps a small negative value where ReLU sets zero.
- 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 . What does GELU give? Answer: , so the output is .
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 times the standard Gaussian probability , which is the chance a Gaussian draw falls below .
- 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 mean in the formula ?Show answer
is the cumulative distribution function of a standard Gaussian: the probability that a standard Gaussian draw is at most . It is the gate that scales the input.
QuestionComputation: what is GELU of , using ?Show answer
Multiply by , which gives . 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
- Hendrycks and Gimpel, Gaussian Error Linear Units (GELUs): https://arxiv.org/abs/1606.08415
- Rectifier (neural networks) (Wikipedia): https://en.wikipedia.org/wiki/Rectifier_(neural_networks)
- Activation function (Wikipedia): https://en.wikipedia.org/wiki/Activation_function