Search

Search papers and lessons

Vanishing Gradient Problem

Level 3 · 9 min read

In a deep network the learning signal is multiplied by one factor per layer, so if those factors are below 1 the early layers get almost no update and stop learning.

On this page

  • Colour = topic mix
  • Calculus
  • Neural network basics
  • Linear algebra
  • Probability
  • Losses & regularization
  • Optimizers
  • Sign in to see your progress

TL;DR

Training a deep network means sending a learning signal backwards from the loss, one layer at a time. At each layer the signal is multiplied by a local factor. If those factors are typically smaller than 1, the signal shrinks geometrically, so the early layers receive updates too small to matter and effectively stop learning.

This is the vanishing gradient problem. It is why deep networks were hard to train for a long time, and why ideas like better activations, careful initialisation, normalisation, and skip connections became standard.

Where you'll see it: in deep sigmoid networks that stall at the start of training, in recurrent networks that cannot remember what happened many steps ago, and in very deep plain stacks that get worse as you add layers.

Intuition

A whisper passed down a line

Picture a line of people passing a message backwards. Each person repeats it at half volume. The first person in line hears almost nothing by the time it reaches them. The message was not wrong; it just decayed on the way.

Backpropagation works the same way. The loss tells the last layer how wrong it was, and each layer passes a blame signal to the one before it, multiplied by how sensitive its own output was to its input. If each layer halves the blame, then after ten layers the first layer gets about one thousandth of it. Its weights barely move, while the last layers learn quickly. The network trains, but only its top few layers do real work.

flowchart RL
  L[Loss] --> A[Layer 10: large update]
  A -->|times factor below 1| B[Layer 9: smaller update]
  B -->|times factor below 1| C[Layer 8: smaller still]
  C -->|and so on| D[Layer 1: near zero update]

Why it appears with saturating activations

A sigmoid squashes any input into the range from 0 to 1. For large positive or negative inputs its curve is almost flat, and a flat curve has a slope near zero. The slope is the local factor that backpropagation multiplies in. Saturated units therefore pass almost no signal back, and the problem is worst exactly when units are confidently on or off.

The math

The chain rule through a deep network

Write a network as layers h1,…,hLh_1, \dots, h_L, where hℓ=f(Wℓhℓ−1)h_\ell = f(W_\ell h_{\ell-1}) and ff is an activation applied elementwise. The loss is L\mathcal{L}. The gradient of the loss with respect to an early weight W1W_1 is found with the chain rule:

∂L∂W1=∂L∂hL⋅∂hL∂hL−1⋯∂h2∂h1⋅∂h1∂W1.\frac{\partial \mathcal{L}}{\partial W_1} = \frac{\partial \mathcal{L}}{\partial h_L} \cdot \frac{\partial h_L}{\partial h_{L-1}} \cdots \frac{\partial h_2}{\partial h_1} \cdot \frac{\partial h_1}{\partial W_1}.

Each factor ∂hℓ+1∂hℓ\frac{\partial h_{\ell+1}}{\partial h_{\ell}} is a Jacobian matrix, whose entries are the derivatives of each output of layer ℓ+1\ell+1 with respect to each output of layer ℓ\ell. Ignoring the matrices for a moment and looking at a single unit, the product becomes

∂L∂h1≈∂L∂hL∏ℓ=1L−1wℓ f′(zℓ),\frac{\partial \mathcal{L}}{\partial h_1} \approx \frac{\partial \mathcal{L}}{\partial h_L} \prod_{\ell=1}^{L-1} w_\ell \, f'(z_\ell),

where zℓz_\ell is the pre-activation at layer ℓ\ell and wℓw_\ell is the weight that connects it to the next layer. The gradient is a product of L−1L-1 factors. If the typical size of each factor is rr, the gradient scales like rL−1r^{L-1}. That is exponential in depth.

Why the sigmoid makes rr small

