Neural network formulas: a cheat sheet · The next level

GELU: the input times a probability

The activation of BERT, GPT and ViT. We take it apart bone by bone to see why it is more than a “smoothed ReLU”: where the normal distribution function Φ comes from, how GELU arises as the average of a random mask, where its minimum lies, what its derivative Φ(x) + xφ(x) looks like and how accurate the tanh and sigmoid approximations are.

Javharbek Abdulatipov · 35 min read
$$\operatorname{GELU}(x)=x\cdot\Phi(x)=\frac{x}{2}\Bigl(1+\operatorname{erf}\frac{x}{\sqrt2}\Bigr)$$

Why we need it

An activation function is the nonlinearity placed after every linear layer. Without it, any number of layers collapses into a single matrix multiplication. For a long time the standard was ReLU: $\max(0,x)$ — let the positive through, zero out the negative. GELU (Gaussian Error Linear Unit, Hendrycks and Gimpel, 2016) solves the same task more gently: the input is not simply passed or cut, it is multiplied by the probability that it should be passed.

Where it appears:

  • BERT, RoBERTa, GPT, GPT-2, GPT-3 — in every feed-forward (FFN) block of the transformer: $\operatorname{FFN}(h)=W_2\,\operatorname{GELU}(W_1h+b_1)+b_2$;
  • Vision Transformer (ViT) and many computer-vision models after it;
  • GeGLU — the gated variant $\operatorname{GELU}(xW)\odot(xV)$, used for example in Gemma; its relative SwiGLU with SiLU powers LLaMA and Qwen;
  • wherever smoothness matters: GELU has derivatives of every order, no kink at zero and no “dead” zone with zero gradient like ReLU.
The essentials in a minute

$\operatorname{GELU}(x)=x\,\Phi(x)$, where $\Phi$ is the cumulative distribution function of the standard normal law. Large positive $x$ pass almost entirely, large negative ones are almost zeroed out, and near zero there is a smooth transition. The function has a minimum of $\approx-0.170$ at $x\approx-0.752$, its derivative $\Phi(x)+x\varphi(x)$ stays between $-0.129$ and $1.129$, and in practice it is often computed with a tanh approximation whose error never exceeds $4.7\cdot10^{-4}$.

Intuition: the input times a probability

Imagine a gate that decides what share of the signal $x$ to let through. ReLU’s gate is hard: for $x>0$ it is fully open, for $x<0$ it is shut. GELU’s gate is open by $\Phi(x)$ — a share between 0 and 1 that grows smoothly with $x$. A large positive signal passes almost entirely, a large negative one is almost extinguished, and near zero half of the signal gets through: $\Phi(0)=0.5$.

Drag the point. The grey bar is the whole input $x$, the blue one is the part that got through: its length is $x\cdot\Phi(x)$. The teal dashed line is the share $\Phi(x)$ itself.

Figure 1. GELU, the share $\Phi(x)$ and the derivative. At $x=1$ the share $\Phi(1)\approx0.841$ passes, so $\operatorname{GELU}(1)\approx0.841$; at $x=-1$ only $0.159$ passes, and the output is $-0.159$. The circle marks the minimum of the function.
What to notice

For negative $x$ the output is not zero: it is negative and small. As $x$ goes down from zero the output first decreases (the input grows in magnitude faster than the share falls), and from about $x=-0.75$ it rises back towards zero: the share $\Phi(x)$ dies out faster than $|x|$ grows. Hence a shallow “dip” — the main difference between the shapes of GELU and ReLU.

The formula, bone by bone

All the complexity of GELU is hidden in $\Phi$. Let us take it apart.

