Introduction to the GELU (Gaussian Error Linear Unit) Activation Function

GELU is widely used in deep learning, particularly in large language models such as BERT and GPT. This article introduces the theory behind the GELU (Gaussian Error Linear Unit) activation function and its applications. It derives GELU and its derivative from a probabilistic perspective and examines its properties by comparing it with classic activation functions such as ReLU and ELU.

1. Introduction

Activation functions have played a crucial role in the development of deep learning. From early functions such as Sigmoid to the widely used ReLU, the choice of activation function directly affects a neural network’s performance. A newer activation function, GeLU (Gaussian Error Linear Unit), has demonstrated strong performance across several fields and is widely used in large language models such as BERT and GPT.

A Probabilistic Perspective

From a probabilistic perspective, GELU can be interpreted as a deterministic approximation to a stochastic regularization process. Consider two familiar methods: ReLU can be viewed as deterministically multiplying the input by either 0 or 1, while Dropout randomly sets the input to 0. Similarly, for an input x, the output of GELU can be viewed as the expected value of the following stochastic process:

\begin{equation}\text{GELU}(x) = x \cdot \mathbb{E}[m], \text{ where } m \sim \text{Bernoulli}(\Phi(x))\end{equation}

This interpretation gives GELU a clearer statistical meaning.

2. Mathematical Formulation of GELU

2.1 Basic Definition


The mathematical expression for GELU is:

\begin{equation}\text{GELU}(x) = x \cdot \Phi(x)\end{equation}

Here, \Phi(x) is the cumulative distribution function (CDF) of the standard normal distribution.

The probability density function of the standard normal distribution is:
\begin{equation}f(x)=\frac1{\sqrt{2\pi}}\mathrm{~exp}{\left(-\frac{x^2}2\right)}\end{equation}

The cumulative distribution function {P}\left(X\leq{x}\right) is therefore:

\begin{equation}\Phi(x)=F(x;0,1)=\frac1{\sqrt{2\pi}}\int_{-\infty}^x\exp\left(-\frac{t^2}2\right)dt.\end{equation}

We start with the definition of the error function erf:
\begin{equation}\mathrm{erf}(x)=\frac1{\sqrt{\pi}}\int_{-x}^xe^{-t^2}\mathrm{~d}t=\frac2{\sqrt{\pi}}\int_0^xe^{-t^2}\mathrm{~d}t.\end{equation}

The derivation proceeds as follows:
\begin{equation}\begin{aligned}\Phi(x)&=\frac{1}{\sqrt{2\pi}}\int_{-\infty}^{x}e^{-\frac{t^{2}}{2}}dt \\&=\frac{1}{\sqrt{2\pi}}\int_{-\infty}^{0}e^{-\frac{t^{2}}{2}}dt\quad+\quad\frac{1}{\sqrt{2\pi}}\int_{0}^{x}e^{-\frac{t^{2}}{2}}dt \\&\overset{z=\frac{t}{\sqrt{2}}}{\Longrightarrow } \frac{1}{2}+\frac{1}{\sqrt{2\pi}}\int_{0}^{x}e^{-z^{2}}\cdot\sqrt{2}\cdot dz \\&= \frac{1}{2}\left[1+\frac{2}{\sqrt{\pi}}\int_{0}^{x}e^{-z^{2}}\cdot dz \right] \\&=\frac{1}{2}\left[ 1+\operatorname{erf}(\frac{x}{\sqrt{2}}) \right]\end{aligned}\end{equation}

Thus, \Phi(x) can be expressed as:
\begin{equation}\Phi(z)=\frac{1}{2}\left[1+\mathrm{erf}{\left(\frac{z}{\sqrt{2}}\right)}\right]\end{equation}

Using these formulas, we can write GELU in its full form:

\begin{equation}\begin{align*}\text{GELU}\left(x\right) &= x{P}\left(X\leq{x}\right) \\&= x\frac1{\sqrt{2\pi}}\int_{-\infty}^x\exp\left(-\frac{t^2}2\right)dt \\&= x\Phi\left(x\right) \\&= x \cdot \frac{1}{2}\left[1 + \text{erf}(x/\sqrt{2})\right],\end{align*}\end{equation}

Unlike ReLU, which gates the input based on its sign (x\mathbf{1}_{x>0}), GELU weights the input x by its cumulative probability (that is, its percentile) in the standard normal distribution. This gives GELU a smoother activation curve than ReLU.

2.2 Approximations

Because GELU is relatively complex to compute exactly, two approximations are commonly used in practice:

Approximation 1 (based on the hyperbolic tangent, also known as the tanh approximation):

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

Approximation 2 (based on the Sigmoid function, also known as the sigmoid approximation):

\begin{equation}\text{GELU}(x) \approx x \cdot \sigma(1.702x)\end{equation}

Here, \sigma(x) is the sigmoid function.

2.3 Deriving the Derivative

Using the Chain Rule

To compute the derivative of GELU, we need the chain rule. First, expand the GELU function:

