5.A — LSTM and GRU: gating as a learned residual path (appendix)
Goal: Derive the LSTM cell and the GRU as mitigations of the vanishing-gradient problem, see why an additive cell-state path is the key, and recognise the same gated-residual pattern in modern Transformer FFNs and state-space models.
1. Why this appendix exists
The vanilla RNN of 5.5 suffers vanishing/exploding gradients (Bengio, Simard & Frasconi, 1994). For two decades, the field's response was a single architectural family: gated recurrent cells, of which the LSTM (Hochreiter & Schmidhuber, 1997)[2] and the GRU (Cho et al., 2014)[4] are the canonical members.
These cells are no longer used in modern LLMs. We retire them — gracefully — by understanding why they worked and which of their ideas survived. The "gated residual" pattern reappears in three downstream places:
- Transformer FFN blocks with gated activations (GLU, SwiGLU, GeGLU — see 2.3).
- Highway networks and the residual stream of Transformers more broadly (foreshadowed in Chapter 6).
- Selective state-space models (Mamba — coming in Chapter 8).
Read this appendix as cultural literacy and as motivation for why "additive paths with learnable gates" is one of the most important architectural primitives in deep learning, even after the specific cell types fell out of fashion.[1]
2. Diagnosis: why vanilla RNNs vanish
Recall from 5.5 §4 that the vanilla-RNN backward recurrence
multiplies by a factor whose spectral norm is bounded by \sigma_\max(W) \cdot \max_t |\phi'(\cdot)|. For , , so unless \sigma_\max(W) = 1 exactly, the product over many time steps either vanishes or explodes.
The structural fix is: design the recurrence so the default backward Jacobian is the identity matrix. If in the absence of any update, then the product and gradients pass through losslessly. The LSTM does this — but only on a designated "memory" path, the cell state , which is separate from the visible hidden state .
3. The LSTM cell
For a hidden size and an input embedding , the LSTM maintains two state vectors: the cell state and the hidden state .
Four gates, each a linear-then-sigmoid layer:
The cell state update is the key piece:
where is element-wise multiplication. And the hidden state is a gated read of the cell state:
Read it: the cell state is updated by forgetting a fraction of its previous value and adding a fraction of a new candidate. The hidden state is what the rest of the network sees of the cell, gated by .
3.1 Parameter count
Four gate matrices each of shape (combining , , ). Total:
An LSTM has roughly more parameters than a vanilla RNN with the same hidden size.
4. Why gating mitigates vanishing
Differentiate the cell-state recurrence with respect to the previous cell state:
(Other terms — from , , also depending on , which depends on — contribute additional smaller terms, but the dominant path is just .) If the forget gate is open (), then and the cell state passes through the time step additively, with the gradient multiplied by approximately 1.
Compare with the vanilla RNN where — a full matrix whose norm is hard to control. The LSTM's is diagonal and bounded in , and most importantly, the network learns to set near 1 wherever long-range memory matters.
Unrolling: the gradient from time to time along the cell-state path picks up the product
which is at most 1 per step and is preserved when . The exponential decay of the vanilla RNN is gone; vanishing happens only when the network chooses to forget. The same reasoning explains why exploding is less of an issue too — each per-step Jacobian is bounded by 1 along the dominant path.
5. The GRU
Cho et al. (2014) proposed a simpler gated cell with only two gates and a single state (no separate cell):
The update equation is a convex combination of the previous state and a candidate update. Setting (close to 1 in the gate's form, the opposite convention) passes through unchanged; setting replaces it with the candidate.
This is structurally the same idea as the LSTM's cell-state update — a gated additive path through time — but with fewer parameters (the GRU has vs for LSTM) and no separate cell state.
Empirical comparisons (Chung et al., 2014; Jozefowicz[6], Zaremba & Sutskever, 2015)[5] found LSTMs and GRUs roughly equivalent on most sequence tasks; the GRU's lower parameter count is a small efficiency win.
6. Why we still abandoned them
Gating fixes the asymptotic vanishing problem on the additive memory path, but it does not fix the two problems that ultimately retired recurrence (5.6):
(a) Effective range is still finite. Gated cells substantially extend the practical memory horizon — LSTMs comfortably handle dependencies of 50–100 tokens, sometimes more — but not arbitrarily. The cell state has finite capacity , and over very long sequences, older information gets gradually overwritten by newer updates. Sufficient depth and gating can't make the cell state magically infinite-capacity.
(b) The sequential bottleneck is unchanged. LSTM and GRU forward passes are still sequential in — the cell state at time depends on , period. No amount of gating fixes the GPU parallelism problem. This is the structural cost that Transformer pretraining at trillion-token scale could not absorb, and it is the bigger of the two reasons we moved on.
By 2018 the field had largely settled: LSTMs continued in production for smaller sequence tasks (small-vocabulary speech, time-series forecasting) where their inference-time efficiency mattered, but pretraining a language model on a web-scale corpus was Transformer-only.
7. Where the idea survives
The implementation — gated cells with cell states and forget gates — is gone from LLMs. The idea — an additive path with a learned gate — is everywhere.
Highway networks (Srivastava, Greff & Schmidhuber, 2015)[7]. A direct generalisation of the LSTM gating idea to feed-forward networks: each layer outputs . This is the immediate predecessor of the residual connection.
Residual connections (He et al., 2015 — ResNet). Drop the gate and always pass the input through: . The residual stream of a Transformer is this pattern at every block, and it is the reason a 100-layer Transformer trains stably where a 100-layer plain MLP would not (cf. 2.9 / 2.10).
Gated FFN variants in Transformers (Shazeer, 2020). Replace the standard FFN with a gated version:
This is the "multiplicative gate on an information path" idea from LSTMs, now applied inside a Transformer block. SwiGLU is the FFN of choice in LLaMA, Mistral, and most LLMs from 2023 onward.
Selective state-space models (Mamba, Gu & Dao, 2023). Modern linear-recurrence architectures revive the recurrence idea with carefully chosen transition matrices and selective (input-dependent) gating. The "selective" mechanism is structurally an LSTM forget-gate, designed for parallel computation via the parallel-scan algorithm. We will see this in Chapter 8.
So the LSTM is gone, but its DNA pervades every modern architecture. The cell type was retired; the design principle was promoted.
(a) Write out the LSTM cell update for one time step explicitly: given and all eight weight/bias matrices, compute and .
(b) Show that (the dominant term). Then write along the dominant path as a product and explain why it does not vanish exponentially when .
(c) Compare the parameter counts of a vanilla RNN, an LSTM, and a GRU with the same hidden size and input size . Express each in terms of and .
(d) The GRU's update is a convex combination. Argue that this guarantees in the element-wise sense. Why does this help stability?
(e) Identify the LSTM/GRU "gated residual" pattern in the SwiGLU FFN and in the Transformer residual connection. In each case, what plays the role of "previous state" and what plays the role of "candidate update"?
References
- [1]
Goodfellow, I., Bengio, Y., & Courville, A. (2016). Deep Learning, §10 (Sequence Modeling: Recurrent and Recursive Nets). MIT Press. — RNN, BPTT, LSTM/GRU (5.5, Appendix 5.A).
↩ - [2]
Hochreiter, S., & Schmidhuber, J. (1997). "Long short-term memory". Neural Computation, 9(8), 1735–1780. — LSTM cell (Appendix 5.A §3).
↩ - [3]
Gers, F. A., Schmidhuber, J., & Cummins, F. (2000). "Learning to forget: Continual prediction with LSTM". Neural Computation, 12(10), 2451–2471. — Forget gate (Appendix 5.A Aside).
↩ - [4]
Cho, K., van Merriënboer, B., Gulcehre, C., et al. (2014). "Learning phrase representations using RNN encoder–decoder for statistical machine translation". EMNLP. arXiv:1406.1078 — GRU (Appendix 5.A §5).
↩ - [5]
Chung, J., Gulcehre, C., Cho, K., & Bengio, Y. (2014). "Empirical evaluation of gated recurrent neural networks on sequence modeling". arXiv:1412.3555 — LSTM/GRU empirical comparison (Appendix 5.A §5).
↩ - [6]
Jozefowicz, R., Zaremba, W., & Sutskever, I. (2015). "An empirical exploration of recurrent network architectures". ICML. — Architecture search over gated RNN variants (Appendix 5.A §5).
↩ - [7]
Srivastava, R. K., Greff, K., & Schmidhuber, J. (2015). "Highway networks". arXiv:1505.00387 — Gated residual feed-forward networks; conceptual bridge from LSTM to ResNet (Appendix 5.A §7).
↩ - [8]
Karpathy, A. (2015). "The unreasonable effectiveness of recurrent neural networks". karpathy.github.io — Cultural-historical reference for LSTM in language modelling (Appendix 5.A Aside).
↩