Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Layers of memory
A single recurrent layer turns a sequence of inputs into a sequence of states. A stacked RNN treats that state sequence as the input of a second recurrent layer, and so on. Lower layers can follow fast details (sounds, letters) while higher layers track slower structure (words, phrases). Depth costs training difficulty and does not always help, as the research exercise shows. The demo shows the gradient problem each layer must also cope with.
Deep LSTM stacks drove the speech and translation systems of the 2010s. Knowing when extra layers pay off avoids wasting compute on embedded sequence models.
- 1. In a stacked RNN, layer 2 reads…
- 2. Higher layers often track…
- 3. More layers always help?
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Stacking: h⁽ˡ⁾ₜ = f(h⁽ˡ⁻¹⁾ₜ, h⁽ˡ⁾ₜ₋₁) with h⁽⁰⁾ₜ = xₜ. Each layer has its own weights.
- Parameters:
- a tanh layer from width d to h has dh + h² + h;
- for input 1: one 16-unit layer has 288, two 9-unit layers 270, three 7-unit layers 273, so the comparison in exercise 2 is roughly fair;
- a 2-layer PyTorch LSTM (d = 10, n = 32) has 5632 + 8448 = 14,080.
- Paths: a path from x₁ to the top output at step T passes through T + L − 1 hidden states, so depth adds little to temporal path length.
- Other ways to be deep (Pascanu et al., 2014):
- a deep transition (an MLP between hₜ₋₁ and hₜ);
- a deep output (an MLP after hₜ);
- stacking (deep across layers).
- Parallel execution: one cell at a time takes T·L slots. A wavefront schedule, where layer l processes step t while layer l + 1 processes step t − 1, takes T + L − 1 (103 instead of 400 for T = 100, L = 4).
- 1. Parameters of one 16-unit tanh layer on scalar input:
- 2. Hidden states on a path from x₁ to the top output at T = 100, L = 3:
- 3. Wavefront slots for T = 100, L = 4:
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Stacked RNNs in code
Exercise 1.
- A 2-layer stacked RNN equalled applying the second layer to the first layer’s output.
- The parameter count matched the formula.
Practice.
nn.LSTM(..., num_layers=L, dropout=p)stacks layers and applies dropout between them (not on the last layer’s output).- Beyond 2–4 layers, add residual connections between equal-width layers. Google’s GNMT used 8-layer encoder and decoder LSTMs with residual connections from the third layer up (Wu et al., 2016).
- Layer normalisation inside the cell stabilises deep stacks (Ba et al., 2016).
- Consider bidirectional lower layers and unidirectional upper layers, or downsampling in time between layers (pyramidal encoders, as in Listen, Attend and Spell), which shortens sequences for higher layers.
lstm = nn.LSTM(input_size=d, hidden_size=n, num_layers=4, dropout=0.2, batch_first=True)
out, (h, c) = lstm(x) # h: (4, B, n), one final state per layer
# residual variant: run layers one by one and add each layer's input to its output- 1. nn.LSTM's dropout argument applies…
- 2. GNMT's encoder depth:
- 3. Pyramidal encoders…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
What depth can add.
- One layer applies a single affine map and nonlinearity per step. Complex per-step functions need many units, or several steps of computation.
- Stacking gives several nonlinear stages per time step and lets layers specialise in timescales. Hermans and Schrauwen (2013) found higher layers of deep RNNs operated on longer timescales.
- Pascanu et al. argued that deep transitions and deep outputs are other, sometimes better, ways to add per-step depth.
What depth costs.
- Gradients must now flow through layers as well as time, so deep stacks need residual connections, normalisation and careful dropout.
- Each layer adds sequential computation, but wavefront scheduling hides most of it.
- At a fixed parameter budget, depth trades width, so a deeper but narrower stack may not win. In exercise 2 it did not.
Evidence.
- Graves, Mohamed and Hinton (2013) found deep bidirectional LSTMs clearly better than shallow ones for TIMIT phoneme recognition.
- Sutskever et al. (2014) found 4-layer LSTMs better than shallower ones for translation.
- These are large-data tasks with genuine hierarchical structure.
- 1. Higher layers of deep RNNs tend to work on…
- 2. Other ways to add depth:
- 3. Deep stacks need…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.Does depth help at equal size?
History.
- J. Schmidhuber’s neural history compressor (1992) and S. El Hihi and Y. Bengio’s hierarchical RNNs (1996) used stacked recurrent levels for multiple timescales.
- A. Graves, A. Mohamed and G. Hinton showed deep LSTMs for speech (ICASSP 2013).
- R. Pascanu, Ç. Gülçehre, K. Cho and Y. Bengio analysed ways to deepen RNNs (ICLR 2014).
- Y. Wu et al.’s GNMT (2016) stacked 8 residual LSTM layers.
Exercise 2.
- Task: output, at every step, whether the product of the last 5 inputs (random ±1) is +1. This is temporal parity, a classic hard task for shallow networks.
- Models: tanh RNNs of about equal size: one 16-unit layer (305 parameters including the output), two 9-unit layers (280) and three 7-unit layers (281).
- Training: Adam on batches of 32 sequences of length 20, 3 seeds.
| layers | after 100 steps | after 200 steps |
|---|---|---|
| 1 × 16 | 0.987, 1.000, 1.000 | 1.0, 1.0, 1.0 |
| 2 × 9 | 0.968, 1.000, 0.940 | 1.0, 1.0, 1.0 |
| 3 × 7 | 1.000, 0.936, 0.939 | 1.0, 1.0, 1.0 |
- Depth did not help here. Every model reached 100%, and the single wide layer was as fast or faster (mean 0.996 at 100 steps against 0.970 and 0.958). It needs fewer recurrent stages to train through.
- The task does not need a hierarchy: a single recurrent layer can compute parity over time by building it up step by step. Parity of 3 and 4 bits, and smaller budgets (55–96 parameters), gave the same picture.
- When depth does help, it is usually at scale and on hierarchical data (speech, language). Test it against wider single layers before paying for it.
Stacked RNNs in AI, robotics and aerospace
- Speech and translation: deep stacked LSTMs (often bidirectional below) were the production standard before transformers.
- Multi-rate sensing: stacked or hierarchical RNNs can handle fast IMU signals in lower layers and slow mission phases higher up.
- Edge deployment: at fixed memory, prefer a wider single layer unless experiments show depth helps.
References
- A. Graves, A. Mohamed, G. Hinton (2013). “Speech recognition with deep recurrent neural networks.” Proc. ICASSP.
- R. Pascanu, Ç. Gülçehre, K. Cho, Y. Bengio (2014). “How to construct deep recurrent neural networks.” Proc. ICLR.
- M. Hermans, B. Schrauwen (2013). “Training and analysing deep recurrent neural networks.” Proc. NeurIPS.
- Y. Wu et al. (2016). “Google’s neural machine translation system: bridging the gap between human and machine translation.” arXiv:1609.08144.