RNN Architecture › Stacked RNNs

Michael BrenndoerferMay 7, 20252 min read
Language AI HandbookAll chapters
Part 11 of 67Recurrent Neural Networks
Chapter 74RNN Architecture › Stacked RNNs
Reading section 10 of 19 · Stacked RNNs · 2 minSection 10 of 19 · 2 min

Book contents

Section 10 of 19 · 2 min read

RNN Architecture › Stacked RNNsOne section · Sections in this chapter10 / 19 ↓
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

Just as feedforward networks gain representational power by stacking multiple layers, RNNs can be stacked by feeding the output hidden states of one RNN layer as the input to the next.

In a stacked (deep) RNN with LL layers, the hidden state at layer ℓ\ell is computed as:

ht(ℓ)=tanh⁡ ⁣(Whh(ℓ) ht−1(ℓ)+Wxh(ℓ) ht(ℓ−1)+bh(ℓ))\mathbf{h}_t^{(\ell)} = \tanh\!\left(\mathbf{W}_{hh}^{(\ell)}\,\mathbf{h}_{t-1}^{(\ell)} + \mathbf{W}_{xh}^{(\ell)}\,\mathbf{h}_t^{(\ell-1)} + \mathbf{b}_h^{(\ell)}\right)

where ht(0)=xt\mathbf{h}_t^{(0)} = \mathbf{x}_t (the input) and ht(ℓ−1)\mathbf{h}_t^{(\ell-1)} is the hidden state from the layer below at the same time step. Each layer has its own independent set of parameters.

The motivation for stacking is hierarchy. The lower layers tend to learn local, surface-level patterns (bigrams, punctuation boundaries, character-level morphology). The higher layers can then learn more abstract, long-range patterns (topic, discourse structure, semantic relationships) on top of these local summaries. This hierarchy of representations mirrors how deep feedforward networks learn a hierarchy of features in computer vision.

In practice, 2-layer stacked RNNs are common. 3 or 4 layers are used in more demanding tasks like machine translation. Beyond 4 layers, training becomes difficult due to compounding gradient instability across both depth and time. Techniques like layer normalization (applied within each RNN step) can help stabilize deeper stacked RNNs.

PyTorch's nn.RNN(num_layers=2) implements a 2-layer stacked RNN. The output tensor has shape (batch, seq_len, hidden_size) (from the final layer), and the h_n output has shape (num_layers, batch, hidden_size) containing the final hidden state of each layer.

Dropout in Stacked RNNs

Dropout in recurrent networks requires care. Applying standard dropout to the hidden state ht\mathbf{h}_t at every time step disrupts the information flow through time, which can hurt performance. The standard approach (Gal & Ghahramani, 2016) is variational dropout: use the same dropout mask for all time steps within a sequence, applied only to non-recurrent connections. This drops out connections between layers (from ht(ℓ−1)\mathbf{h}_t^{(\ell-1)} to ht(ℓ)\mathbf{h}_t^{(\ell)}) but not the recurrent connections (ht−1(ℓ)\mathbf{h}_{t-1}^{(\ell)} to ht(ℓ)\mathbf{h}_t^{(\ell)}). PyTorch implements this through nn.RNN(dropout=0.5, num_layers=2), which applies dropout between layers but not within the recurrent step.