Concepts / GELU Activation Function

GELU Activation Function

ReLU and GELU are a good pair to study for this reason.

  • Programming

Two Curves That Look Alike

Two activation functions can look similar at a glance and still behave differently in ways that matter during training. ReLU and GELU are a good pair to study for this reason. Both rise for positive inputs and look broadly alike there, so the interesting part is what happens on the left side of the axis, where the inputs are negative.

Rather than starting with formulas, this article starts with a short script that evaluates both functions on the same evenly spaced inputs and draws their outputs side by side. Watching what each function does across a range of values, especially the negative ones, gives you a concrete picture to reason from before any vocabulary is introduced. Read the script once for its structure, then focus on what the two panels are meant to reveal.

What do you think happens?

Before running anything, consider a single negative input such as $x=-2$. What do you expect ReLU to return for it, and what do you expect GELU to return?

Reveal answer
Answer:

ReLU returns 0 for $x=-2$. GELU returns a small negative number, roughly -0.05, not zero.

ReLU passes positive inputs through unchanged and maps every negative input to zero. GELU keeps a small, nonzero output for negative inputs, so its curve dips slightly below the baseline rather than sitting flat on it.

Reading the Plotting Script

The script assumes that a GELU class and the PyTorch neural network module are already available, as they are set up earlier in the same chapter. It creates one GELU object and one ReLU object, generates 100 input values from -3 to 3, passes those inputs through both functions, and then plots each set of outputs in its own panel. Because both panels use the same input range, any difference you see between them is a difference in how each function transforms its inputs.

python
Notes on the code
  • gelu and relu are two separate activation objects, each applied to the same tensor of inputs
  • linspace(-3, 3, 100) produces 100 evenly spaced inputs covering the negative and positive sides
  • Each function is applied once to the full input tensor, so both outputs line up index by index with x
  • Two subplots place the GELU panel on the left and the ReLU panel on the right, sharing the same input range

When you run the script, look at the two panels together. The inputs on the horizontal axis are identical in both. The vertical axis shows the outputs. The only thing that changes between the panels is the function applied to the inputs, so the shape difference you see is entirely caused by that function.

Tracing ReLU Through Negative Inputs

ReLU outputs its input for positive values and zero for any negative input. It is a piecewise linear function, meaning it is built from straight-line pieces joined together. For positive inputs it passes the value through unchanged, and for negative inputs it returns zero. Those two rules meet at zero and form a sharp corner in the curve, which is the point where the function changes from one linear piece to the other.

Trace a few negative inputs through ReLU. For $x=-3$, the output is 0. For $x=-2$, the output is 0. For $x=-0.5$, the output is 0. No matter how negative the input is, the output stays fixed at zero, which is why the ReLU panel shows a flat line sitting on the baseline across the entire left half of the axis.

Where the Two Curves Diverge

Both curves rise for positive inputs and look broadly alike there. The separation happens on the negative side. Where the ReLU panel shows a flat line on zero, the GELU panel shows a curve that dips slightly below the baseline near the negative side before returning toward zero. The diagram below compares the two outputs at one negative input.

ReLU output0GELU outputabout -0.05Input x = -2negative inputInput x = -2negative input
For the same negative input, ReLU returns exactly zero while GELU returns a small negative value, so the GELU curve keeps a visible signal where the ReLU curve is flat.

How GELU Keeps a Negative Signal

GELU is a smooth, nonlinear function that approximates ReLU in its overall shape. Instead of flattening to zero for negative inputs, it keeps a small, nonzero output, so the curve bends gradually rather than cutting sharply at the origin. A negative input to a GELU unit still produces a signal, even though that signal is much weaker than a positive input of similar size.

One way to describe GELU is as the input multiplied by a gate. The gate is the Gaussian cumulative distribution function $\Phi(x)$, which takes values between 0 and 1. Positive inputs tend to receive a gate value close to 1, so they pass through almost unchanged. Strongly negative inputs receive a gate value close to 0, so they are suppressed heavily but not cut off by a hard threshold the way ReLU cuts them at zero.

Formula
Input xany real numberGate Φ(x)between 0 and 1x times Φ(x)gated inputGELU outputsmooth curvefeedsscaled by gatemultipliesproduces
GELU multiplies the input by a gate value between 0 and 1, so the gate softens the input smoothly instead of applying ReLU's hard threshold at zero.

