Activation Functions and GELU
Activation Functions and GELU: The Bend That Makes Depth Work
Like my other notes, this is my own understanding written in plain words. For the full picture, take a proper deep learning course. Think of this as a friendly map, not the textbook.
Series: Neurons, Weights and Bias Explained → Neural Networks Explained → Activation Functions and GELU (this note) → Transformers Explained → Multi-Head Attention Explained.
In Neural Networks Explained we saw the punchline: without a bend after each layer, a hundred layers collapse into one. That bend is the activation function. It takes the neuron's raw sum
It looks like a tiny detail, but choosing it well changed what neural networks could do. Let me walk through the main ones, ending at GELU, the one inside GPT-style models.
What makes a good activation?
- Non-linear. Otherwise there is no point (see the collapse argument).
- Cheap to compute. It runs billions of times.
- Gives useful gradients. Training pushes the error signal back through
. If its slope is almost zero, learning stalls.
The four classics
Sigmoid
Squeezes any number into 0 to 1, which reads nicely as a probability. It was the default for decades.
Problem: for large positive or negative
Tanh
A sigmoid stretched to run from −1 to 1 and centered on zero, which helps. It still flattens out at both ends, so it still suffers from vanishing gradients. It lived on in older RNNs.
ReLU
Dead simple: negative becomes 0, positive passes straight through. Its slope is exactly 1 for positive inputs, so gradients flow freely, and it is very cheap. ReLU made training deep networks practical around 2012 and dominated for years.
Problem: a neuron whose input is always negative outputs 0 with slope 0 forever, so it never learns again. This is the dying ReLU. Variants like Leaky ReLU (
GELU: a smoother ReLU
GELU (Gaussian Error Linear Unit, Hendrycks and Gimpel, 2016) is the activation used in BERT, GPT-2 and GPT-3.
Here
- For a big positive
, , so the output is about . - For a big negative
, , so the output is about 0. - Near zero,
is in between, so it fades in smoothly instead of snapping.
Compare it with ReLU (the dashed line). ReLU has a sharp corner at zero. GELU rounds it off, and even dips slightly below zero (to about −0.17 at
Some numbers
| ReLU | GELU | Sigmoid | Tanh | |
|---|---|---|---|---|
| −2 | 0 | −0.045 | 0.119 | −0.964 |
| −1 | 0 | −0.159 | 0.269 | −0.762 |
| 0 | 0 | 0 | 0.5 | 0 |
| 1 | 1 | 0.841 | 0.731 | 0.762 |
| 2 | 2 | 1.955 | 0.881 | 0.964 |
At
In code
Exact version, and the fast approximation used in the original GPT-2 code:
import math
def gelu(x): # exact
return 0.5 * x * (1 + math.erf(x / math.sqrt(2)))
def gelu_tanh(x): # approximation
return 0.5 * x * (1 + math.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * x**3)))
for v in [-2, -1, 0, 1, 2]:
print(v, round(gelu(v), 3), round(gelu_tanh(v), 3))
The two agree to about three decimals. Deep learning libraries include both (torch.nn.GELU, with an option for the tanh version).
In the feed-forward part of every transformer block:
What newer models use
GELU is not the end of the story. Many recent large models use a relative called SiLU (also called Swish):
Same "input times a gate" idea, with a sigmoid as the gate. Combined with an extra learned gate (a GLU variant), it becomes SwiGLU, used in models like Llama and PaLM. So the modern trend is: smooth, gate-like bends.
A special one: Softmax
Softmax is not applied to one neuron. It takes a whole list of scores and turns them into probabilities that sum to 1:
It is used at the very end to pick one word out of a vocabulary, and inside attention to decide how much to listen to each word. We will meet it in Transformers Explained.
Who uses what
An activation function is the bend that stops layers from collapsing into one. Sigmoid and tanh squash but fade out; ReLU is simple but has a hard corner; GELU smooths that corner by multiplying the input by the chance it should pass. Small change, but it trains a little better, and it sits inside every GPT-style block.
Try it yourself
Next: Transformers Explained
Related: Neurons, Weights and Bias Explained, Neural Networks Explained