Dive into Deep Learning · §12.1
Gated Recurrent Networks: LSTM and GRU
vanishing gradients · sigmoid decay gates · memory cells · gate bias initialization · residual sequence mixers
Motivation
\mathbf{h}_t = \tanh(\mathbf{W}_\textrm{hh}\mathbf{h}_{t-1} + \mathbf{W}_\textrm{xh}\mathbf{x}_t + \mathbf{b}_\textrm{h})
Vanishing & Exploding Gradients
\frac{\partial \mathbf{h}_t}{\partial \mathbf{h}_{t-k}} = \prod_{s=t-k+1}^{t} \textrm{diag}(1 - \mathbf{h}_s^2)\,\mathbf{W}_\textrm{hh}
The Gating Principle
c_t = f_t\, c_{t-1} + i_t\, \tilde{c}_t, \qquad \tau_{1/2} = \frac{\ln 2}{\ln(1/f)}
LSTM Mechanics
The LSTM (Hochreiter & Schmidhuber, 1997; Gers et al., 2000) maintains an internal memory cell \mathbf{c}_t \in \mathbb{R}^d and an exposed hidden state \mathbf{h}_t \in \mathbb{R}^d:
Silent Retention & Gradient Highway
LSTM Implementation
Because \mathbf{i}_t, \mathbf{f}_t, \tilde{\mathbf{c}}_t, and \mathbf{o}_t all read (\mathbf{x}_t, \mathbf{h}_{t-1}), stacking their weights into \mathbf{W}_\textrm{x} \in \mathbb{R}^{d_\textrm{in} \times 4d} and \mathbf{W}_\textrm{h} \in \mathbb{R}^{d \times 4d} computes all four pre-activations in one batched matrix product per step:
max difference: 6.0e-08
Our from-scratch loop matches fused nn.LSTM to float32 rounding error (\sim 10^{-7}), so we use fused library kernels for training.
GRU Architecture
The GRU (Cho et al., 2014) merges the cell and hidden state into a single vector \mathbf{h}_t \in \mathbb{R}^d governed by two gates:
Architectural Comparison
Experimental Setup
Gated equations represent long memories, but does gradient descent actually discover them? We test retention on synthetic delayed recall:
RecallModel): Token embedding (d_\textrm{in} = 32) \to single recurrent layer (d = 64, LeCun normal weights) \to linear classifier at step T, trained for 1{,}500 steps of Adam (\text{lr} = 3 \times 10^{-3}, batch size 128).[[ 6 10 10 8 8 8]
[ 5 14 13 15 12 12]] [[6]
[5]]
Initialization Hazard
float32, 2^{-49} \approx 1.8 \times 10^{-15} at T=50 is rounded away when the state adds it to the O(1) contributions of recent steps.Chrono & Keep-Gate Bias
Spectral Radius Analysis
Before training, we also compare two initializations of a 64 \times 64 plain RNN recurrent matrix \mathbf{W}_\textrm{hh}:
LeCun normal: spectral radius 1.02, largest singular value 2.01
default scale: spectral radius 0.62, largest singular value 1.10
Empirical Results
Accuracy across two random seeds at delays T \in \{20, 50, 100, 200\} (chance = 0.125):
20 50 100 200
RNN 1.00/1.00 0.25/0.26 1.00/0.35 0.23/0.24
RNN, default scale 1.00/1.00 1.00/0.12 0.12/0.12 0.12/0.13
LSTM 0.61/0.22 0.12/0.12 0.12/0.13 0.12/0.12
GRU 1.00/1.00 0.12/0.12 0.13/0.13 0.13/0.13
LSTM, keep gate 1.00/1.00 1.00/1.00 1.00/1.00 1.00/1.00
GRU, keep gate 1.00/1.00 1.00/1.00 1.00/1.00 1.00/1.00
Initializing forget/update biases to +5 (keep gate) turns complete failure (0.12) at T \ge 50 into 1.00 accuracy across every seed and delay up to T = 200.
Interpreting the Recall Sweep
Comparing the three model pairs in the delayed recall table isolates architecture from initialization:
float32.Gates replace fragile whole-matrix eigenvalue tuning with per-unit learned decay, provided initial biases keep the gradient horizon open.
The SeqLM Backbone
\mathbf{H} \leftarrow \mathbf{H} + \textrm{mixer}(\textrm{LN}(\mathbf{H})), \quad \mathbf{H} \leftarrow \mathbf{H} + \textrm{MLP}(\textrm{LN}(\mathbf{H}))
mixer(·) changes across §§12.1–12.8.Language Modeling Benchmark
Two runs per configuration reporting parameters, overall validation loss (nats/byte), loss by window position band (0-15, 16-63, 64-127), and training step time:
validation 0-15 16-63 64-127 ms/step
RNN 2,764,544 1.347/1.349 1.510/1.512 1.324/1.325 1.324/1.325 13/13
LSTM 4,343,552 1.292/1.293 1.488/1.490 1.266/1.267 1.263/1.264 32/38
GRU 3,817,216 1.297/1.299 1.490/1.492 1.271/1.273 1.269/1.271 34/38
attention 3,286,784 1.378/1.375 1.560/1.556 1.354/1.350 1.351/1.349 20/21
RNN, d=302 3,816,932 1.334/1.334 1.501/1.501 1.310/1.311 1.309/1.310 21/21
Quality vs. Step Cost
Three systems and modeling lessons emerge from the WikiText-103 benchmark:
SeqLM reaches \sim 1.35 nats/byte—matching Chapter 11’s 6-block SwiGLU GPT and outperforming the 4-block GELU attention stack (1.38) at this 1{,}000-step budget.LSTM or GRU lowers loss by 0.05 nats/byte to \sim 1.30, whereas widening the plain RNN to match the GRU’s 3.81\text{M} parameters (RNN, d=302) closes less than a third of that gap (\sim 1.33).LSTM and GRU take 1.4\times–3\times longer per step than the plain RNN because each step computes three or four gate products instead of one. All three wait for \mathbf{h}_{t-1}; the length sweep of §12.3 isolates that cost.Check Your Understanding
Work out the following three design questions:
init_keep_gate set the LSTM input-gate bias to b_\textrm{i} = -5 alongside b_\textrm{f} = +5, whereas the GRU only needs b_\textrm{z} = +5?Answers:
float32 when summed with recent gradients—destroying directional information.Key Takeaways
| Architecture / Setup | Direct Memory Path | Recurrent Params (d_\textrm{in}=d) | T=200 Recall Accuracy | WikiText-103 (nats/byte) | Training Depth |
|---|---|---|---|---|---|
| Plain RNN (uniform scale) | \tanh(\mathbf{W}_\textrm{hh}\mathbf{h}_{t-1} + \cdots), \rho \approx 0.62 | 2d^2 | 0.12 (chance) | — | T sequential steps |
| Plain RNN (LeCun normal) | \tanh(\mathbf{W}_\textrm{hh}\mathbf{h}_{t-1} + \cdots), \rho \approx 1.02 | 2d^2 | 0.12–0.24 (up to 1.00 at T=100) | 1.346–1.349 (1.332–1.337 at d=302) | T sequential steps |
| LSTM (zero gate bias) | \mathbf{c}_t = \mathbf{f}_t \odot \mathbf{c}_{t-1} + \mathbf{i}_t \odot \tilde{\mathbf{c}}_t, f_0 = 0.5 | 8d^2 | 0.12 (chance at T \ge 50) | 1.292–1.296 | T sequential steps |
| GRU (zero gate bias) | \mathbf{h}_t = \mathbf{z}_t \odot \mathbf{h}_{t-1} + (1-\mathbf{z}_t)\odot \tilde{\mathbf{h}}_t | 6d^2 | 0.12–0.13 (chance at T \ge 50) | 1.297–1.300 | T sequential steps |
| LSTM / GRU (keep gate +5) | Initial gate \sigma(5) = 0.993 (\tau_{1/2} \approx 103) | 8d^2 / 6d^2 | 1.00 (all seeds) | — | T sequential steps |