Gradients for Negative Inputs

The gradient is the slope of the function at a given input. It determines how strongly a change in the input moves the output, and during training it is what carries the learning signal backward through a network. For ReLU, the slope on the negative side is zero everywhere, because the output is constant there. A neuron with a negative input under ReLU therefore passes no gradient backward at that step.

GELU behaves differently. Across almost all of its negative values it has a nonzero gradient, so a neuron with a negative input still takes part in learning. The source notes one specific exception: near $x \approx -0.75$, the gradient is zero. Outside that small neighborhood, the slope is nonzero across the negative range.

ReLU slopezero for all negative xGELU slopesmall nonzero for mostnegative xReLU slope near -0.75zeroGELU slope near -0.75zero
On the negative side, ReLU's slope is zero throughout, while GELU's slope is small and nonzero except in a small neighborhood near x of about -0.75.

Why Curve Shape Affects Training

The source ties the shape of each curve to how easily a network can be optimized. A smooth curve lets parameter adjustments respond in fine increments, which the source describes as more nuanced changes to the model's parameters. ReLU's sharp corner at zero can make optimization harder, and the source says this can be especially true in very deep networks or networks with complex architectures.

The corner matters because the slope changes abruptly there. Just to the left of zero, ReLU's slope is 0. Just to the right, it is 1. The derivative jumps at the point where the two linear pieces meet. GELU's curve passes through zero with no such jump, so its slope changes continuously, which is the property the source connects to easier optimization.

The second consequence concerns neurons that receive negative input. Under ReLU, those neurons output zero, so they contribute nothing to the learning signal at that step. Under GELU, a neuron with negative input still produces a small output and still takes part in learning, though to a lesser degree than a neuron with positive input. This is the reason the source gives for GELU keeping such neurons involved in training.

Mistakes Beginners Make With These Curves

  • Common mistake

    Assuming GELU outputs zero for every negative input, just like ReLU

    What it looks like

    Evaluating GELU at $x=-2$ and expecting exactly 0

    Why it fails

    GELU keeps a small nonzero output on the negative side, so the curve dips slightly below the baseline rather than lying flat on it.

    Do this instead

    Read the GELU panel for negative inputs as a small dip, and expect a small negative value rather than zero.

  • Common mistake

    Thinking that a negative input under GELU contributes nothing to learning

    Why it fails

    GELU has a nonzero gradient across almost all negative values, so those neurons still take part in training, though with less influence than positive inputs.

    Do this instead

    Treat a negative GELU input as a weak but live signal, and remember the exception near $x \approx -0.75$ where the gradient is zero.

  • Common mistake

    Assuming that a smooth curve automatically improves every network

    Why it fails

    The source links smoothness to optimization properties and describes its benefits, but it does not claim that smoothness guarantees better results in every case.

    Do this instead

    Describe the smoothness benefit as an optimization property, and check the specific model and training setup before drawing conclusions.

  • Common mistake

    Comparing the two panels with different input ranges, which hides the very difference the plot is meant to show

Practice Tracing the Curves

MEDIUM

For the negative inputs $x=-3$, $x=-0.75$, and $x=-0.5$, predict whether ReLU's output and gradient are zero, and whether GELU's gradient is zero or nonzero. Then explain which of these inputs would still send a learning signal backward under GELU but not under ReLU.

Hints
  • ReLU returns zero for every negative input, and its slope on the negative side is also zero.
  • The source identifies one exception for GELU's gradient, near $x \approx -0.75$, where the gradient is zero.
  • A nonzero GELU gradient at a negative input means that neuron still contributes to learning, which ReLU does not allow at negative inputs.

Key Takeaways

  • ReLU outputs its input for positive values and zero for any negative input, creating a sharp corner at zero where the function changes between two linear pieces.
  • GELU is a smooth, nonlinear function that approximates ReLU's overall shape but keeps a small, nonzero output for negative inputs.
  • ReLU has zero gradient across the negative side, so negative-input neurons contribute nothing to learning under ReLU; GELU keeps a nonzero gradient for almost all negative values, with an exception near $x \approx -0.75$.
  • The smooth shape of GELU at zero is linked to optimization properties, while ReLU's corner can make optimization harder, especially in very deep or complex networks.

Related concepts

More concepts starting with G