Theory ofIntelligence
Index
Deep Learning FoundationsBook Index: Letter A

Activation Functions in Neural Networks: The Engine of Non-Linearity

Why neural networks require non-linear activations, how stacking linear layers collapses without them, and in-depth comparisons between ReLU, Sigmoid, Tanh, Softmax, and GELU.

functionsMathematical Representation
f(z)=max⁡(0,z),σ(z)=11+e−z,Softmax(zi)=ezi∑ezj,GELU(z)=z⋅Φ(z)f(z) = \max(0, z), \quad \sigma(z) = \frac{1}{1 + e^{-z}}, \quad \text{Softmax}(z_i) = \frac{e^{z_i}}{\sum e^{z_j}}, \quad \text{GELU}(z) = z \cdot \Phi(z)
lightbulb

[!NOTE] Prerequisite Foundation
This guide builds directly upon The Structure of an Artificial Neuron. Before studying how activation functions work, recall that a neuron computes a weighted sum of its inputs plus a bias (z=wTx+bz = \mathbf{w}^T \mathbf{x} + b). The activation function is the mathematical gate that transforms that pre-activation value zz into the neuron's final output signal aa.

An activation function is a mathematical operator applied to the pre-activation sum of a neuron before broadcasting its output to downstream layers.

While it is easy to memorize formulas, there is a fundamental mathematical question at the core of deep learning: Why do we need activation functions at all? Without them, even a million-layer neural network collapses into a trivial linear model.


1. What a Neuron Calculates: From zz to aa

Recall the mathematical operation performed by a single artificial neuron:

Inputs (x) ────► [ Weights (w) ] ────► ( Weighted Sum Σ ) ──► ( + Bias b ) ──► [ z ] ──► [ Activation f(z) ] ──► Output (a)

Suppose a neuron receives two input signals:

x1=2,x2=3x_1 = 2, \quad x_2 = 3

The connection weights learned by the network are:

w1=0.5,w2=0.8w_1 = 0.5, \quad w_2 = 0.8

And the neuron possesses an internal bias parameter:

b=0.1b = 0.1

Step 1: Pre-Activation Accumulation (zz)

The neuron computes the dot product of its inputs and weights, then adds its bias offset:

z=x1w1+x2w2+bz = x_1 w_1 + x_2 w_2 + b

Substituting the numerical values:

z=(2)(0.5)+(3)(0.8)+0.1=1.0+2.4+0.1=3.5z = (2)(0.5) + (3)(0.8) + 0.1 = 1.0 + 2.4 + 0.1 = \mathbf{3.5}

This intermediate value zz is known as the pre-activation value (or logit).

Step 2: Non-Linear Transformation (aa)

Now, what happens to this value of 3.53.5? The neuron does not simply forward 3.53.5 as its output. Instead, it passes zz through an activation function ff:

a=f(z)\mathbf{a = f(z)}

Where:

  • zz = weighted sum before activation.
  • ff = activation function.
  • aa = final output (activation) passed to downstream neurons.

For example, if the network employs the Rectified Linear Unit (ReLU):

f(z)=max⁡(0,z)f(z) = \max(0, z)

Then:

a=max⁡(0,3.5)=3.5a = \max(0, 3.5) = \mathbf{3.5}

If the input had instead produced z=−2.4z = -2.4, ReLU would have clamped the output to max⁡(0,−2.4)=0.0\max(0, -2.4) = 0.0.


2. Why Do We Need Activation Functions? The Linear Collapse Theorem

This is the most critical conceptual foundation in all of machine learning.

Imagine we construct a deep neural network with 10 hidden layers, but we omit all activation functions. Each layer simply performs an affine linear transformation:

h1=W1x+b1\mathbf{h}_1 = \mathbf{W}_1 \mathbf{x} + \mathbf{b}_1 h2=W2h1+b2\mathbf{h}_2 = \mathbf{W}_2 \mathbf{h}_1 + \mathbf{b}_2 h3=W3h2+b3\mathbf{h}_3 = \mathbf{W}_3 \mathbf{h}_2 + \mathbf{b}_3

Let us substitute h1\mathbf{h}_1 and h2\mathbf{h}_2 directly into the output equation for a 3-layer model:

y=W3(W2(W1x+b1)+b2)+b3\mathbf{y} = \mathbf{W}_3(\mathbf{W}_2(\mathbf{W}_1 \mathbf{x} + \mathbf{b}_1) + \mathbf{b}_2) + \mathbf{b}_3

Expanding the matrix algebra:

y=(W3W2W1)x+(W3W2b1+W3b2+b3)\mathbf{y} = (\mathbf{W}_3 \mathbf{W}_2 \mathbf{W}_1)\mathbf{x} + (\mathbf{W}_3 \mathbf{W}_2 \mathbf{b}_1 + \mathbf{W}_3 \mathbf{b}_2 + \mathbf{b}_3)

Notice that the product of matrices (W3W2W1)(\mathbf{W}_3 \mathbf{W}_2 \mathbf{W}_1) is simply another single matrix W′\mathbf{W}', and the accumulated bias terms collapse into a single vector b′\mathbf{b}':

y=W′x+b′\mathbf{y} = \mathbf{W}' \mathbf{x} + \mathbf{b}'

The Linear Collapse Theorem

lightbulb

[!IMPORTANT] The Linear Collapse Theorem
Any linear combination of linear functions is always just another linear function.
Without non-linear activation functions, a network with 1,000 hidden layers has the exact same representational capacity as a 1-layer linear regression model. It can only draw straight lines or flat decision boundaries.

Activation functions introduce non-linearity. By bending, warping, and folding the feature space at every layer, activation functions allow deep neural networks to satisfy the Universal Approximation Theorem: with enough non-linear neurons, an ANN can approximate any continuous mathematical function.


3. Core Activation Functions Catalog

Comparison of Key Activation Functions

A. Rectified Linear Unit (ReLU)

ReLU is the undisputed workhorse of modern deep learning hidden layers.

f(x)=max⁡(0,x)\mathbf{f(x) = \max(0, x)}

Its operational rule is simple:

  • If the input is positive, return the input value directly.
  • If the input is zero or negative, return zero.