\begin{equation}\text{GELU}(x) = \frac{x}{2} + \frac{x}{2}\text{erf}\left(\frac{x}{\sqrt{2}}\right)\end{equation}

Differentiating this expression also requires the product rule:

\begin{equation}\frac{\mathrm{d}}{\mathrm{dx}}\text{GELU}(x) = \frac{\mathrm{d}}{\mathrm{dx}}\left(\frac{x}{2}\right) + \frac{\mathrm{d}}{\mathrm{dx}}\left(\frac{x}{2}\text{erf}\left(\frac{x}{\sqrt{2}}\right)\right)\end{equation}

Step-by-Step Derivation

The first term is straightforward:

\begin{equation}\frac{\mathrm{d}}{\mathrm{dx}}\left(\frac{x}{2}\right) = \frac{1}{2}\end{equation}

The second term requires the product rule:

\begin{equation}\frac{\mathrm{d}}{\mathrm{dx}}\left(\frac{x}{2}\text{erf}\left(\frac{x}{\sqrt{2}}\right)\right) = \frac{1}{2}\text{erf}\left(\frac{x}{\sqrt{2}}\right) + \frac{x}{2}\frac{\mathrm{d}}{\mathrm{dx}}\text{erf}\left(\frac{x}{\sqrt{2}}\right)\end{equation}

Derivative of the Error Function

The derivative of the error function is:

\begin{equation}\frac{\mathrm{d}}{\mathrm{dx}}\text{erf}(x) = \frac{2}{\sqrt{\pi}}e^{-x^2}\end{equation}

Applying the chain rule gives:

\begin{equation}\frac{\mathrm{d}}{\mathrm{dx}}\text{erf}\left(\frac{x}{\sqrt{2}}\right) = \frac{2}{\sqrt{\pi}}e^{-x^2/2} \cdot \frac{1}{\sqrt{2}} = \sqrt{\frac{2}{\pi}}e^{-x^2/2}\end{equation}

Combining the Results

Putting all the terms together gives:

\begin{equation}\begin{aligned}\frac{\mathrm{d}}{\mathrm{dx}}\text{GELU}(x) &= \frac{1}{2} + \frac{1}{2}\text{erf}\left(\frac{x}{\sqrt{2}}\right) + \frac{x}{2} \cdot \sqrt{\frac{2}{\pi}}e^{-x^2/2} \\&= \frac{1}{2}\left[1 + \text{erf}\left(\frac{x}{\sqrt{2}}\right)\right] + \frac{x}{\sqrt{2\pi}}e^{-x^2/2}\end{aligned}\end{equation}

An Alternative Form of the Derivative

Using the cumulative distribution function \Phi(x) and the probability density function \phi(x) of the standard normal distribution, we can write the derivative of GELU more concisely:

\begin{equation}\frac{\mathrm{d}}{\mathrm{dx}}\text{GELU}(x) = \Phi(x) + x\phi(x)\end{equation}

where:

\begin{equation}\begin{aligned}\Phi(x) &= \frac{1}{2}\left[1 + \text{erf}\left(\frac{x}{\sqrt{2}}\right)\right] \\\phi(x) &= \frac{1}{\sqrt{2\pi}}e^{-x^2/2}\end{aligned}\end{equation}

Properties of the Derivative

The derivative of GELU has the following important properties:

  • Continuous and Differentiable Everywhere
  • As x \to +\infty, the derivative approaches 1
  • As x \to -\infty, the derivative approaches 0
  • At x = 0, the derivative is 0.5

3. Comparing GELU with Other Activation Functions

3.1 Comparison with ReLU


The ReLU activation function is defined as follows:
\begin{equation}\operatorname{ReLU}(x)=\begin{cases}x &\quad \text{ if } x>0\\0 &\quad \text{ if } x\leq0\end{cases}\end{equation}

GELU can be viewed as a probabilistic generalization of ReLU:

  • When x \gg 0, \Phi(x) \approx 1, so \operatorname{GELU}(x) \approx x, as with ReLU
  • When x \ll 0, \Phi(x) \approx 0, so \operatorname{GELU}(x) \approx 0, as with ReLU

3.2 Comparison with ELU


The ELU activation function is defined as follows:
\begin{equation}\operatorname{ELU}(x)=\begin{cases}x &\quad \text{ if } x \geq 0 \\\alpha(e^x-1) &\quad \text{ if } x < 0\end{cases}\end{equation}

  • ELU can be viewed as a variant of xP(X \leq x), X \sim \operatorname{Cauchy}(0, 1) (Cauchy distribution) and is similar in form to GELU.
  • Like ELU, GELU provides a smooth transition near 0

References

  1. Gaussian Error Linear Units (GELUs)
  2. GeLU – Papers with Code
  3. PyTorch Documentation – torch.nn.GELU
  4. WIKI – Error Function
  5. Introduction to the GELU Activation Function and Notes
  6. [Deep Learning] GELU (Gaussian Error Linerar Units)

Add a Comment

Your email address will not be published. Required fields are marked *