$$\operatorname{GELU}(x)=x\cdot\Phi(x),\qquad \Phi(x)=\int_{-\infty}^{x}\varphi(t)\,dt,\qquad \varphi(t)=\frac{1}{\sqrt{2\pi}}\,e^{-t^{2}/2}$$
$\varphi(t)$
the density of the standard normal distribution $N(0,1)$ — the Gaussian bell with its maximum $1/\sqrt{2\pi}\approx0.399$ at zero;
$\Phi(x)$
the cumulative distribution function: the area under the bell to the left of $x$, i.e. the probability $P(\varepsilon\le x)$ for $\varepsilon\sim N(0,1)$; it grows from 0 to 1, $\Phi(0)=\tfrac12$;
$x\cdot\Phi(x)$
the input multiplied by that probability.

The integral of a Gaussian cannot be expressed through elementary functions, so it has a special name — the error function, hence “Gaussian Error Linear Unit”:

$$\operatorname{erf}(z)=\frac{2}{\sqrt\pi}\int_0^{z}e^{-t^2}\,dt,\qquad \Phi(x)=\frac12\Bigl(1+\operatorname{erf}\frac{x}{\sqrt2}\Bigr)\quad\Longrightarrow\quad\operatorname{GELU}(x)=\frac{x}{2}\Bigl(1+\operatorname{erf}\frac{x}{\sqrt2}\Bigr)$$

The factor $1/\sqrt2$ converts the standard normal variable with density $e^{-t^2/2}$ to the kernel $e^{-t^2}$ for which erf is defined. The erf itself is odd and runs from $-1$ to $1$, so $\tfrac12(1+\operatorname{erf})$ runs from 0 to 1.

$x$$\Phi(x)$$\operatorname{GELU}(x)$$\operatorname{GELU}'(x)$$\operatorname{ReLU}(x)$
−30.0013−0.0040−0.01190
−20.0228−0.0455−0.08520
−10.1587−0.1587−0.08330
−0.50.3085−0.15430.13250
00.50000.00000.50000
0.50.69150.34570.86750.5
10.84130.84131.08331
20.97721.95451.08522
30.99872.99601.01193

The table shows three things. On the right GELU quickly catches up with ReLU: at $x=3$ the difference is only $0.004$. On the left the output is negative but small: the deepest value in the table is $-0.159$ at $x=-1$. The derivative at zero is $0.5$, and for $x>0.752$ (in the table, from $x=1$ on) it exceeds one: the function grows slightly faster than the line $y=x$ as it catches up with it.

Where it came from: a noisy mask on average

The authors of GELU combined two ideas. ReLU multiplies the input by a deterministic mask: $x\cdot\mathbb 1[x>0]$. Dropout multiplies the input by a random mask that does not depend on the input. What if the mask is random but does depend on the input: the larger $x$, the more likely it is to be passed?

$$m\sim\operatorname{Bernoulli}\bigl(\Phi(x)\bigr),\qquad \mathbb E\,[\,x\cdot m\,]=x\cdot P(m=1)=x\,\Phi(x)=\operatorname{GELU}(x)$$

Such a mask is easy to build: take noise $\varepsilon\sim N(0,1)$ and set $m=1$ if $\varepsilon\le x$. Then $P(m=1)=\Phi(x)$ by the definition of the distribution function. Why the normal law? The authors justify it this way: neuron inputs, especially after normalization (BatchNorm, LayerNorm), are distributed roughly normally, and $\Phi(x)$ answers a natural question: what share of typical inputs is smaller than this $x$.

Figure 2. On the left, the orange dots are the mean of $x\cdot m$ over $n$ random masks at 25 points from $-3$ to $3$; on the right, the noise $\varepsilon$ and the share $\Phi(x_0)$ for which the mask is open. Press “Play”: at $n=20$ the largest deviation from GELU is $0.146$, at $n=2000$ it is down to $0.0125$; the error of the mean shrinks as $1/\sqrt n$. At $x_0=0.5$ the share of open masks approaches $\Phi(0.5)\approx0.691$.
GELU itself is not random

The noisy mask only explains the shape. The network uses the deterministic mean $x\Phi(x)$: there is no randomness in training or inference (if noise is wanted, dropout is added separately).