The sigmoid is σ(z)=1/(1+e−z)\sigma(z) = 1/(1+e^{-z}). Its derivative is σ′(z)=σ(z)(1−σ(z))\sigma'(z) = \sigma(z)(1-\sigma(z)), which peaks at z=0z = 0 with value σ′(0)=0.25\sigma'(0) = 0.25, and falls towards 0 as ∣z∣|z| grows. So ∣σ′(z)∣≤0.25|\sigma'(z)| \le 0.25 everywhere. Even with a perfect weight of 1 in every layer, each layer multiplies the gradient by at most a quarter. The derivative of tanh⁡\tanh peaks at 1, which helps, but it still falls to zero for large ∣z∣|z|.

−6−4−2024600.20.40.60.81pre-activation zlocal gradient factor
  • sigmoid derivative
  • tanh derivative
The sigmoid's slope never exceeds 0.25 and vanishes for large |z|. The tanh slope peaks at 1 but also vanishes when units saturate.

The ReLU and the weight scale

The ReLU, relu(z)=max⁡(0,z)\mathrm{relu}(z) = \max(0, z), has derivative exactly 1 for positive inputs and 0 otherwise. For active units the factor is f′(zℓ)=1f'(z_\ell) = 1, so only the weights decide the scale. Then the gradient scales like ∏wℓ\prod w_\ell, which can still vanish if the weights are typically below 1 or explode if above 1. Good initialisation keeps the typical ww at a scale where the product stays near 1. Xavier and He initialisation set the variance of the weights from the layer width for this reason.

Recurrent networks are deep in time

