Backpropagation › Gradient Flow Through Activation Functions

Michael BrenndoerferMay 6, 20258 min read
Language AI HandbookAll chapters
Part 10 of 67Neural Network Foundations
Chapter 65Backpropagation › Gradient Flow Through Activation Functions
Reading section 7 of 20 · Gradient Flow Through Activation Functions · 8 minSection 7 of 20 · 8 min

Book contents

Section 7 of 20 · 8 min read

Backpropagation › Gradient Flow Through Activation FunctionsOne section · Sections in this chapter7 / 20 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

The backward pass depends critically on the derivatives of activation functions. Different activations have very different gradient behaviors, which directly affects how well information flows backward through deep networks. We covered activation functions in depth in the Activation Functions chapter, so here we focus on their gradient properties specifically in the context of backpropagation.

Sigmoid Gradient

The sigmoid function σ(z)=11+e−z\sigma(z) = \frac{1}{1+e^{-z}} has derivative:

σ′(z)=σ(z)(1−σ(z))\sigma'(z) = \sigma(z)(1 - \sigma(z))

where:

  • σ(z)\sigma(z): the sigmoid output, a value in (0,1)(0, 1)
  • σ′(z)\sigma'(z): the derivative, which equals σ(z)(1−σ(z))\sigma(z)(1-\sigma(z))

The maximum value of σ′(z)\sigma'(z) is 0.250.25, achieved at z=0z = 0. For ∣z∣|z| large, the sigmoid saturates (output near 0 or 1), and the derivative approaches 0. This vanishing gradient problem is severe: each sigmoid layer multiplies the gradient by at most 0.25. In a 10-layer network, the gradient at the first layer could be multiplied by 0.2510≈10−60.25^{10} \approx 10^{-6} relative to the last layer.

There is another way to see why this is so problematic. The gradient of the loss with respect to the first layer's weights is computed as a product of gradients across all intermediate layers. If those gradients are small fractions (as they are for sigmoid in saturation), their product is tiny. This means the first-layer weights barely move during training. The network effectively cannot learn any useful feature representation in its early layers when trained with sigmoid activations in deep networks.

Tanh Gradient

The hyperbolic tangent tanh⁡(z)=ez−e−zez+e−z\tanh(z) = \frac{e^z - e^{-z}}{e^z + e^{-z}} has derivative:

tanh⁡′(z)=1−tanh⁡2(z)\tanh'(z) = 1 - \tanh^2(z)

This has maximum value 1.0 at z=0z = 0 and decays toward 0 as ∣z∣|z| grows. Tanh is better than sigmoid in that its maximum derivative is 1 (versus 0.25 for sigmoid), but it still saturates and can cause vanishing gradients in deep networks. The fact that tanh is zero-centered (outputs range from −1-1 to +1+1) makes it slightly easier to optimize than sigmoid in practice, since the gradients with respect to downstream weights are not all positive or all negative for a given input.

ReLU Gradient

The ReLU function ReLU(z)=max⁡(0,z)\text{ReLU}(z) = \max(0, z) has derivative:

ReLU′(z)={1if z>00if z≤0\text{ReLU}'(z) = \begin{cases} 1 & \text{if } z > 0 \\ 0 & \text{if } z \leq 0 \end{cases}

For positive inputs, ReLU passes the gradient through unchanged (gradient is 1). For negative inputs, ReLU blocks the gradient entirely (gradient is 0). This has two important consequences:

  1. No vanishing gradient for active neurons: When a neuron is active (output > 0), the gradient passes through without attenuation. A 100-layer ReLU network can potentially propagate gradients all the way back to the first layer without shrinkage, as long as enough neurons remain active.
  2. Dying ReLU problem: If a neuron consistently outputs negative values (typically due to a large negative bias or unfortunate weight initialization), it permanently stops learning because all gradients through it are zero. This can be detected during training by monitoring the fraction of neurons with zero activations, and mitigated with careful initialization or variants like Leaky ReLU.

The ReLU gradient's binary nature means it is not just non-vanishing for positive inputs: it also cannot explode. A gradient of 1 times 1 is still 1. This stability is a major practical advantage, and it explains why the shift from sigmoid to ReLU activations in the early 2010s dramatically improved the trainability of deep networks.

GELU Gradient

The GELU function (used in modern transformers) has a smooth, data-dependent gradient that avoids both sharp saturation and hard zeros. Its derivative involves the standard normal PDF and CDF, creating a smoother gradient landscape than ReLU. GELU's derivative is positive even for moderately negative inputs, which means neurons rarely die completely, though they do attenuate gradients in the negative regime.

Out[5]:
Visualization
Line plot of activation function values for sigmoid, tanh, ReLU, and GELU over [-4,4].
Activation function values for sigmoid, tanh, ReLU, and GELU over the range [-4, 4]. ReLU is piecewise linear and grows without bound for positive inputs, while sigmoid and tanh saturate as inputs move away from zero.
Line plot of derivatives of sigmoid, tanh, ReLU, and GELU showing how they compare for gradient flow.
Activation function values for sigmoid, tanh, ReLU, and GELU over the range [-4, 4]. ReLU is piecewise linear and grows without bound for positive inputs, while sigmoid and tanh saturate as inputs move away from zero.

The derivative plots reveal a key asymmetry. Sigmoid and tanh derivatives peak at 0.25 and 1.0 respectively, then decay toward zero as inputs grow large. ReLU's derivative is a step function: exactly 1 for positive inputs, exactly 0 for negative. GELU interpolates smoothly between the two extremes, with a small but nonzero gradient even for moderately negative inputs. In deep networks, these gradient magnitudes multiply together across layers, so choosing ReLU or GELU over sigmoid was a major practical advance in training deep networks.

To make the vanishing gradient problem concrete, consider a network with 20 layers. The gradient at the first layer has been multiplied by the activation derivative at each intermediate layer. If the average derivative is less than 1 (sigmoid at 0.25, tanh at up to 1 but below 1 for most inputs), the gradient shrinks exponentially with depth:

Out[6]:
Visualization
Log-scale line plot of gradient magnitude per layer for sigmoid and ReLU activations across 20 layers.
Log-scale gradient magnitude at each layer for a 20-layer network using sigmoid vs ReLU activations, starting from a unit gradient at the output. Sigmoid gradients decay exponentially toward the input, reaching near-zero magnitudes by layer 10. ReLU gradients also shrink in this random network, but retain many orders of magnitude more signal through all 20 layers.
Log-scale line plot of gradient magnitude ratio vs network depth for sigmoid and ReLU up to 50 layers.
Log-scale gradient magnitude at each layer for a 20-layer network using sigmoid vs ReLU activations, starting from a unit gradient at the output. Sigmoid gradients decay exponentially toward the input, reaching near-zero magnitudes by layer 10. ReLU gradients also shrink in this random network, but retain many orders of magnitude more signal through all 20 layers.

The left plot confirms the textbook result: sigmoid gradients decay exponentially as they travel toward earlier layers, while ReLU gradients remain comparatively stable. The right plot shows how rapidly this ratio grows with depth for sigmoid, reaching many orders of magnitude even for moderate-depth networks.