Properties and shape

  • Range: $[-0.170,\,+\infty)$ — bounded below, unbounded above.
  • Smoothness: GELU is infinitely differentiable; $\operatorname{GELU}(0)=0$, $\operatorname{GELU}'(0)=\tfrac12$.
  • Non-monotonicity: the function decreases on $(-\infty,\,-0.752)$ and increases to the right; the minimum is $\approx-0.170$.
  • Asymptotes: $\operatorname{GELU}(x)-x\to0$ as $x\to+\infty$ and $\operatorname{GELU}(x)\to0$ as $x\to-\infty$, with the left tail dying out like a Gaussian: $x\Phi(x)\approx-\varphi(x)$, already $\operatorname{GELU}(-4)\approx-1.3\cdot10^{-4}$.
  • Identity: $\operatorname{GELU}(x)-\operatorname{GELU}(-x)=x$, because $\Phi(x)+\Phi(-x)=1$.
  • Convexity: only on the interval $(-\sqrt2,\,\sqrt2)$; outside it the function is concave (the sign of the second derivative is in the next section).

GELU = ReLU minus an even “dip”

The identity gives a handy form. For $x>0$: $x\Phi(x)=x-x\Phi(-x)$; for $x<0$: $x\Phi(x)=-|x|\Phi(-|x|)$. In both cases

$$\operatorname{GELU}(x)=\operatorname{ReLU}(x)-|x|\,\Phi(-|x|)$$

The correction $|x|\Phi(-|x|)$ is even: it is the same on both sides of zero, peaks at $0.170$ at $|x|=0.752$ and fades quickly. SiLU $x\,\sigma(x)$ has the same property, only its dip is wider and deeper: $0.278$ at $|x|=1.278$.

Figure 3. Top: GELU, SiLU and ReLU; bottom: the differences $\operatorname{ReLU}-f$. Hover over either plot. At $x=-1$: $\operatorname{GELU}=-0.159$, $\operatorname{SiLU}=-0.269$. The dots on the lower plot are the largest gaps from ReLU: $0.170$ for GELU and $0.278$ for SiLU; these are also the depths of their minima.
The link with the sigmoid

The distribution functions of the normal and logistic laws are very close: $\Phi(x)\approx\sigma(1.702\,x)$ with an error of at most $0.0095$. That is why GELU nearly coincides with $x\,\sigma(1.702x)$ — SiLU with a steeper sigmoid. More about $\sigma$ itself is on the sigmoid page.

The derivative: Φ(x) + x·φ(x)

The derivative is needed for backpropagation. Let us derive it and find the minimum of the function.

Deriving the first and second derivatives

The key fact: the derivative of a distribution function is the density, $\Phi'(x)=\varphi(x)$ (the fundamental theorem of calculus for an integral with a variable upper limit). By the product rule:

$$\operatorname{GELU}'(x)=(x)'\cdot\Phi(x)+x\cdot\Phi'(x)=\Phi(x)+x\,\varphi(x)$$

For the Gaussian, $\varphi'(x)=-x\,\varphi(x)$. Differentiating once more:

$$\operatorname{GELU}''(x)=\varphi(x)+\varphi(x)+x\cdot\bigl(-x\,\varphi(x)\bigr)=\varphi(x)\,\bigl(2-x^{2}\bigr)$$

The sign of $\operatorname{GELU}''$ is the sign of $2-x^2$: the function is convex on $(-\sqrt2,\,\sqrt2)$ and concave outside it, and the derivative $\operatorname{GELU}'$ has its extrema at $x=\pm\sqrt2$.

$$\operatorname{GELU}'(x)=\underbrace{\Phi(x)}_{\text{share}}+\underbrace{x\,\varphi(x)}_{\text{change of the share}}$$

The derivative has two terms. The first is the share $\Phi(x)$ itself: that is how the derivative would look if the gate did not depend on $x$. The second is a correction for the gate opening as $x$ grows; it is positive to the right of zero and negative to the left. Compare with ReLU, whose derivative is a 0/1 step.