Concrete Numerical Examples:
f(5)=5,f(2)=2,f(−3)=0,f(0)=0f(5) = 5, \quad f(2) = 2, \quad f(-3) = 0, \quad f(0) = 0
Why is ReLU so popular?
  1. Computational Speed: It requires no exponentiation (exp⁡\exp) or division — only a single threshold comparison, which graphics cards (GPUs) compute in nanoseconds.
  2. Sustained Gradients: For any positive value (x>0x > 0), its derivative is exactly 1.01.0 (f′(x)=1f'(x) = 1). This completely prevents gradients from shrinking or vanishing when backpropagating through dozens of layers.
The "Dying ReLU" Limitation:

If a neuron's weights shift such that it receives negative inputs across the entire dataset (z<0z < 0), its output is permanently 00 and its gradient is permanently 00. The neuron becomes inactive ("dead") and cannot update its weights again.

lightbulb

[!TIP] Deep Dive on ReLU & Its Derivatives
For an in-depth breakdown of the piecewise subgradient at x=0x = 0, the "one-way valve" cat-ear detector, graph folding mechanics, and modern variants (Leaky, PReLU, ELU), explore our standalone guide: The Rectified Linear Unit (ReLU).


B. Sigmoid (Logistic Function)

Sigmoid squashes any real number from (−∞,+∞)(-\infty, +\infty) into a smooth probability interval between 00 and 11:

σ(x)=11+e−x\mathbf{\sigma(x) = \frac{1}{1 + e^{-x}}}
Concrete Numerical Examples:
σ(0)=11+e0=0.50\sigma(0) = \frac{1}{1 + e^{0}} = \mathbf{0.50} σ(2)=11+e−2≈0.881\sigma(2) = \frac{1}{1 + e^{-2}} \approx \mathbf{0.881} σ(−2)=11+e2≈0.119\sigma(-2) = \frac{1}{1 + e^{2}} \approx \mathbf{0.119}
Why is Sigmoid useful?

Because its outputs are bounded strictly in (0,1)(0, 1), they naturally represent probabilities. In binary classification models (e.g., medical diagnosis or spam filtering), the output neuron uses Sigmoid:

P(Spam∣x)=0.92(92% confidence)P(\text{Spam} \mid \mathbf{x}) = 0.92 \quad (92\% \text{ confidence})
Why Sigmoid is rarely used in modern hidden layers:
  • Vanishing Gradients: When ∣x∣>4|x| > 4, the curve becomes completely flat (saturated). Its derivative σ′(x)=σ(x)(1−σ(x))\sigma'(x) = \sigma(x)(1 - \sigma(x)) has a maximum value of only 0.250.25. Multiplying numbers ≤0.25\le 0.25 through 20 layers causes gradients to vanish to zero (0.2520≈9×10−130.25^{20} \approx 9 \times 10^{-13}).
lightbulb

[!TIP] Master Guide: The Sigmoid Function & The Steepness Parameter kk
For a rigorous walkthrough of the Sigmoid derivative identity, its application to spam classification, the fast-forward clock analogy for the steepness parameter kk, horizontal vs. vertical scaling, and how weights physically embody kk, explore our dedicated guide: The Sigmoid Activation Function.


C. Hyperbolic Tangent (Tanh)

Tanh squashes input signals into the range between −1-1 and +1+1:

tanh⁡(x)=ex−e−xex+e−x\mathbf{\tanh(x) = \frac{e^x - e^{-x}}{e^x + e^{-x}}}
Concrete Numerical Examples:
tanh⁡(0)=0.00\tanh(0) = \mathbf{0.00} tanh⁡(2)≈+0.964\tanh(2) \approx \mathbf{+0.964} tanh⁡(−2)≈−0.964\tanh(-2) \approx \mathbf{-0.964}
Why use Tanh over Sigmoid?
  • Zero-Centered: Unlike Sigmoid (which is centered at 0.50.5), Tanh has a mean output near 00. Zero-centered activations prevent gradient updates from oscillating in zig-zag directions during training.
  • Common Use: Extensively used in recurrent architectures (RNNs, LSTMs, GRUs) and state representations.

D. Softmax: Normalizing Vectors into Probabilities

Unlike ReLU, Sigmoid, or Tanh (which operate independently on single scalar numbers), Softmax operates across an entire vector of raw scores (logits).

Pi=ezi∑j=1Kezj\mathbf{P_i = \frac{e^{z_i}}{\sum_{j=1}^{K} e^{z_j}}}
Concrete Numerical Example:

Suppose a neural network produces a raw score vector (logits) across 3 candidate classes:

Z=[z1=2.0,z2=1.0,z3=0.1]\mathbf{Z} = [z_1 = 2.0, \quad z_2 = 1.0, \quad z_3 = 0.1]
  1. Exponentiate each score:

    • e2.0≈7.389e^{2.0} \approx 7.389
    • e1.0≈2.718e^{1.0} \approx 2.718
    • e0.1≈1.105e^{0.1} \approx 1.105
    • Sum of exponents: ∑ezj=7.389+2.718+1.105=11.212\sum e^{z_j} = 7.389 + 2.718 + 1.105 = 11.212
  2. Normalize by the sum:

    • P1=7.38911.212≈0.659(65.9%)P_1 = \frac{7.389}{11.212} \approx \mathbf{0.659} \quad (65.9\%)
    • P2=2.71811.212≈0.242(24.2%)P_2 = \frac{2.718}{11.212} \approx \mathbf{0.242} \quad (24.2\%)
    • P3=1.10511.212≈0.099(9.9%)P_3 = \frac{1.105}{11.212} \approx \mathbf{0.099} \quad (9.9\%)

Notice that all probabilities are strictly positive and sum exactly to 1.0 (0.659+0.242+0.099=1.0000.659 + 0.242 + 0.099 = 1.000).

Role in Large Language Models (LLMs):

In models like GPT-4, the final linear layer produces a vector of logits corresponding to every word in its vocabulary (~50,000 to 100,000 tokens). Softmax converts those scores into the probability distribution used to sample the next word:

Candidate TokenRaw Logit (ziz_i)Softmax Probability (PiP_i)
"apple"2.02.065.9%65.9\%
"orange"1.01.024.2%24.2\%
"banana"0.10.19.9%9.9\%

E. Gaussian Error Linear Unit (GELU)

GELU is the standard activation function powering modern Transformer architectures (BERT, GPT-2, GPT-3, GPT-4, LLaMA):

GELU⁡(x)=x⋅Φ(x)=x⋅P(X≤x)\mathbf{\operatorname{GELU}(x) = x \cdot \Phi(x) = x \cdot P(X \le x)}

Where Φ(x)\Phi(x) is the cumulative distribution function of the standard normal distribution N(0,1)\mathcal{N}(0, 1).

Unlike ReLU's abrupt sharp corner at x=0x = 0, GELU is smooth everywhere and probabilistically weights inputs by their value. Notice in the comparison plot that GELU dips slightly below zero (reaching ≈−0.17\approx -0.17 at x≈−0.75x \approx -0.75), providing curvature and smooth, non-zero gradient flow.


4. Interactive Simulation Laboratory

A. Interactive Activation & Local Derivative Plotter

Compare how input values map to outputs f(x)f(x) and local gradients f′(x)f'(x) across Standard ReLU, Leaky ReLU, and Sigmoid:

Activation Response & Local Derivative

Domain: [-5.0, 5.0]•f(-0.4) = -0.040•f'(-0.4) = 0.100
Cursor x = -0.4
-5.0-2.50.02.55.0-1.50.02.04.05.0
Activation f(x)
Derivative f'(x)
Evaluation Point (x)
Input x-0.4
-5.00.0+5.0
Leaky Slope α (Leaky ReLU)α = 0.10
0.01 (Gentle)0.10 (Standard)0.50 (Aggressive)
info
State Analysis:Leaky State (Slope α = 0.10)

Allows a slight negative signal through (-0.040). Preserves a constant non-zero derivative of 0.10 so dying neurons can recover.

B. Artificial Neuron Accumulation Laboratory

Adjust the input features x1,x2,x3x_1, x_2, x_3, modify weights w1,w2,w3w_1, w_2, w_3, alter the bias bb, and inspect how different activation functions fire in real time:

Interactive Artificial Neuron StudioLIVE VISUALIZER

Manipulate weights and bias to trace signal propagation: y = f(wᵀx + b)

Live Signal Flow SchematicFeed-Forward Propagation
w₁=+0.50 (→+1.0)w₂=+0.20 (→+0.8)w₃=+0.80 (→+2.4)x₁= 2x₂= 4x₃= 3Bias (b)+0.5∑Σ=4.20z=4.70z = 4.70ACTIVATIONRELUf(4.7)OUTPUT y4.70
Neuron Parameters & Synapse Weights
w₁Weight 1x₁ = 2
Contrib: +1.00+0.50
-2.0 (Inhibitory)w₁ · x₁ = +1.00+2.0 (Excitatory)
w₂Weight 2x₂ = 4
Contrib: +0.80+0.20
-2.0 (Inhibitory)w₂ · x₂ = +0.80+2.0 (Excitatory)
w₃Weight 3x₃ = 3
Contrib: +2.40+0.80
-2.0 (Inhibitory)w₃ · x₃ = +2.40+2.0 (Excitatory)
bNeuron Bias (b)Threshold Offset
+0.50
-5.0 (Inhibitory shift)Threshold Shift = +0.50+5.0 (Excitatory shift)
Non-Linear Activation Function f(z)Select Transfer Curve
Step-by-Step Mathematical CalculationFormula Trace
1. Weighted Evidence Sum (Σ wᵢ · xᵢ)
(2 × 0.50) + (4 × 0.20) + (3 × 0.80)= 4.20
2. Add Bias Offset (z = Σ + b)
4.20 + (+0.50)z = 4.70
3. Non-Linear Activation (y = RELU(z))
RELU(4.70)y = 4.7000
Conceptual Takeaway: The neuron is actively firing with output y = 4.70. In deep neural networks and Transformer FFNs, millions of these units compute simultaneously via matrix multiplication.

5. Comprehensive Comparison Matrix

Activation FunctionFormulaOutput RangeTypical Architecture PlacementKey AdvantagePrimary Limitation
ReLUmax⁡(0,x)\max(0, x)[0,+∞)[0, +\infty)Hidden layers (CNNs, MLPs)Fast to compute; no positive saturationCan suffer from "Dying ReLUs"
Sigmoid11+e−x\frac{1}{1 + e^{-x}}(0,1)(0, 1)Output layer (Binary Classification)Direct probability interpretationVanishing gradients in deep layers
Tanhex−e−xex+e−x\frac{e^x - e^{-x}}{e^x + e^{-x}}(−1,+1)(-1, +1)Hidden layers in RNNs / LSTMsZero-centered outputsSaturates at large magnitudes
Softmaxezi∑ezj\frac{e^{z_i}}{\sum e^{z_j}}(0,1),∑=1(0, 1), \sum = 1Output layer (Multi-class / LLMs)Valid probability distributionComputationally expensive over large vocabularies
GELUxΦ(x)x \Phi(x)≈[−0.17,+∞)\approx [-0.17, +\infty)Hidden layers in Transformers (GPT, BERT)Smooth curvature; non-zero negative gradientsRequires numerical approximation

6. Where Do Activation Functions Appear in GPT?

A common misconception is that Transformers only use self-attention. In reality, over two-thirds of the parameters in a Large Language Model reside in its Feed-Forward Networks (FFNs).

Inside every single Transformer block, signals pass through a 2-layer MLP:

Input Vector (x)
       │
       ▼
[ Linear Layer 1 ] ────► Computes: h = xW₁ + b₁ (projects d_model ──► 4 × d_model)
       │
       ▼
[ Activation Function ] ─► Applies GELU non-linearity: a = GELU(h)
       │
       ▼
[ Linear Layer 2 ] ────► Computes: y = aW₂ + b₂ (compresses 4 × d_model ──► d_model)
       │
       ▼
Output Vector (y)

Without the GELU activation function in the middle, W1\mathbf{W}_1 and W2\mathbf{W}_2 would collapse into a single matrix, eliminating the model's ability to act as a deep memory store.

At the very end of the model, after passing through all Transformer blocks, the output vector passes through a linear projection layer and a Softmax activation to predict the next word.


7. The Three Foundational Pillars of Deep Learning

To build complete mastery of neural network architectures, always keep these three elements clearly distinguished in your mental model:

  1. Weights and Biases (W,b\mathbf{W}, \mathbf{b}):
    The learnable dials of the network. They start randomized and are systematically adjusted through backpropagation and gradient descent to minimize loss.
  2. Linear Transformations (Wx+b\mathbf{W}\mathbf{x} + \mathbf{b}):
    The combining mechanism. High-speed linear algebra that aggregates feature signals via matrix multiplication.
  3. Activation Functions (f(z)f(z)):
    The curvature engine. Mathematical non-linear operators that warp the space, preventing linear collapse and enabling the representation of complex, non-linear relationships.

8. Test Your Understanding

<details className="not-prose my-4 rounded-2xl border border-slate-200 dark:border-neutral-800 bg-slate-50/50 dark:bg-neutral-900/40 p-5 shadow-2xs"> <summary className="font-bold text-slate-900 dark:text-slate-100 cursor-pointer flex items-center justify-between"> <span>Question 1: What is the main purpose of non-linear activation functions in deep neural networks?</span> <span className="text-xs font-mono font-normal text-teal-600 dark:text-teal-400">Click to reveal answer</span> </summary> <div className="mt-4 pt-4 border-t border-slate-200/80 dark:border-neutral-800 text-sm space-y-2"> <p className="font-semibold text-emerald-600 dark:text-emerald-400">Answer: To introduce non-linearity into the network, preventing linear collapse.</p> <p className="text-slate-600 dark:text-slate-400"> Without non-linear activations, stacking 100 linear layers collapses mathematically into a single linear transformation ($\mathbf{y} = \mathbf{W}'\mathbf{x} + \mathbf{b}'$). Non-linear activations allow networks to learn curved decision boundaries and model complex real-world data. </p> </div> </details> <details className="not-prose my-4 rounded-2xl border border-slate-200 dark:border-neutral-800 bg-slate-50/50 dark:bg-neutral-900/40 p-5 shadow-2xs"> <summary className="font-bold text-slate-900 dark:text-slate-100 cursor-pointer flex items-center justify-between"> <span>Question 2: What value does the standard ReLU function return for an input of $z = -4$?</span> <span className="text-xs font-mono font-normal text-teal-600 dark:text-teal-400">Click to reveal answer</span> </summary> <div className="mt-4 pt-4 border-t border-slate-200/80 dark:border-neutral-800 text-sm space-y-2"> <p className="font-semibold text-emerald-600 dark:text-emerald-400">Answer: 0.0</p> <p className="text-slate-600 dark:text-slate-400"> By definition, $\operatorname{ReLU}(z) = \max(0, z)$. Since $-4 \le 0$, the function returns $\max(0, -4) = 0.0$. </p> </div> </details> <details className="not-prose my-4 rounded-2xl border border-slate-200 dark:border-neutral-800 bg-slate-50/50 dark:bg-neutral-900/40 p-5 shadow-2xs"> <summary className="font-bold text-slate-900 dark:text-slate-100 cursor-pointer flex items-center justify-between"> <span>Question 3: Which activation function transforms an entire vector of raw logits into a valid probability distribution that sums to 1.0?</span> <span className="text-xs font-mono font-normal text-teal-600 dark:text-teal-400">Click to reveal answer</span> </summary> <div className="mt-4 pt-4 border-t border-slate-200/80 dark:border-neutral-800 text-sm space-y-2"> <p className="font-semibold text-emerald-600 dark:text-emerald-400">Answer: Softmax</p> <p className="text-slate-600 dark:text-slate-400"> Softmax exponentiates each logit $z_i$ and divides by the sum of all exponentiated logits ($\frac{e^{z_i}}{\sum e^{z_j}}$). This guarantees that every output is positive and the sum across all categories is strictly $1.0$, which is essential for multi-class classification and next-token generation in LLMs. </p> </div> </details>