In Part 2 of this series, our three-step classifier (Linear(20, 64) -> ReLU -> Linear(64, 10)) held 1,994 trainable parameters, and every single one of them lived inside the two Linear layers. nn.ReLU() added zero weights and biases.
Yet deleting that zero-parameter layer collapses the entire stack. Two linear layers composed without a nonlinear activation fold algebraically into a single flat affine step (W2(W1 x + b1) + b2 = (W2 W1) x + (W2 b1 + b2) = W_eff x + b_eff), leaving a multi-layer network no more expressive than one neuron's straight boundary from Part 1. Even once you commit to adding an activation function, though, which curve you pick decides whether a deep network actually learns or stalls out.
For decades, neural networks defaulted to a smooth S-shaped curve called the sigmoid. Modern networks almost universally default to the sharp-cornered ReLU in hidden layers instead. Understanding that shift comes down to one physical quantity: the slope of each activation curve.
What an activation does to learning signals
A neuron begins by computing an affine score z = w^T x + b (the dot product of its weight row w and input vector x, shifted by bias b), then passes that raw number through an element-wise activation function f(z). Think of f as doing two coupled jobs. Forward, it bends straight weighted sums so stacked layers can carve curved boundaries. Backward during training, its curve governs how much sensitivity passes back to earlier weights.
To see why without getting ahead of full backpropagation calculus, follow the question training must answer right after checking the final prediction error (loss): if an early weight nudges slightly, how much does that final loss change? By the chain rule of calculus, local sensitivities multiply step by step backward from the output error toward Layer 1.
Every time that backward learning signal (gradient) crosses a neuron's activation f(z), it gets multiplied by f'(z), the local tangent slope (rise / run) of f at that neuron's current input z. Check the concrete arithmetic: if nudging z by +0.01 lifts f(z) by +0.0025, then f'(z) = 0.0025 / 0.01 = 0.25, so only a quarter of that sensitivity continues backward to earlier weights. When a curve goes flat (f'(z) ≈ 0), a neuron squeezes almost the entire learning signal down to nothing.
Sigmoid and why smooth curves saturate
The logistic sigmoid takes any real pre-activation z from -infinity to +infinity and squeezes it smoothly into the open interval (0, 1):
Read the behavior directly from e^(-z) (where e ≈ 2.718). Because an exponential of any real number is strictly positive, the denominator 1 + e^(-z) is always strictly greater than 1, trapping output values neatly inside (0, 1). Testing three values maps out the S-shape immediately:
- At
z = 0:e^0 = 1, sosigma(0) = 1 / (1 + 1) = 0.500right in the center. - At
z = +5:e^(-5) ≈ 0.00674, so the denominator sits near1.0067andsigma(5) ≈ 0.99331, hugging the top ceiling1. - At
z = -5:e^(+5) ≈ 148.41, inflating the denominator sosigma(-5) = 1 / 149.41 ≈ 0.00669, flattening near0.
Now inspect sigmoid's derivative formula, sigma'(z) = sigma(z) * (1 - sigma(z)). Letting s = sigma(z) and completing the square shows s(1 - s) = s - s^2 = -(s^2 - s + 0.25) + 0.25 = 0.25 - (s - 0.5)^2. Because a squared real number can never go negative, sigmoid's slope can never exceed 0.25, reaching that peak only right at z = 0 (s = 0.5). Out at z = 5 or z = -5, the slope collapses to 0.99331 * 0.00669 ≈ 0.00665, less than seven thousandths.
Multiply those slopes through depth. Suppose a network stacks 4 hidden sigmoid layers whose neurons sit slightly away from zero around slope 0.1: multiplying 0.1 * 0.1 * 0.1 * 0.1 shrinks the signal to 0.0001 (0.01% remaining). Even in the theoretical best case where every single neuron across all 4 layers lands right at z = 0 (slope = 0.25), 0.25^4 ≈ 0.0039, leaving under four tenths of one percent for Layer 1 weights. That rapid shrinkage across depth is the start of the vanishing gradient problem.
Sigmoid carries a second subtle drawback inside hidden layers: every output is strictly positive (0 < sigma(z) < 1). On a training sample, each weight update feeding a downstream neuron multiplies the same scalar upstream error sensitivity by the positive hidden activations h_j > 0, forcing all weights in that neuron's row to nudge with the exact same sign (either all increase together or all decrease together). Whenever an ideal correction needs some weights to grow while others shrink, optimization has to stair-step through multi-step zig-zags instead of moving directly toward the solution.
Does that mean sigmoid is obsolete? Not at all. While it fell out of favor in hidden layers, sigmoid still shines at the output layer of a binary classifier whenever a single unbounded logit needs converting into a class probability between 0 and 1. Notice a practical PyTorch nuance here too: during training, we rarely place an explicit nn.Sigmoid() in front of binary cross-entropy loss. Instead, nn.BCEWithLogitsLoss() accepts the raw linear logit directly and fuses sigmoid plus logarithm arithmetic algebraically for cleaner numerical precision near 0 and 1.
Tanh centers outputs around zero
The hyperbolic tangent (tanh) solves sigmoid's positive-only bias while keeping a smooth S-shape:
Rewriting tanh(z) via 2 * sigma(2z) - 1 = 2 / (1 + e^(-2z)) - 1 = (1 - e^(-2z)) / (1 + e^(-2z)) and multiplying top and bottom by e^z explains both advantages in one glance. Subtracting 1 shifts the range from (0, 1) to (-1, 1) centered cleanly at tanh(0) = 0, so negative inputs emit negative activations (tanh(-1) ≈ -0.762, tanh(+1) ≈ +0.762) and break sigmoid's positive-only constraint. Meanwhile, differentiating 2 * sigma(2z) picks up a factor of 2 outside and 2 inside, quadrupling the center slope from 0.25 to 1.0 (1 - 0^2 = 1).
Why did tanh still fail to hold its crown across deep stacks? Because scaling an S-curve only delays saturation without removing it. At z = 1, tanh'(1) = 1 - 0.762^2 ≈ 0.420, and by z = 3, tanh(3) ≈ 0.99505, so its local slope 1 - 0.99505^2 drops to 0.00987, below one percent. Across 4 saturated layers at z = 3, 0.00987^4 shrinks past one part in a hundred million (~9.5e-9). As Xavier Glorot and Yoshua Bengio showed in 2010, saturating activations still starve deep layers of gradients whenever units drift outside the narrow center band.
ReLU eliminates positive saturation
The fix that transformed deep training turned out to be dramatically simpler than exponential S-curves: ReLU (Rectified Linear Unit) keeps a completely straight line for positive inputs (ReLU(1) = 1, ReLU(100) = 100) and clamps everything at or below zero flat to 0:
Check the obvious calculus question right at the sharp corner z = 0: on the left side the slope is 0, and on the right side the slope is 1, so a strict classical derivative does not exist at z = 0. Deep learning libraries handle this seamlessly by picking a fixed subgradient value at exact 0.0 (verified in PyTorch 2.9.1: nn.ReLU() returns 0.0 gradient at z = 0.0). Because continuous floating-point weighted sums rarely land on exact zero, that convention causes zero trouble in practice.
Replacing exponential curves with max(0, z) gave deep architectures three decisive advantages:
- Zero saturation for positive inputs: even at
z = 100,ReLU(100) = 100and its slope stays1.0. Multiply1across4active hidden layers (1 * 1 * 1 * 1 = 1) and100%of the backward learning signal passes through active paths unattenuated. - Cheap compute: evaluating sigmoid or tanh requires computing exponentials (
exp), whereasmax(0, z)is one fast threshold comparison per neuron. - Natural sparsity: clamping negative pre-activations directly to
0switches off inactive units (observed in Glorot, Bordes, and Bengio's 2011 study on rectifier networks), letting models build clean sparse representations rather than keeping every unit partially active.
ReLU solves positive saturation, yet introduces its own failure mode on the opposite side of zero: the dying ReLU problem. Suppose a large training update (triggered for instance by an aggressive learning rate) pushes a neuron's weights and bias negative enough that w^T x + b < 0 for every example in the dataset. Because ReLU(z) = 0 and ReLU'(z) = 0 for all z < 0, every backward learning signal crossing that unit multiplies by 0. With zero gradient flowing to its weights or bias, that neuron can never update itself back across zero and stops learning permanently.
Leaky ReLU and the slope trade-off
Leaky ReLU fixes the dead negative zone with a small tweak: instead of clamping z <= 0 completely flat at 0, leave a shallow nonzero negative slope alpha (typically 0.01):
Test z = -5 with the standard alpha = 0.01: instead of returning 0 with 0 slope, Leaky ReLU emits 0.01 * (-5) = -0.05 and carries slope 0.01 backward. That tiny trickle lets weights recover even if a neuron temporarily drifts negative. Yet choosing alpha reflects a direct trade-off. Make alpha too small (0.0001), and recovery gradients become negligible like plain ReLU. Make alpha too large (toward 1.0), and both halves blend into a straight identity line (f(z) = z at alpha = 1), weakening the nonlinearity that separates layers.
Comparing all four curves and slopes side by side
Now that you have seen each curve individually, place all four activation functions and their derivatives onto shared axes to compare saturation, centering, and tail gradients directly (note alpha = 0.1 is exaggerated on these charts so Leaky ReLU's negative tilt stays distinct from ReLU's zero floor):
Leaky ReLU uses alpha = 0.1 for visibility (PyTorch default is 0.01)
Comparing sigmoid, tanh, ReLU, and Leaky ReLU (with alpha = 0.1 exaggerated to distinguish its negative slope from ReLU's flat zero floor). Click legend pills to compare pairs.
| pre-activation z | Sigmoid | Tanh | ReLU | Leaky ReLU (alpha = 0.1) |
|---|---|---|---|---|
| -3 | 0.047 | -0.995 | 0 | -0.3 |
| -2.5 | 0.076 | -0.987 | 0 | -0.25 |
| -2 | 0.119 | -0.964 | 0 | -0.2 |
| -1.5 | 0.182 | -0.905 | 0 | -0.15 |
| -1 | 0.269 | -0.762 | 0 | -0.1 |
| -0.5 | 0.378 | -0.462 | 0 | -0.05 |
| 0 | 0.5 | 0 | 0 | 0 |
| 0.5 | 0.622 | 0.462 | 0.5 | 0.5 |
| 1 | 0.731 | 0.762 | 1 | 1 |
| 1.5 | 0.818 | 0.905 | 1.5 | 1.5 |
| 2 | 0.881 | 0.964 | 2 | 2 |
| 2.5 | 0.924 | 0.987 | 2.5 | 2.5 |
| 3 | 0.953 | 0.995 | 3 | 3 |
PyTorch returns ReLU'(0) = 0.0, with Leaky ReLU alpha = 0.1 shown for visibility
Plotting derivatives directly shows why ReLU displaced sigmoid and tanh in hidden layers: sigmoid peaks at 0.25 and both S-curves drop near 0 by z = +-3, whereas ReLU stays flat at 1.0 across positive inputs.
| pre-activation z | Sigmoid slope | Tanh slope | ReLU slope | Leaky ReLU slope (alpha = 0.1) |
|---|---|---|---|---|
| -5 | 0.0066 | 0.0002 | 0 | 0.1 |
| -4.5 | 0.0109 | 0.0005 | 0 | 0.1 |
| -4 | 0.0177 | 0.0013 | 0 | 0.1 |
| -3.5 | 0.0285 | 0.0036 | 0 | 0.1 |
| -3 | 0.0452 | 0.0099 | 0 | 0.1 |
| -2.5 | 0.0701 | 0.0266 | 0 | 0.1 |
| -2 | 0.105 | 0.0707 | 0 | 0.1 |
| -1.5 | 0.1491 | 0.1807 | 0 | 0.1 |
| -1 | 0.1966 | 0.42 | 0 | 0.1 |
| -0.5 | 0.235 | 0.7864 | 0 | 0.1 |
| 0 | 0.25 | 1 | 0 | 0.1 |
| 0.5 | 0.235 | 0.7864 | 1 | 1 |
| 1 | 0.1966 | 0.42 | 1 | 1 |
| 1.5 | 0.1491 | 0.1807 | 1 | 1 |
| 2 | 0.105 | 0.0707 | 1 | 1 |
| 2.5 | 0.0701 | 0.0266 | 1 | 1 |
| 3 | 0.0452 | 0.0099 | 1 | 1 |
| 3.5 | 0.0285 | 0.0036 | 1 | 1 |
| 4 | 0.0177 | 0.0013 | 1 | 1 |
| 4.5 | 0.0109 | 0.0005 | 1 | 1 |
| 5 | 0.0066 | 0.0002 | 1 | 1 |
| Activation | Output range | Slope for z > 0 | Slope for z < 0 | Centering & trade-off |
|---|---|---|---|---|
| Sigmoid | (0, 1) | <= 0.25, approaches 0 | <= 0.25, approaches 0 | Positive-only outputs and two saturating tails shrink gradients |
| Tanh | (-1, 1) | <= 1, approaches 0 | <= 1, approaches 0 | Zero-centered, four times steeper at zero, still saturates in both tails |
| ReLU | [0, infinity) | 1 everywhere | 0 everywhere | Healthy positive gradients and fast math, but negative units can die |
| Leaky ReLU | (-infinity, infinity) | 1 everywhere | alpha (default 0.01) | Avoids dead ReLU, introduces negative slope hyperparameter alpha |
Swapping activations in PyTorch
In PyTorch, switching between these activations inside our series classifier takes a single line. Every element-wise module (nn.Sigmoid(), nn.Tanh(), nn.ReLU(), and nn.LeakyReLU(negative_slope=0.01)) stores zero parameters of its own, so swapping any of them into Linear(20, 64) -> Activation -> Linear(64, 10) leaves the model at the exact same 1,994 trainable parameters while changing its training behavior:
import torch.nn as nn# Hidden-layer activation options (one-line swap, 1,994 params for all four):for act in (nn.Sigmoid(),nn.Tanh(),nn.ReLU(),nn.LeakyReLU(negative_slope=0.01),):model = nn.Sequential(nn.Linear(20, 64),act,nn.Linear(64, 10),)print(f"{act.__class__.__name__:10s} -> {sum(p.numel() for p in model.parameters())} params")# For binary classification (1 output logit), skip explicit nn.Sigmoid() in the model:binary_model = nn.Sequential(nn.Linear(20, 64),nn.ReLU(),nn.Linear(64, 1),)criterion = nn.BCEWithLogitsLoss() # fuses sigmoid + cross-entropy numerically cleanly
You can probe exact activation outputs, tangent slopes, and multi-layer signal survival (slope ** depth) directly in Python below. Try changing z to 3.0, testing extreme negative inputs like -1000.0, or varying depth and alpha to see how quickly saturating slopes crush deep gradients while active ReLU holds at 1:
import mathdef sigmoid(z: float) -> float:if z >= 0:return 1.0 / (1.0 + math.exp(-z))ez = math.exp(z)return ez / (1.0 + ez)def inspect_activations(z: float, depth: int = 4, alpha: float = 0.01) -> None:s = sigmoid(z)ds = s * (1.0 - s)th = math.tanh(z)dth = 1.0 - th * thrl = max(0.0, z)drl = 1.0 if z > 0 else 0.0lrl = z if z > 0 else alpha * zdlrl = 1.0 if z > 0 else alphaprint(f"=== z = {z} across {depth} layers ===")rows = [("Sigmoid", s, ds),("Tanh", th, dth),("ReLU", rl, drl),(f"LeakyReLU(a={alpha})", lrl, dlrl),]for name, out, slope in rows:signal = slope ** depthprint(f"{name:18s} out={out:8.4f} slope={slope:7.4f} signal={signal:.4g}")for z in (0.0, 1.5, 3.0, 5.0, -5.0):inspect_activations(z)print()
Brain Link on YouTube
Why Neural Networks Need Activation Functions
Watch the full animated Brain Link episode covering tangent slopes, sigmoid saturation, 4-layer signal decay, tanh, ReLU, dying neurons, Leaky ReLU, and PyTorch swaps.
Notice the overarching pattern across all four functions: every activation fixes one failure mode while trading in another. Sigmoid gave smooth probability outputs, yet suffered from two-sided saturation and positive bias. Tanh centered activations at zero and quadrupled peak slope, yet still vanished in the tails. ReLU unlocked deep positive gradient highways at negligible compute cost, yet risked silencing negative neurons forever. In Part 4 of this series, we build on that foundation to derive smooth modern activations (ELU, GELU, SiLU, and Softplus), output Softmax, and how to choose the right activation by layer position.
References
- [1]Understanding the difficulty of training deep feedforward neural networks · Xavier Glorot and Yoshua Bengio, AISTATS (2010)Demonstrates how saturating sigmoid activations impede deep training signals compared to zero-centered and unsaturated alternatives.
- [2]Deep Sparse Rectifier Neural Networks · Xavier Glorot, Antoine Bordes, and Yoshua Bengio, AISTATS (2011)Examines ReLU sparsity, gradient propagation, and biological and computational advantages over sigmoid and tanh.
- [3]torch.nn.Sigmoid documentation · PyTorch documentationElement-wise logistic sigmoid function mapping inputs to (0, 1).
- [4]torch.nn.Tanh documentation · PyTorch documentationElement-wise hyperbolic tangent activation mapping inputs to (-1, 1).
- [5]torch.nn.ReLU documentation · PyTorch documentationApplies rectified linear unit max(0, x) element-wise.
- [6]torch.nn.LeakyReLU documentation · PyTorch documentationApplies max(0, x) + negative_slope * min(0, x) with negative_slope defaulting to 0.01.
- [7]torch.nn.BCEWithLogitsLoss documentation · PyTorch documentationCombines a Sigmoid layer and binary cross-entropy loss in one numerically stable module.
- [8]Why Neural Networks Need Activation Functions (video) · Brain Link on YouTube
- [9]Deep Learning Fundamentals playlist · Brain Link on YouTube







