All formulas

Machine learning building blocks

GELU (exact, erf form)

ml.geluAlso called gelu, gaussian error linear unit, gelu exact, gelu erf

GELU⁡(x)=x2(1+erf⁡(x2))\operatorname{GELU}(x) = \frac{x}{2}\left(1 + \operatorname{erf}\left(\frac{x}{\sqrt{2}}\right)\right)

x times the standard normal CDF Φ(x). This is the exact form, the default of torch.nn.GELU; the tanh approximation is a separate entry, ml.gelu-tanh.

Notation

xx

input

x ∈ ℝ

GELU⁡(x)\operatorname{GELU}(x)

Result: the activation

scalar

Worked examples

Each result is solved exactly and confirmed by two independent computations.

  1. Given

    x=0x = 0

    Result

    GELU⁡(x)=0\displaystyle \operatorname{GELU}(x) = 0
  2. Given

    x=1x = 1

    Result

    GELU⁡(x)=erf⁡(22)2+12\displaystyle \operatorname{GELU}(x) = \frac{\operatorname{erf}{\left(\frac{\sqrt{2}}{2} \right)}}{2} + \frac{1}{2}

    ≈ 0.841344746069

  3. Given

    x=−1x = -1

    Result

    GELU⁡(x)=−12+erf⁡(22)2\displaystyle \operatorname{GELU}(x) = - \frac{1}{2} + \frac{\operatorname{erf}{\left(\frac{\sqrt{2}}{2} \right)}}{2}

    ≈ -0.158655253931

  4. Given

    x=2x = 2

    Result

    GELU⁡(x)=erf⁡(2)+1\displaystyle \operatorname{GELU}(x) = \operatorname{erf}{\left(\sqrt{2} \right)} + 1

    ≈ 1.9544997361

Common mistakes

  • Uses the tanh approximation

    The result is x/2 (1 + tanh(√(2/π)(x + 0.044715x3))), which differs from the exact form by up to about 5e-4.

    Fix: Use the erf form, or torch.nn.GELU() with its default approximate='none'.

  • Uses the sigmoid approximation x σ(1.702x)

    The result is the cheaper sigmoid approximation, which differs from the exact form by up to about 2e-2.

    Fix: Use the erf form.

Source

  • Hendrycks and Gimpel 2016

    Hendrycks, Dan; Gimpel, Kevin. Gaussian Error Linear Units (GELUs). arXiv:1606.08415, 2016.

    arxiv.org