Vanishing Gradients › The Gradient Product Problem

Michael BrenndoerferMay 8, 20253 min read
Language AI HandbookAll chapters
Part 11 of 67Recurrent Neural Networks
Chapter 76Vanishing Gradients › The Gradient Product Problem
Reading section 2 of 18 · The Gradient Product Problem · 3 minSection 2 of 18 · 3 min

Book contents

Section 2 of 18 · 3 min read

Vanishing Gradients › The Gradient Product ProblemOne section · Sections in this chapter2 / 18 ↓
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

To train an RNN, we need to compute how the loss at time TT depends on the hidden state at some earlier time tt. This is exactly the gradient ∂LT∂ht\frac{\partial L_T}{\partial h_t}, where LTL_T is the loss at the final timestep and hth_t is the hidden state at time tt.

Recall from the BPTT derivation that the hidden state evolves as:

ht=tanh⁡(Whht−1+Wxxt+b)h_t = \tanh(W_h h_{t-1} + W_x x_t + b)

where:

  • ht∈Rnh_t \in \mathbb{R}^n is the hidden state vector at time tt
  • Wh∈Rn×nW_h \in \mathbb{R}^{n \times n} is the recurrent weight matrix
  • Wx∈Rn×dW_x \in \mathbb{R}^{n \times d} maps input xtx_t to the hidden layer
  • b∈Rnb \in \mathbb{R}^n is the bias

Applying the chain rule, the gradient of hTh_T with respect to hth_t involves all intermediate steps:

∂hT∂ht=∏k=t+1T∂hk∂hk−1\frac{\partial h_T}{\partial h_t} = \prod_{k=t+1}^{T} \frac{\partial h_k}{\partial h_{k-1}}

where each factor in the product is the Jacobian matrix:

∂hk∂hk−1=diag(tanh⁡′(zk))⋅Wh\frac{\partial h_k}{\partial h_{k-1}} = \text{diag}(\tanh'(z_k)) \cdot W_h

where:

  • zk=Whhk−1+Wxxk+bz_k = W_h h_{k-1} + W_x x_k + b is the pre-activation at step kk
  • tanh⁡′(zk)\tanh'(z_k) is the element-wise derivative of tanh evaluated at zkz_k
  • diag(tanh⁡′(zk))\text{diag}(\tanh'(z_k)) is the diagonal Jacobian of the tanh nonlinearity

So the full gradient becomes:

∂hT∂ht=∏k=t+1Tdiag(tanh⁡′(zk))⋅Wh\frac{\partial h_T}{\partial h_t} = \prod_{k=t+1}^{T} \text{diag}(\tanh'(z_k)) \cdot W_h

This is a product of T−tT - t matrices. When T−tT - t is large, this product determines whether learning succeeds or fails.

To understand why this product is dangerous, think about what it represents physically. Each factor diag(tanh⁡′(zk))⋅Wh\text{diag}(\tanh'(z_k)) \cdot W_h is a local linear approximation to how perturbations to hk−1h_{k-1} propagate to hkh_k. Multiplying these local approximations together gives the global sensitivity: how much does a small change to hth_t affect hTh_T? The answer, for long sequences, is almost always: very little. The product of many factors smaller than one collapses to zero.

The Chain Rule Imposes a Product Structure

It is worth pausing to understand why the chain rule produces this particular structure and why there is no way around it within the RNN formulation. The chain rule is not a choice or an approximation: it is the exact rule for computing how composed functions depend on their inputs. The RNN is a composed function. hTh_T depends on hT−1h_{T-1}, which depends on hT−2h_{T-2}, all the way back to hth_t. The gradient of this chain of compositions is exactly the product of the local derivatives.

This means the vanishing gradient problem is not a bug in the BPTT algorithm or a consequence of a bad implementation. It is built into the structure of the RNN itself. The recurrence relation that makes RNNs powerful at processing sequences is also the source of the gradient problem. You cannot have sequential composition without a multiplicative gradient chain.

The only escape is to change the recurrence relation itself, which is precisely what LSTM does. Rather than composing a function at every timestep (multiplying), the LSTM adds to a memory cell. Addition distributes gradient flow uniformly backward, as we will see in the LSTM chapter.