Figure 4. The derivative of GELU (orange) and its two terms. The largest value is $\Phi(\sqrt2)+\sqrt2\,\varphi(\sqrt2)\approx1.129$, the smallest is $\approx-0.129$ at $x=-\sqrt2$ — the two add up to 1, since $\operatorname{GELU}'(x)+\operatorname{GELU}'(-x)=1$. The derivative vanishes at $x\approx-0.752$; at the starting point $x=-0.75$ it is already almost zero. Turn on the second derivative: its zeros are at $\pm\sqrt2$.

Where the minimum is

The minimum is where $\operatorname{GELU}'(x)=0$, i.e. $\Phi(x)=-x\,\varphi(x)$. This equation has no solution in elementary functions; Newton’s method $x_{k+1}=x_k-\operatorname{GELU}'(x_k)/\operatorname{GELU}''(x_k)$ from $x_0=-1$ gives $-0.6557$, $-0.7448$, $-0.75175$, $-0.751792$ — every iteration roughly doubles the number of correct digits.

$$x^{*}\approx-0.7518,\qquad \operatorname{GELU}(x^{*})=x^{*}\Phi(x^{*})=-(x^{*})^{2}\varphi(x^{*})\approx-0.1700$$

The second equality follows from the minimum condition $\Phi(x^*)=-x^*\varphi(x^*)$. At the minimum the share $\Phi(x^*)\approx0.226$ gets through.

A negative gradient on the left

For $x<-0.752$ the derivative is negative: if the input increases, the output decreases. ReLU gives exactly zero in this zone, while GELU gives a small but non-zero signal (no lower than $-0.129$). It fades like a Gaussian: $\operatorname{GELU}'(-3)\approx-0.012$, $\operatorname{GELU}'(-4)\approx-0.0005$.

Fast approximations: tanh and the sigmoid

Computing erf costs more than elementary functions, so the authors proposed two approximations for when speed matters more than exactness. The first one uses tanh:

$$\operatorname{GELU}(x)\approx\frac{x}{2}\Bigl(1+\tanh\Bigl[\sqrt{2/\pi}\,\bigl(x+0.044715\,x^{3}\bigr)\Bigr]\Bigr)$$

This is the formula in the original BERT and GPT-2 code (Hugging Face calls it gelu_new, PyTorch — approximate="tanh"). Its largest error is $4.7\cdot10^{-4}$ at $|x|\approx2.70$. The second approximation uses the sigmoid:

$$\operatorname{GELU}(x)\approx x\,\sigma(1.702\,x)$$

It is coarser: the error reaches $0.020$ at $|x|\approx2.27$. Both errors are even functions, because all three formulas satisfy the identity $f(x)-f(-x)=x$.

Where √(2/π) and 0.044715 come from

We need to approximate $\operatorname{erf}(z)$ with $\tanh(u)$. Expand both around zero: $\operatorname{erf}(z)=\frac{2}{\sqrt\pi}\bigl(z-\frac{z^3}{3}+\dots\bigr)$ and $\tanh u=u-\frac{u^3}{3}+\dots$. Take $u=\frac{2}{\sqrt\pi}(z+cz^3)$ and match the coefficients.

$$z^{1}:\ \ \frac{2}{\sqrt\pi}=\frac{2}{\sqrt\pi};\qquad z^{3}:\ \ \frac{2c}{\sqrt\pi}-\frac{8}{3\pi\sqrt\pi}=-\frac{2}{3\sqrt\pi}\ \Longrightarrow\ c=\frac{4}{3\pi}-\frac13$$

