Foundations to Frontier
Tests
E. Deep Learning › E1 Activation Functions (one page each)

GELU

The Gaussian error linear unit, GELU(z) = z·Φ(z), weights each input by the probability that a standard normal variable falls below it. It is smooth, close to ReLU for large |z|, and dips slightly below zero for negative inputs, with a minimum of about −0.17 at z ≈ −0.75. GELU is the default activation in BERT, GPT and Vision Transformers. This page covers its definition, its interpretation as an expected stochastic gate, the tanh and sigmoid approximations used in practice, its gradient and its non-monotonic dip. In the exercises you compare the approximations with the exact function and locate the minimum, then test whether the dip matters by training networks with GELU, a monotone GELU and ReLU.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Let inputs through in proportion to how large they are

ReLU makes a hard decision: positive passes, negative is blocked. GELU makes a soft one. It multiplies each input by the probability that a standard normal value is smaller, Φ(z). Large positive inputs are passed almost fully, large negative ones almost fully blocked, and inputs near zero are partly passed. The result looks like a smoothed ReLU with a small dip below zero.

Activation lab: the curve, its slope, and what a network learns with it
z from −4 to 4 · orange f · dashed blue f′
GELU: training accuracy after 300 steps 1.000
Compare activations at the same number of steps. Smooth, non-saturating units usually fit the spirals fastest. Sigmoid and the hard-threshold units learn slowly or need surrogate gradients. Gated and maxout units use twice as many hidden pre-activations. Learning rates are fixed per activation and not tuned, so treat small differences as noise (press "new data and weights").
Why engineers care

Transformers for language, vision and multimodal perception almost all use GELU or its relatives (E1-9, E1-11) in their feed-forward blocks. When deploying them on edge hardware, the choice between exact GELU and its tanh or sigmoid approximations affects both speed and whether the deployed model matches the trained one.

📝 Quick check (Level 0)
0/3 answered
  1. 1. GELU(z) =
  2. 2. GELU for large positive z is about…
  3. 3. Unlike ReLU, GELU is…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Values by hand

  • Definition: GELU(z) = z·Φ(z) = (z/2)(1 + erf(z/√2)).
  • Values: GELU(0) = 0; GELU(1) = Φ(1) ≈ 0.841; GELU(−1) = −Φ(−1) ≈ −0.159; GELU(3) ≈ 2.996.
  • Gradient: GELU′(z) = Φ(z) + z·φ(z), where φ is the normal density. GELU′(0) = 0.5 and GELU′(1) ≈ 1.083. The slope exceeds 1 for z above about 0.752 (since GELU′(z) + GELU′(−z) = 1, this mirrors the minimum at −0.752), peaks at about 1.13 at z = √2, and then settles back to 1.
  • The dip: GELU′(z) = 0 at z ≈ −0.752, where GELU reaches its minimum of ≈ −0.170. For z < −0.752 the function increases back toward 0, so the slope is negative.
  • Approximations:
    • tanh: 0.5z(1 + tanh(√(2/π)(z + 0.044715z³))), used in the original BERT and GPT-2 code;
    • sigmoid: z·σ(1.702z), the cheapest.
  • Stochastic view: GELU(z) = E[z·m], where m ~ Bernoulli(Φ(z)). It is the expected output of a gate that keeps larger inputs more often, the deterministic counterpart of adaptive dropout.
📝 Quick check (Level 1)
0/3 answered
  1. 1. GELU′(0) =
  2. 2. GELU's minimum is about…
  3. 3. The cheapest common approximation is…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Exact or approximate?

Exercise 1.

  • Accuracy over [−6, 6]: the tanh approximation is within 4.7 × 10⁻⁴ of the exact GELU; the sigmoid approximation is within 2.0 × 10⁻².
  • The minimum: Newton’s method on GELU′(z) = 0 finds z = −0.7518 and GELU = −0.1700.
  • The stochastic view: a Monte Carlo average of z·m with m ~ Bernoulli(Φ(z)) reproduces GELU.

Practical notes.

  • PyTorch’s nn.GELU() is exact, and nn.GELU(approximate='tanh') gives the tanh form.
  • A model trained with one form and deployed with another sees small output differences. They are usually harmless, but they matter for bit-exact regression tests and for quantised deployment.
  • The sigmoid form is the same function as SiLU with β = 1.702 (E1-9).
  • For a standard normal input, 22.6% of values fall in the dip region z < −0.752, where the gradient is negative.
