Stacked RNNs › Gradient Flow in Deep RNNs

Michael BrenndoerferMay 13, 20253 min read
Language AI HandbookAll chapters
Part 11 of 67Recurrent Neural Networks
Chapter 82Stacked RNNs › Gradient Flow in Deep RNNs
Reading section 10 of 16 · Gradient Flow in Deep RNNs · 3 minSection 10 of 16 · 3 min

Book contents

Section 10 of 16 · 3 min read

Stacked RNNs › Gradient Flow in Deep RNNsOne section · Sections in this chapter10 / 16 ↓
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

One concern with stacking recurrent layers is gradient flow. In a stacked RNN, gradients must travel backward through both the depth dimension (across layers) and the time dimension (across timesteps). There are two distinct paths for gradient decay:

Across time (within a layer): For a vanilla RNN, this is the vanishing gradient problem covered in the Vanishing Gradients chapter. LSTMs solve this within a single layer via the cell state highway.

Across layers (at a single timestep): Even with LSTMs, adding more layers creates a new source of gradient attenuation. Each layer applies a tanh activation and a gating operation, and gradients multiplied through many layers can still shrink.

For a 4-layer stacked LSTM, the gradient for a parameter in layer 1 must pass through 3 additional layers before reaching the output. If each layer gate reduces the gradient by a factor of γ<1\gamma < 1, the effective gradient at layer 1 is γ3\gamma^3 times what it would be at layer 4. For deep stacks, this vertical gradient attenuation becomes the bottleneck.

Residual Connections for Deep Stacks

The standard solution in modern deep stacked RNNs is residual connections, also called skip connections. Originally developed for convolutional networks (ResNet), residual connections add the layer's input directly to its output:

ht(ℓ)=f ⁣(ht(ℓ−1))+ht(ℓ−1)h_t^{(\ell)} = f\!\left(h_t^{(\ell-1)}\right) + h_t^{(\ell-1)}

where f(⋅)f(\cdot) represents the full LSTM computation for layer ℓ\ell. The additive shortcut provides a direct gradient path: the derivative of the loss with respect to ht(ℓ−1)h_t^{(\ell-1)} includes both the gradient through ff and a direct gradient of 1.0. This prevents complete vanishing.

For residual connections to work, the output dimension of ff must match the input dimension. If layers have different sizes, a linear projection is added:

ht(ℓ)=f ⁣(ht(ℓ−1))+Wproj ht(ℓ−1)h_t^{(\ell)} = f\!\left(h_t^{(\ell-1)}\right) + W_{\text{proj}}\, h_t^{(\ell-1)}

Residual connections allowed much deeper stacking. The original Google Neural Machine Translation (GNMT) system from 2016 used 8 stacked LSTMs with residual connections in both the encoder and decoder.

Out[12]:
Visualization
Bar chart showing gradient norms at each layer with and without residual connections.
Gradient magnitude at each layer for a 4-layer stacked LSTM, comparing models with and without residual connections. Without residual connections, gradients shrink significantly in lower layers, reducing learning signal. Residual connections maintain more uniform gradient flow, enabling deeper models to train effectively.

The gradient difference between the output layer (Layer 4) and the deepest layer (Layer 1) is much more pronounced without residual connections. With residual connections, the lower layers still receive meaningful gradient signal, allowing the full depth of the network to participate in learning.