Substituting $z=x/\sqrt2$ gives $\sqrt{2/\pi}\,(x+c'x^3)$ with $c'=c/2=\frac{2}{3\pi}-\frac16\approx0.0455$. Matching the series guarantees accuracy only near zero, so the formula uses a coefficient fitted numerically over the whole axis: $0.044715$. A check shows that it is the minimax coefficient for $\Phi$ itself: with it the largest error of $\Phi(x)\approx\tfrac12\bigl(1+\tanh[\sqrt{2/\pi}(x+c'x^3)]\bigr)$ is as small as possible, $1.8\cdot10^{-4}$. For GELU itself the optimum is slightly different ($c'\approx0.04438$, error $3.9\cdot10^{-4}$), but the gap is small. With the Taylor value $0.0455$ the maximum error is $7.5\cdot10^{-4}$, with $0.044715$ it is $4.7\cdot10^{-4}$, and without the cubic term ($c'=0$) it is $0.034$.

Figure 5. Top: the exact GELU and the two approximations; bottom: the absolute error on a log scale (the dips are the points where the error changes sign). Move the coefficient $c$: at $c=0.044715$ the maximum error is $4.7\cdot10^{-4}$, at $c=0$ it is $0.034$, and at $c=0.04438$ only $3.9\cdot10^{-4}$. At $x=1$: the exact value is $0.8413$, the tanh approximation $0.8412$, the sigmoid one $0.8458$.
These are different functions

A model trained with the tanh approximation must use it at inference too. The difference is at most $4.7\cdot10^{-4}$ per neuron, but over hundreds of layers and thousands of neurons it produces a noticeable mismatch of logits when weights are converted, and it breaks bit-exact tests.

The x·Φ(x/s) family in 3D: from ReLU to a line

The noise in the mask need not have unit scale. If $\varepsilon\sim N(0,s^2)$, then $P(\varepsilon\le x)=\Phi(x/s)$, and we get a whole family of functions with a “temperature” $s$:

$$f(x,s)=x\,\Phi\Bigl(\frac{x}{s}\Bigr)=s\cdot\operatorname{GELU}\Bigl(\frac{x}{s}\Bigr)$$
  • $s\to0$: no noise, $\Phi(x/s)$ becomes a step, and $f\to\operatorname{ReLU}(x)$;
  • $s=1$: the usual GELU;
  • $s\to\infty$: the noise drowns the input, $\Phi(x/s)\to\tfrac12$, and $f\to x/2$ — a straight line, the nonlinearity disappears.

The second equality shows that all members of the family are similar: the minimum is at $x=-0.752\,s$ and equals $-0.170\,s$. On the surface this is the straight teal line along the bottom of the “ravine”.

Figure 6. The surface $f(x,s)=x\,\Phi(x/s)$ (you can rotate it) and the slice at the current $s$. The orange line is the slice, the teal one is the line of minima. At $s=1$ the minimum at $x=-0.752$ equals $-0.170$. Switch to the derivative $\operatorname{GELU}'(x/s)$: for small $s$ it becomes ReLU’s step, and as $s$ grows it stretches along the $x$ axis and gets ever flatter; in the limit $s\to\infty$ it is the constant $\tfrac12$.

In a network GELU is applied to a vector element-wise: $h=\operatorname{GELU}(W_1x+b_1)$. Each coordinate is its own “fold” along the hyperplane $w_j^\top x+b_j=0$, but unlike ReLU the fold is smooth, with a small negative dip on the side where ReLU gives zero.

GELU in training and in the transformer

A network with one hidden layer builds its answer as a sum of “bricks” $w_{2j}\,f(w_{1j}x+b_{1j})$. With ReLU every brick is a broken line with a single bend, and the sum is a broken line too. With GELU the bricks are smooth, so a smooth dependence can be described with fewer of them.

Figure 7. The same network of 4 neurons with identical initialization, trained by Adam with step $0.03$ on 40 points of the noisy curve $\sin1.5x+0.3x$. On the right, the mean squared error (MSE) on a log scale. In 2000 steps the GELU network brings the MSE from $7.06$ down to $0.0042$ — even slightly below the noise level of the data (the mean square of the added noise is $0.0051$) — while the ReLU network gets stuck at $0.0083$: four bends are not enough. The “Neurons” switch shows the individual bricks of either network.

Training uses the same derivative: the gradient arriving at a neuron is multiplied by $\operatorname{GELU}'(z)=\Phi(z)+z\varphi(z)$. Unlike ReLU, a neuron with negative $z$ keeps receiving a signal, so GELU networks suffer less from “dead” neurons.

The transformer’s feed-forward block

In BERT and GPT every attention layer is followed by a two-layer perceptron that processes each token on its own:

$$\operatorname{FFN}(h)=W_2\,\operatorname{GELU}(W_1h+b_1)+b_2,\qquad W_1\in\mathbb R^{4d\times d},\ \ W_2\in\mathbb R^{d\times4d}$$

In BERT-base $d=768$, so each token in each of the 12 layers needs $4d=3072$ GELU evaluations — almost 19 million evaluations per pass for a sequence of 512 tokens. That is why the speed of the activation itself matters, and the tanh approximation is often used instead of the exact erf. The FFN is preceded by LayerNorm, so at the start of training the inputs $W_1h+b_1$ are of order one — exactly the region where GELU has both the dip and the smooth transition, i.e. where it differs noticeably from ReLU.

How GELU became the standard

Hendrycks and Gimpel proposed GELU in 2016 and showed that on vision, speech and text tasks it is no worse than ReLU and ELU. It became widely known through GPT (2018) and BERT (2018), then GPT-2, GPT-3 and the Vision Transformer (2020). In 2020 Shazeer showed that the gated variants GeGLU and SwiGLU are better still, and newer LLMs mostly use those.

In code: exact, fast and stable

NumPy has no erf function, so it is taken from SciPy. To avoid losing precision at large negative $x$, compute the distribution function through the complementary error function: $\Phi(x)=\tfrac12\operatorname{erfc}(-x/\sqrt2)$. The formula $\tfrac12(1+\operatorname{erf})$ at $x=-10$ subtracts almost equal numbers and gives exactly 0, although the true value is $\operatorname{GELU}(-10)\approx-7.6\cdot10^{-23}$; this does not matter for the activation itself, but the same $\Phi$ is often needed in other formulas.

pythonimport numpy as np
from scipy.special import erfc        # NumPy itself has no erf

SQRT2 = np.sqrt(2.0)

def gelu(x):
    """Exact GELU: x·Φ(x) with Φ(x) = ½·erfc(−x/√2) — no subtraction of close numbers."""
    x = np.asarray(x, dtype=float)
    return x * 0.5 * erfc(-x / SQRT2)

def gelu_grad(x):
    """The derivative: Φ(x) + x·φ(x)."""
    x = np.asarray(x, dtype=float)
    cdf = 0.5 * erfc(-x / SQRT2)
    pdf = np.exp(-0.5 * x * x) / np.sqrt(2.0 * np.pi)
    return cdf + x * pdf

def gelu_tanh(x):
    # the approximation from the original BERT and GPT-2 code
    return 0.5 * x * (1.0 + np.tanh(np.sqrt(2.0 / np.pi) * (x + 0.044715 * x**3)))

np.set_printoptions(precision=5, suppress=True)
x = np.array([-3.0, -1.0, 0.0, 1.0, 3.0])
print(gelu(x))        # [-0.00405 -0.15866  0.       0.84134  2.99595]
print(gelu_grad(x))   # [-0.01195 -0.08332  0.5      1.08332  1.01195]
print(np.abs(gelu_tanh(x) - gelu(x)).max())   # ≈ 4.1e-04 (at x = ±3)
pythonimport torch
import torch.nn as nn
import torch.nn.functional as F

x = torch.tensor([-1.0, 0.0, 1.0], requires_grad=True)
y = F.gelu(x)                          # −0.1587, 0.0000, 0.8413
y.sum().backward()
print(x.grad)                          # −0.0833, 0.5000, 1.0833 = Φ(x) + x·φ(x)
print(F.gelu(x, approximate="tanh"))   # −0.1588, 0.0000, 0.8412

# A transformer FFN block (as in BERT and GPT): d → 4d → d
class FeedForward(nn.Module):
    def __init__(self, d_model=768, mult=4, p_drop=0.1):
        super().__init__()
        self.fc1 = nn.Linear(d_model, mult * d_model)
        self.act = nn.GELU()           # for GPT-2 weights: nn.GELU(approximate="tanh")
        self.fc2 = nn.Linear(mult * d_model, d_model)
        self.drop = nn.Dropout(p_drop)

    def forward(self, h):
        return self.drop(self.fc2(self.act(self.fc1(h))))
The rule

Pick the GELU variant the weights were trained with: nn.GELU() is exact, nn.GELU(approximate="tanh") is the approximation. For a new model there is almost no difference in quality; the tanh variant can be faster on older hardware.

Common mistakes

  1. Assuming GELU’s output is non-negative, as with ReLU. It can go down to $-0.170$; if a logarithm, a square root or a division follows, this has to be taken into account.
  2. Mixing the exact GELU and the tanh approximation between training and inference, or when converting a model (ONNX, TensorRT, another framework). The error is small, but it accumulates across layers.
  3. Confusing $x\,\sigma(1.702x)$ with GELU. It is a fast approximation with an error of up to $0.02$, essentially SiLU with $\beta=1.702$.
  4. Believing GELU fully cures vanishing gradients. As $x\to-\infty$ the derivative dies out like a Gaussian: already $\operatorname{GELU}'(-4)\approx-0.0005$. Normalizing the inputs is still necessary.
  5. Writing $\Phi$ as $\tfrac12(1+\operatorname{erf}(x))$ and forgetting the division by $\sqrt2$. That is the distribution function of $N(0,\tfrac12)$, i.e. $x\Phi(\sqrt2x)$ — a different curve.
  • ReLU: the limit of $x\,\Phi(x/s)$ as $s\to0$ and the “skeleton” of GELU: $\operatorname{GELU}=\operatorname{ReLU}-|x|\Phi(-|x|)$.
  • SiLU / Swish: the same “input × soft gate” scheme with the sigmoid instead of $\Phi$; $x\,\sigma(1.702x)\approx\operatorname{GELU}(x)$.
  • Sigmoid: $\sigma(1.702x)$ approximates $\Phi(x)$ with an error of at most $0.0095$ — the link between logit and probit models.
  • tanh: the basis of the most popular GELU approximation.
  • Leaky ReLU: another way to let a signal through for $x<0$ — a constant small slope instead of a smooth dip.
  • Adam and MSE: the optimizer and the loss of the simulation in Figure 7.

Summary

1) $\operatorname{GELU}(x)=x\,\Phi(x)$: the input multiplied by the probability $P(\varepsilon\le x)$ for $\varepsilon\sim N(0,1)$; it is the mean of a random mask that depends on the input. 2) $\operatorname{GELU}=\operatorname{ReLU}-|x|\Phi(-|x|)$: a smooth ReLU with a minimum of $-0.170$ at $x=-0.752$. 3) $\operatorname{GELU}'=\Phi+x\varphi\in[-0.129,\,1.129]$, $\operatorname{GELU}''=\varphi\,(2-x^2)$. 4) The tanh approximation with coefficient $0.044715$ is off by at most $4.7\cdot10^{-4}$, but it is a different function. 5) $x\,\Phi(x/s)$ connects ReLU ($s\to0$) and $x/2$ ($s\to\infty$). 6) It sits in the FFN blocks of BERT, GPT and ViT.

Check yourself

1. What is $\operatorname{GELU}(-1)$ approximately?
2. What is the derivative of GELU at zero?
3. Where is the minimum of GELU?
4. What does $\Phi(x)$ mean in the GELU formula?
5. What is $\operatorname{GELU}(3)-\operatorname{GELU}(-3)$?