A Recurrent Neural Network applies the same weight matrix WW at every time step tt. Unrolled over TT steps, a gradient from step TT back to step 11 contains a product of T−1T-1 Jacobians of the form diag(f′(zt))W\mathrm{diag}(f'(z_t)) W. If the spectral norm of these (the largest factor by which the matrix can stretch a vector) is below 1, the gradient vanishes over long spans. Bengio, Simard and Frasconi showed in 1994 that this makes it hard for plain gradient descent to learn dependencies that span many time steps.

Worked examples

Example
How fast a sigmoid stack loses signal

Suppose every layer's local factor is its best case, r=0.25r = 0.25, which means each unit sits at z=0z = 0 and the weights are 1.

  1. One layer: the gradient is multiplied by 0.250.25.
  2. Two layers: 0.252=0.06250.25^2 = 0.0625.
  3. Five layers: 0.255≈0.000980.25^5 \approx 0.00098, about one thousandth.
  4. Ten layers: 0.2510=2−20≈9.5×10−70.25^{10} = 2^{-20} \approx 9.5 \times 10^{-7}, about one millionth.

Interpretation: even in this ideal case, the first layer of a ten-layer sigmoid network gets a gradient about a million times smaller than the last layer's. Its weights move a million times more slowly, so for practical purposes it is frozen.

Example
Weight scale: vanishing versus exploding

Consider a deep linear chain with the same weight ww in every layer. After L=50L = 50 layers the gradient scales like w50w^{50}.

  1. Weight w=0.9w = 0.9: 0.950=e50ln⁡0.9=e−5.27≈0.00520.9^{50} = e^{50 \ln 0.9} = e^{-5.27} \approx 0.0052. The gradient is about 200 times smaller than at the top.
  2. Weight w=1.1w = 1.1: 1.150=e50ln⁡1.1=e4.77≈1171.1^{50} = e^{50 \ln 1.1} = e^{4.77} \approx 117. The gradient is about 117 times larger.
  3. Weight w=1.0w = 1.0: the product stays exactly 1, which is the goal of careful initialisation.

Interpretation: a 10 percent change in each weight changes the signal at depth 50 by a factor of about 23,000 (117/0.0052117 / 0.0052). This is why the scale of the weights matters so much in deep networks, and why normalisation layers exist.

Tradeoffs and failure modes

SymptomLikely causeRemedy
Loss flat from the first step; only top layers changeSaturated sigmoid or tanh units, or small weightsUse Rectified Linear Unit (ReLU) or a related activation; check initialisation
Training works for shallow nets and stalls as depth growsMultiplicative decay across layersAdd [[residual-connection
Early layers have tiny gradient norms, activations drift in scaleScale mismatch between layersBatch Normalization or Layer Normalization
Recurrent net forgets anything older than a few stepsLong products of Jacobians through timeGated cells such as Long Short-Term Memory
Gradients sometimes blow up, sometimes vanishWeight scale drifts above or below 1Careful initialisation, plus Gradient Norm Clipping for explosions

Two failure modes need care. First, measuring gradient norms is the quickest diagnosis: a layer whose gradient is 10^-6 of the top layer's is not learning, no matter what the loss curve does overall. Second, ReLU is not a cure on its own. Dead units output zero and pass no gradient at all, and a stack of ReLUs with badly scaled weights can still explode or vanish.

Skip connections are the most important modern fix. A residual block adds its input back to its output, so the derivative contains an identity term that does not shrink with depth. Gradients can reach early layers through the identity path even when the transformations in between are poor.

History

The problem was identified by Sepp Hochreiter in his 1991 diploma thesis, which analysed why error signals decay through time in recurrent networks. In 1994 Yoshua Bengio, Patrice Simard and Paolo Frasconi wrote a formal analysis showing that gradient descent struggles to learn long-term dependencies, and this became a standard reference.

Through the 1990s and 2000s deep networks were hard to train, and the problem was a big reason. Glorot and Bengio's 2010 paper on deep feedforward networks analysed how activations and gradients change scale across layers and proposed the initialisation that now bears Glorot's name. Glorot, Bordes and Bengio showed in 2011 that ReLU units made deep networks easier to train, and that caught on quickly.

Hochreiter and Schmidhuber's LSTM in 1997 was designed explicitly to solve this problem in recurrent networks, with a memory cell whose additive update lets gradients flow unchanged. In 2015 He and colleagues introduced residual networks, which won the ImageNet classification challenge that year and made very deep networks routine. Batch normalisation (2015) and He initialisation (2015) completed the toolkit that let depth scale.

Check yourself

QuestionIn one sentence, what does 'vanishing' mean in the vanishing gradient problem, and in which layers is it most damaging?Show answer

The gradient signal shrinks each time it passes back through a layer, so after many layers it is tiny. It is most damaging in the early layers, because they receive the smallest updates and are furthest from the loss.

QuestionWhy is the maximum of the sigmoid derivative 0.25, and what does that imply for a 10-layer stack with weights of 1?Show answer

σ′(z)=σ(z)(1−σ(z))\sigma'(z) = \sigma(z)(1-\sigma(z)) is largest at z=0z = 0, where σ(0)=0.5\sigma(0) = 0.5, giving 0.5×0.5=0.250.5 \times 0.5 = 0.25. Each layer multiplies the gradient by at most 0.25, so after ten layers the factor is at most 0.2510=2−20≈10−60.25^{10} = 2^{-20} \approx 10^{-6}.

QuestionA plain 50-layer network with weight 1.1 in every layer and one with weight 0.9 are both trained. Roughly how do their gradients at the first layer compare with the top layer?Show answer

With weight 1.1 the gradient is multiplied by 1.150≈1171.1^{50} \approx 117, so the first layer gets a larger signal, which can explode. With weight 0.9 it is multiplied by 0.950≈0.0050.9^{50} \approx 0.005, so the first layer gets about 1/200 of the signal. Both are bad. The fix is to keep the effective scale near 1, through initialisation and normalisation.

QuestionYou add residual connections to a very deep network and training starts working. Explain the mechanism in terms of the gradient.Show answer

A residual block computes h+F(h)h + F(h), so its derivative contains an identity term, I+∂F/∂hI + \partial F / \partial h. The identity part passes the gradient back unchanged, so even if ∂F/∂h\partial F / \partial h is small, the gradient reaching early layers is not multiplied down to near zero. The shortcut gives the signal a direct route to the early layers.

Sources