๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #74

GELU

GELU (Gaussian Error Linear Unit) is a smooth activation function that weights its input by how likely that input is under a standard normal distribution. It's the default activation inside every major Transformer-based model โ€” BERT, GPT, and essentially every modern large language model.

Formula

\[ \text{GELU}(z) = z \cdot \Phi(z) \]

\(\Phi(z)\) is the cumulative distribution function of the standard normal distribution โ€” the probability that a standard normal random variable is less than \(z\) (see Probability Distributions). Intuitively: GELU multiplies the input by "the probability this input would be kept if we randomly decided whether to zero it out, weighted by how large it is" โ€” a smooth, probabilistic version of ReLU's hard \(z\ge0\) gate.

A Practical Approximation

Computing \(\Phi(z)\) exactly is expensive, so implementations commonly use a fast approximation:

\[ \text{GELU}(z) \approx 0.5z\left(1+\tanh\left[\sqrt{2/\pi}(z+0.044715z^3)\right]\right) \]

Graph

Similar overall shape to ReLU, but smooth everywhere, with a small negative dip near zero instead of a hard corner.

GELU vs ReLU โ€” The Key Differences

PropertyReLUGELU
SmoothnessSharp corner at \(z=0\)Smooth (infinitely differentiable) everywhere
Negative inputsAlways exactly 0Small negative values allowed near \(z=0\), approaching 0 for very negative \(z\)
MonotonicityMonotonic (never decreases)Slightly non-monotonic โ€” dips very slightly before rising
InterpretationA hard, deterministic gateA smooth, probabilistic gate

Code

import torch.nn as nn
import torch

layer = nn.GELU()   # PyTorch's default is the exact (erf-based) formulation
x = torch.tensor([-2.0, -0.5, 0.0, 0.5, 2.0])
print(layer(x))

# Approximate version, matching the tanh-based approximation formula above
layer_approx = nn.GELU(approximate='tanh')
print(layer_approx(x))

Where It's Used Today

GELU is the standard activation inside the feed-forward blocks of Transformer architectures (see the Transformers category) โ€” used in BERT, GPT-style models, and most modern large language models. Its smoothness is believed to help with the optimization dynamics of very large, very deep Transformer stacks, though it's more computationally expensive than plain ReLU.

Common Mistakes

  • Assuming GELU is simply "ReLU with better branding" โ€” the probabilistic interpretation and smooth, slightly non-monotonic shape are genuinely different mathematical objects, not a cosmetic variation.
  • Not distinguishing between the exact and tanh-approximated GELU implementations when comparing published model architectures โ€” some models specify one variant explicitly, and results can differ subtly.

Interview Relevance

Q: "Why do Transformer architectures like BERT and GPT typically use GELU instead of ReLU?" GELU is smooth everywhere (no sharp corner at zero, unlike ReLU) and weights inputs probabilistically rather than with a hard cutoff, which empirically tends to help optimization in very deep, large-scale Transformer stacks. It has become something of a de facto standard in Transformer feed-forward blocks, though the specific advantage over ReLU is more empirical than proven.

Practice Question

Using the tanh-based approximation formula, would GELU(z) be closer to 0 or closer to z for a large positive z? What does that tell you about GELU's behavior compared to ReLU for large positive inputs?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

GELU โ€“ FAQs

Quick answers about learning GELU in Deep Learning.

This free note from CodingNow 2.0 explains GELU in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including GELU, is 100% free with no signup required.
With focused practice, most students grasp GELU in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now