nn.GELU()                    # exact erf
nn.GELU(approximate='tanh')  # BERT/GPT-2 approximation
🐍 Exercise 1: exact GELU, approximations and the dip
Implement exact GELU with erf, the tanh and sigmoid approximations, the gradient, Newton's method for the minimum, and a Monte Carlo check of the stochastic-gate view.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Maximum error of the tanh approximation on [−6, 6] is about…
  2. 2. The sigmoid approximation of GELU equals…
  3. 3. Share of N(0, 1) values in GELU's negative-slope region?

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Derivation from a stochastic regulariser. Hendrycks and Gimpel (2016) combined three ideas:

  • dropout, which multiplies by a random 0/1 mask;
  • zoneout;
  • ReLU, which multiplies by a deterministic 0/1 mask, 1[z > 0].

Multiplying by m ~ Bernoulli(Φ(z)) makes the mask depend on the input value. Taking the expectation gives the deterministic GELU, z·Φ(z). The choice of Φ is motivated by pre-activations being roughly normal after normalisation layers.

Properties.

  • Smooth: infinitely differentiable, with nonzero curvature everywhere, which helps second-order methods and physics-informed losses.
  • Non-monotonic: the dip means the gradient is negative for z < −0.752. Small negative inputs can therefore pass a small negative signal and gradient, unlike ReLU’s flat zero.
  • Not convex.
  • Asymptotically: GELU tends to ReLU as |z| → ∞. Scaled as z·Φ(z/ε), it approaches ReLU as ε → 0.

Why transformers use it. BERT (2018) and GPT (2018) adopted GELU, and empirical comparisons in transformers have found GELU and its gated relatives slightly better than ReLU (Shazeer, 2020). The advantage is modest and architecture-dependent. In small MLPs, exercise 2 found no advantage at all.

📝 Quick check (Level 3)
0/3 answered
  1. 1. GELU was derived as the expectation of…
  2. 2. GELU is…
  3. 3. The first major models to adopt GELU were…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

Does GELU’s dip matter?

History.

  • D. Hendrycks and K. Gimpel (2016) introduced GELU.
  • J. Devlin et al. (BERT) and A. Radford et al. (GPT), both 2018, made it the transformer default.
  • A. Dosovitskiy et al. (2021) used it in Vision Transformers.
  • N. Shazeer (2020) compared GELU, ReLU and gated variants in transformer feed-forward layers.

Exercise 2. A 4-hidden-layer MLP (width 64) on spirals, He initialisation, minibatch SGD. There are three activations:

  • ReLU;
  • GELU;
  • monotone GELU, which equals GELU but is held constant at its minimum for z < −0.752. This removes the dip and nothing else.

The table gives training accuracy per seed, and the share of hidden pre-activations below −0.752 after training.

activation learning rate after 100 steps after 300 steps share in the dip region
ReLU 0.05 0.912, 0.958, 0.910 0.998, 0.995, 0.998 0.123
GELU 0.05 0.845, 0.863, 0.862 0.987, 0.987, 0.995 0.124
monotone GELU 0.05 0.820, 0.828, 0.837 0.985, 0.983, 0.992 0.123
ReLU 0.3 0.995, 0.968, 1.000 1.000, 1.000, 1.000 0.158
GELU 0.3 0.997, 0.987, 0.998 1.000, 1.000, 1.000 0.155
monotone GELU 0.3 0.997, 0.973, 0.998 1.000, 1.000, 1.000 0.157
  • The dip region is used. About 12–16% of hidden pre-activations lay below −0.752 after training (22.6% would for a standard normal).
  • The dip helped slightly. At the smaller learning rate, GELU was ahead of monotone GELU after 100 steps in every seed, here and in three further seeds, by 0.02 to 0.07.
  • ReLU learned fastest in this small MLP at the smaller learning rate, and all three were equivalent at the larger one. Every network reached at least 0.98.
  • GELU’s reported advantages come from large transformers with normalisation layers, and do not transfer automatically to small networks. Test on your own architecture rather than assuming.
🐍 Exercise 2 (research): does GELU's dip matter?
Train spiral classifiers with ReLU, GELU and a monotone GELU (dip removed) at learning rates 0.05 and 0.3, recording accuracy after 100 and 300 steps and how many pre-activations fall in the dip region.

GELU in AI, robotics and aerospace

  • Vision transformers for satellite imagery, aerial mapping and robot perception use GELU in every MLP block.
  • Language and multimodal models for mission planning assistants and robot instruction following are GELU-based transformers.
  • Edge deployment: the tanh or sigmoid approximations, or a lookup table, replace erf on microcontrollers and NPUs. Check accuracy after the substitution.

References

  1. D. Hendrycks, K. Gimpel (2016). “Gaussian error linear units (GELUs).” arXiv:1606.08415.
  2. J. Devlin, M.-W. Chang, K. Lee, K. Toutanova (2019). “BERT: pre-training of deep bidirectional transformers for language understanding.” Proc. NAACL.
  3. A. Dosovitskiy et al. (2021). “An image is worth 16×16 words: transformers for image recognition at scale.” Proc. ICLR.
  4. N. Shazeer (2020). “GLU variants improve transformer.” arXiv:2002.05202.