Activation Functions Explained: ReLU, Sigmoid, GELU, and the Math Behind Them
The real gradient numbers behind vanishing gradients, a ReLU neuron dying step by step, and why GELU quietly became the default in every modern LLM
An activation function is applied to a neuron's weighted input to introduce non-linearity, without which a deep network would collapse mathematically into a single linear model no matter how many layers it has. Sigmoid, tanh, and ReLU are the classic choices; GELU and SwiGLU are the modern defaults inside transformer-based models like BERT, GPT, and LLaMA.
Every deep learning explainer, including good ones, will tell you that sigmoid causes vanishing gradients and that ReLU can "die." Almost none of them show you the actual numbers. This guide does, using a from-scratch NumPy implementation I built that computes real gradient values and walks a real neuron through the exact steps that kill it.
Why Non-Linearity Matters
A single neuron computes a weighted sum of its inputs plus a bias, a purely linear operation. If every layer in a network only ever applied linear operations, the entire network, no matter how many layers deep, would be mathematically equivalent to one single linear layer, since a linear function of a linear function is still linear.
Activation functions break that collapse by applying a non-linear transformation after each layer's linear step. This is what allows a network with enough layers and neurons to approximate extremely complex, non-linear relationships between inputs and outputs, a result formalized as the universal approximation theorem [1]. The full layer-by-layer sequence of weighted sums and activations is covered end to end in Forward Propagation Explained.
Sigmoid, Tanh, ReLU, and Leaky ReLU
Vanishing Gradients, Shown With Real Numbers
Gradient descent updates a network's weights using the derivative of the activation function at each layer. Sigmoid and tanh both saturate, meaning their curves flatten out for large positive or negative inputs, and a flat curve has a derivative close to zero.
| Input (x) | Sigmoid gradient | Tanh gradient | ReLU gradient |
|---|---|---|---|
| -6 | 0.002467 | 0.000025 | 0.0 |
| 0 | 0.250000 | 1.000000 | 0.0 |
| 6 | 0.002467 | 0.000025 | 1.0 |
At x = 6, sigmoid's gradient has shrunk to 0.00246651, roughly 101 times smaller than its peak value of 0.25 at x = 0. Stack ten sigmoid layers where this kind of shrinkage compounds multiplicatively during backpropagation, and the gradient reaching the earliest layers can underflow toward numbers small enough that those layers effectively stop learning. That is the entire vanishing gradient problem, expressed as arithmetic rather than a warning label.
The Dying ReLU Problem, Caught in the Act
ReLU avoids saturation on the positive side, but it has its own failure mode. If a neuron's weighted input becomes negative for every single example in the training data, ReLU outputs zero and its gradient is exactly zero as well. A zero gradient means gradient descent has nothing to work with, so that neuron's weights never update again, for the rest of training.
| Step | Bias | Gradient | % of batch still activating |
|---|---|---|---|
| 1 | 0.500 | 3.000 | 100% |
| 2 | -1.000 | 3.000 | 0% |
| 3-6 | -1.000 | 0.000 | 0% |
This is a real gradient descent trace from a single neuron with an unlucky initialization. By step 2, every example in the batch produces a negative pre-activation, the neuron's output collapses to zero for all of them, and from step 3 onward the gradient locks at exactly 0.000. The bias never moves again. That neuron is now permanently dead for the rest of training, which is exactly why Leaky ReLU, ELU, and other variants with a non-zero negative slope exist.
GELU, Swish, and SwiGLU: What Modern Models Actually Use
Most explainers stop at ReLU, but almost no large language model built since 2018 actually uses it in its feedforward layers. The transformer-based models covered in the Transformers guide on this site rely on smoother, more recent activation functions.
GELU (Gaussian Error Linear Unit)
GELU weights an input by the probability that a standard normal random variable is less than that input, rather than applying ReLU's hard cutoff at zero. This produces a smooth curve that dips slightly negative just below zero instead of flattening abruptly, and that smoothness gives better gradient flow through the very deep stacks used in transformer architectures. GELU is the activation used in BERT, GPT, and T5 [2].
Swish
Swish, defined as x times sigmoid(x), was discovered through automated architecture search and shares GELU's smooth, non-monotonic shape. It performs comparably to GELU in most settings and is common in modern convolutional architectures.
SwiGLU
SwiGLU takes the idea further by gating: it applies Swish to one linear projection of the input and multiplies the result by a second, separate linear projection acting as a learned gate. This gated structure has become the feedforward activation of choice in several recent large language models, including LLaMA and PaLM, typically outperforming a plain GELU feedforward layer.
If you are reading model cards or architecture papers for current LLMs, seeing GELU or SwiGLU rather than plain ReLU in the feedforward block is the norm, not the exception. ReLU has not disappeared, it is still common in CNNs and simpler MLPs, but it is no longer the default for transformer-based architectures.
Sigmoid vs. Softmax
These two are frequently confused because both produce values that look like probabilities. Sigmoid treats every output independently, so a network with three sigmoid outputs might produce 0.9, 0.8, and 0.7, values that do not sum to anything meaningful. This makes sigmoid appropriate for binary classification or multi-label problems where an input can belong to more than one class at once.
Softmax, by contrast, produces a full probability distribution across all classes that always sums to exactly 1, since increasing the probability assigned to one class necessarily decreases the others. This makes softmax the standard choice for single-label multi-class classification output layers.
Which Activation Function Should You Use?
| Scenario | Recommended Activation |
|---|---|
| Hidden layers, standard MLP or CNN | ReLU (default), Leaky ReLU if dying neurons are observed |
| Transformer feedforward layers | GELU (solid default) or SwiGLU (often stronger, more complex) |
| Binary classification output | Sigmoid |
| Multi-class classification output | Softmax |
| Regression output | Linear (no activation) |
Building It From Scratch: What the Numbers Actually Show
Every reference on activation functions describes vanishing gradients and dying ReLU in words. Before writing this article, I implemented every function above and its exact derivative in NumPy, computed real gradient values across a range of inputs, and simulated an actual neuron dying step by step.
Six activation functions (Sigmoid, Tanh, ReLU, Leaky ReLU, GELU, Swish) and their exact derivatives, evaluated at real input values, plus a live simulation of a single neuron dying over six gradient descent steps.
The dying ReLU simulation was the most useful part to build. Watching the gradient column hit exactly 0.000 and stay there, rather than reading that a neuron "can get stuck," made the permanence of the failure mode concrete. There is no step 7 where it recovers. Once the batch produces zero activation everywhere, that neuron's contribution to the network is finished.
Conclusion: A Small Function With Large Consequences
Activation functions are a handful of lines of code, and yet the choice between them shapes whether a deep network trains at all. Sigmoid and tanh's saturation, ReLU's dead zone, and GELU's smoothness are not abstract trivia, they are the mechanical reasons certain architectures work and others stall during training. Seeing the actual numbers, rather than the qualitative description, is what makes that concrete.
Frequently Asked Questions
What is an activation function in a neural network?
An activation function is a mathematical function applied to a neuron's weighted input to decide what value it passes forward. It introduces non-linearity into the network, which is what allows a stack of layers to model complex, non-linear relationships instead of collapsing into a single linear function.
Why can't neural networks just use linear activation functions?
Stacking linear functions produces another linear function no matter how many layers are used, since a linear transformation of a linear transformation is still linear. Without a non-linear activation function, a deep network would have no more representational power than a single-layer linear model.
What is the dying ReLU problem?
The dying ReLU problem occurs when a neuron's weighted input becomes negative for every example in the training data, causing ReLU's output and gradient to be exactly zero. Since the gradient is zero, gradient descent can no longer update that neuron's weights, so it stays permanently inactive.
What is the vanishing gradient problem and which activation functions cause it?
The vanishing gradient problem happens when gradients shrink toward zero as they are propagated backward through many layers, preventing earlier layers from learning. Sigmoid and tanh are especially prone to this because their gradients approach zero for large positive or negative inputs, a property called saturation.
What is the difference between sigmoid and softmax?
Sigmoid produces an independent probability-like value between 0 and 1 for each output, typically used for binary or multi-label classification. Softmax produces a full probability distribution across multiple classes that always sums to 1, used for single-label multi-class classification.
What activation function do modern LLMs like GPT and BERT use?
GELU is used in BERT, GPT, and T5. Some newer large language models, including LLaMA and PaLM, use SwiGLU, a gated variant that combines a Swish activation with an additional learned gate for improved performance.
What is GELU and why does it outperform ReLU in transformers?
GELU is a smooth activation function based on the Gaussian cumulative distribution function that weights inputs probabilistically rather than applying a hard cutoff at zero like ReLU. Its smoothness produces better gradient flow in very deep transformer stacks, making it the practical default for BERT- and GPT-style architectures.
What is SwiGLU?
SwiGLU is a gated activation function used in models like LLaMA and PaLM. It applies Swish to one linear projection of the input and multiplies the result by a second linear projection acting as a learned gate, which often improves performance over a plain GELU feedforward layer.
Which activation function should I use?
ReLU remains a reasonable default for hidden layers in standard feedforward and convolutional networks. Use GELU for transformer-style architectures, sigmoid for binary classification output layers, and softmax for multi-class classification output layers.
References
- Wikipedia. Universal Approximation Theorem.
- Hendrycks, D. & Gimpel, K. (2016). Gaussian Error Linear Units (GELUs). arXiv.
- Shazeer, N. (2020). GLU Variants Improve Transformer. arXiv.
- GeeksforGeeks. Activation Functions in Neural Networks.
- Review Publically (2026). Transformers Explained: How Self-Attention Actually Works.
- Review Publically (2026). Activation Functions From Scratch, Built With NumPy (GitHub).
Khalid Hussain
Founder of Review Publically. MSc in Computer Science, Google Advanced Data Analytics certified. Hands-on with Python, Pandas, NumPy, Seaborn, and Scikit-learn, including building models from scratch to understand them beyond the library level.