9.2 LSTM (torch gate order)
The vanilla RNN showed its problem in numbers: the gradient into a state 30 steps back is the late gradient times thirty times, and with that is a factor of about . The vanilla RNN language model learns which character comes next but not that a quote opened a line ago must close. The long short-term memory (LSTM) fixes this with one structural change, a memory cell updated by addition instead of by a squashing matmul, plus three gates that decide what to write, what to keep, and what to show.
Concepts from other sections (13)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- RNN language model: an embedding, a recurrent layer, and a linear read-out trained to predict the next token. Read more: 9.6 RNN language model with stateful TBPTT
- PCG32: an LCG whose output is a permutation (xorshift, then a state-chosen rotation) of the old state. Read more: 12.3 Modular arithmetic, hashing, and PCG32 in Python in Mathematics
- sigmoid: , mapping the real line into , with . Read more: 7.3 Activation Functions and Their Derivatives in Mathematics
- Dropout: randomly zeroing activations during training to approximate an ensemble of subnetworks. Read more: Chapter 2: Deep Learning
- cell state: the LSTM’s additively updated memory, protected by forget, input, and output gates. Read more: Chapter 3: Neural Architectures and Their History
- Orthogonal matrix: a square with ; its inverse is its transpose. Read more: 9.3 Orthogonality, Householder QR, and orthogonal initialization in Mathematics
- VJP (vector-Jacobian product): , the gradient of ; what reverse mode computes. Read more: 10.2 Jacobians, the multivariable chain rule, and numeric JVP/VJP in Mathematics
- op library (
F): the module of differentiable functions, each a NumPy forward plus one closed-form VJP joined byfrom_op. Read more: 6.3 The op library and gradcheck_all - Backpropagation through time (BPTT): reverse-mode differentiation of the recurrence unrolled over time, with weights shared across steps. Read more: 9.1 Vanilla RNN with manual BPTT and truncation
- Gradcheck: a comparison of a hand-written gradient against central differences of . Read more: 3.3 Oracles, golden and differential tests, gradcheck as a test in Software Engineering
- strict loading: refusing a state dict with any missing or unexpected key, before copying anything. Read more: 6.5 Module system and basic layers
- central differences: the numerical derivative , used as an oracle for analytic gradients. Read more: 6.3 The op library and gradcheck_all
- registration order: the order layers are created in a
Module, which fixes both the state-dict keys and the order of random initialization draws. Read more: 8.2 Bengio’s neural probabilistic language model
The LSTM is also the first model in this book whose weights must match a framework’s layout exactly. torch-trained LSTMs are everywhere, and the solution loads torch’s weights by name. With autograd from Part 0 you write only the forward; the backward through time comes for free. Two LSTMs make the bidirectional layer of the next sections, the RNN language model uses it as cell='lstm' (where it must beat the vanilla RNN on the same text), and it is the LSTM option of the encoder in Encoder-decoder with teacher forcing.
This section builds on Tensor, the op library the cell is written in, and the module system (Module, load_state_dict, Dropout) from Part 0. From Mathematics it uses xavier_uniform from Variance propagation and initialization, orthogonal_init from Orthogonality, Householder QR, and orthogonal initialization, and the default PCG32 stream from Modular arithmetic, hashing, and PCG32; Activation Functions and Their Derivatives is useful background for the sigmoid and tanh. The solution is lstm.py: lstm_cell, LSTMCell, and LSTM (stacked layers, inter-layer dropout, lengths for padded batches), with the helpers _init and _mask. Its contract is lstm.pyi and its model tests are test_lstm.py.
Learning objectives
- Write the LSTM cell in torch’s gate order , with both biases, and say why each gate uses the squashing function it does.
- Show that along the cell path , and explain why this keeps gradients from vanishing.
- Initialise an LSTM so it is trainable from step 0: orthogonal recurrent blocks and a forget bias of 1.
- Stack layers with torch’s names and dropout convention, and mask a padded batch so padding never reaches an output or a gradient.
The cell and its gates
Section titled “The cell and its gates”The table fixes the notation.
| Symbol | Meaning | Type / shape |
|---|---|---|
| input at step | float32[B, D] | |
| , | hidden state (what the next layer sees) and cell state (the memory) | float32[B, H] each |
| , | input and recurrent weights, four gate blocks stacked | [4H, D], [4H, H] |
| , | the two biases (torch keeps both) | [4H] each |
| all four pre-activations, | [B, 4H] | |
| input gate, forget gate, candidate, output gate | [B, H] each | |
| the logistic sigmoid, values in | function | |
| elementwise product | ||
| the length of sequence in a padded batch | integer in |
One matmul per weight computes every gate at once, and the result is split into four blocks of columns, in torch’s order:
The gates are sigmoids because they are fractions: how much of the old memory to keep (), how much of the candidate to write (), how much of the memory to show (). The candidate is a tanh because it is content, centered at 0 and able to subtract. The output goes through one more tanh so stays in however large grows.
Why the gradient does not vanish
Section titled “Why the gradient does not vanish”Differentiate the cell update: , plus terms through the gates’ dependence on . Along the cell path the gradient from to is , elementwise, with no weight matrix and no tanh slope in it. If the network wants to remember, it sets and the gradient flows back almost unchanged; Hochreiter called this the “constant error carousel”. With the cell path is the only path, and is exactly .
torch’s layout
Section titled “torch’s layout”The math fixes what the cell computes; the layout fixes how its parameters are stored, and that is what makes checkpoints interchangeable. torch.nn.LSTMCell and torch.nn.LSTM store weight_ih and weight_hh , one row per gate unit, so the products use the transpose, like Linear in the module system. They also store two biases, bias_ih and bias_hh. The two biases only ever appear as a sum, so a single bias is mathematically enough, but the checkpoint has both keys and both receive the same gradient.
A stacked LSTM names its parameters per layer: weight_ih_l0, weight_hh_l0, bias_ih_l0, bias_hh_l0, weight_ih_l1, ..., registered in that order (assignment order is state_dict order). Layer reads layer ‘s output sequence, so its weight_ih is , and each layer starts from its own slice of the initial state.
Initialization, stacking, and padding
Section titled “Initialization, stacking, and padding”A correct cell can still be hard to train if it starts in the wrong place. The contract draws, per layer, each of the four input blocks with xavier_uniform (gain 1) and each of the four recurrent blocks with orthogonal_init. An orthogonal matrix has every singular value 1, so the recurrence neither inflates nor shrinks the state at step 0; this is the vanilla-RNN analysis with . The forget-gate block of is 1 and every other bias is 0, so starts near : the cell remembers by default (Gers 2000, Jozefowicz 2015). torch’s own default draws everything from ; comparisons with torch load torch’s weights, so the initializations need not match.
For stacking, torch applies dropout to each layer’s output sequence except the last layer’s, and only in training mode. The dropout here is the module system’s Dropout, so eval() turns it off.
Finally, batching sequences of different lengths pads them to steps. torch’s pack_padded_sequence makes the RNN skip padding; the equivalent here is a mask. At a step , sequence keeps its unchanged (F.where(mask, new, old)) and outputs 0. Then the returned final state is each sequence’s state after its own last real step, and nothing the padding contains reaches any output or receives any gradient.
Working it by hand
Section titled “Working it by hand”Take one unit (), , , . The weights are (only the candidate reads ), , , . So .
- ; ; ; .
- .
- .
- Backward from : , and through the cell path , so .
With the gate order the same weights give and , so and : one swapped block, every number different.
The model tests in test_lstm.py start from this example (lstm_cell with and must give and ). They then compare the cell with a numpy formula under random weights and two nonzero biases, and check the state_dict keys of a 2-layer LSTM, the forget bias and orthogonal blocks, a padded batch, a 2-layer LSTM against stacked numpy cells from a nonzero , dropout ignored by a 1-layer LSTM, and bad arguments.
Designing the interface
Section titled “Designing the interface”The module has a functional cell and two modules:
def lstm_cell(x, h, c, w_ih, w_hh, b_ih, b_hh) -> tuple[Tensor, Tensor] # (h', c')class LSTMCell(Module): # weight_ih [4H, D], weight_hh [4H, H], bias_ih, bias_hh [4H] def __init__(self, d_in, d_h, rng=None); def forward(self, x, state=None)class LSTM(Module): # weight_ih_l{k}, weight_hh_l{k}, bias_ih_l{k}, bias_hh_l{k} def __init__(self, d_in, d_h, num_layers=1, dropout=0.0, rng=None) def forward(self, x, state=None, lengths=None) -> tuple[Tensor, tuple[Tensor, Tensor]] # x [T, B, d_in] -> out [T, B, H], (h_n, c_n) [num_layers, B, H]lstm_cell is one step on explicit tensors. LSTMCell wraps it with torch’s parameter names. LSTM runs the whole sequence through num_layers stacked layers; with no state it starts from zeros, and lengths gives each sequence’s real length in a padded batch.
Building it step by step
Section titled “Building it step by step”The reasoning that shapes the code runs in six steps.
- Give the gradient a path without a squashing matmul. In the vanilla RNN the carry is multiplied by every step. A state updated as has along that path, so the network controls the decay directly through .
- Each decision is a fraction or a content. Keep, write, and show are amounts in : sigmoids. The candidate is signed content: a tanh. keeps the visible state bounded however large grows.
- Compute all gates with one matmul per weight. The four gates read the same and , so stacking their weights into and slicing the result is four matmuls in one, and fixes a block order. Matching torch means torch’s order (), torch’s orientation (, used transposed), and torch’s two biases.
- Autograd does the backward. Everything is
F.matmul,F.transpose, slicing,F.sigmoid,F.tanh, and elementwise ops, all with VJPs from the op library, so BPTT through any number of steps and layers comes from the graph. - Initialise for step 0. Orthogonal recurrent blocks have every singular value 1, so the state’s scale is preserved; a forget bias of 1 starts at so memory is kept by default.
- Padding must be invisible. Past its length, a sequence must neither change its state nor emit output, and nothing computed at a padded step may receive gradient. A per-step mask selecting between the new and the old state does all three.
One layer’s parameters
Section titled “One layer’s parameters”Parameters are drawn block by block from one generator in the contract’s order: four Xavier input blocks, then four orthogonal recurrent blocks.
def _init(d_in, d_h, rng): r = rng if rng is not None else PCG32(0).substream("init") w_ih = np.concatenate([xavier_uniform((d_h, d_in), 1.0, r) for _ in range(GATES)]) # [4H, D] w_hh = np.concatenate([orthogonal_init((d_h, d_h), 1.0, r) for _ in range(GATES)]) # [4H, H] b_ih = np.zeros(GATES * d_h) b_ih[d_h : 2 * d_h] = 1.0 # forget gate open at the start: remember by default b_hh = np.zeros(GATES * d_h) return w_ih, w_hh, b_ih, b_hhEach recurrent block is orthogonal on its own; the stacked matrix is not square, so drawing per block is what gives every gate’s recurrence singular values of 1. The forget block is the second slice, , because of the gate order.
The cell
Section titled “The cell”Both biases are added (they only ever appear as a sum, but torch has two keys and both must receive gradient), then the four slices are taken in torch’s order.
def lstm_cell(x, h, c, w_ih, w_hh, b_ih, b_hh): z = (F.matmul(x, F.transpose(w_ih, 0, 1)) + b_ih + F.matmul(h, F.transpose(w_hh, 0, 1)) + b_hh) # [B, 4H] H = z.shape[-1] // GATES i = F.sigmoid(z[..., 0:H]) # input gate: how much to write f = F.sigmoid(z[..., H : 2 * H]) # forget gate: how much to keep g = F.tanh(z[..., 2 * H : 3 * H]) # candidate: signed content o = F.sigmoid(z[..., 3 * H : 4 * H]) # output gate: how much to show c_new = f * c + i * g # additive: dc_new/dc = f, no matmul, no tanh slope return o * F.tanh(c_new), c_new # h stays in (-1, 1)On the hand example, , so , , . The h and c arguments may be plain arrays (a zero start state); F ops treat them as constants.
The single cell module
Section titled “The single cell module”LSTMCell registers torch’s names in torch’s order, so its state_dict matches torch.nn.LSTMCell.
w_ih, w_hh, b_ih, b_hh = _init(d_in, d_h, rng)self.weight_ih = _param(w_ih) # float32, requires_gradself.weight_hh = _param(w_hh)self.bias_ih = _param(b_ih)self.bias_hh = _param(b_hh)The padding mask
Section titled “The padding mask”The mask is a boolean [T, B, 1] that is true at real steps; the trailing axis broadcasts over .
def _mask(lengths, T, B): if lengths is None: return None n = np.asarray(lengths) # ... B integers, each in 1..T ... return (np.arange(T)[:, None] < n[None, :])[:, :, None] # step t of sequence b is realThe stacked LSTM
Section titled “The stacked LSTM”Layers are created with one shared generator and registered as weight_ih_l{k}, weight_hh_l{k}, bias_ih_l{k}, bias_hh_l{k}; layer reads inputs.
r = rng if rng is not None else PCG32(0).substream("init")for k in range(num_layers): w_ih, w_hh, b_ih, b_hh = _init(d_in if k == 0 else d_h, d_h, r) # torch's names, in torch's order: the safetensors key contract. setattr(self, f"weight_ih_l{k}", _param(w_ih)) setattr(self, f"weight_hh_l{k}", _param(w_hh)) setattr(self, f"bias_ih_l{k}", _param(b_ih)) setattr(self, f"bias_hh_l{k}", _param(b_hh))self.drop = Dropout(dropout)The forward runs layer by layer over the whole sequence. Each layer starts from its own slice h0[k], c0[k], and the mask keeps a finished sequence’s state while zeroing its output.
mask = _mask(lengths, T, B)seq = xh_n, c_n = [], []for k in range(L): w = [getattr(self, f"{n}_l{k}") for n in ("weight_ih", "weight_hh", "bias_ih", "bias_hh")] h, c = h0[k], c0[k] # this layer's own initial state outs = [] for t in range(T): h_new, c_new = lstm_cell(seq[t], h, c, *w) if mask is None: h, c = h_new, c_new outs.append(h_new) else: # Past its length a sequence keeps its state and outputs 0. m = mask[t] h = F.where(m, h_new, h) c = F.where(m, c_new, c) outs.append(F.where(m, h_new, 0.0)) seq = F.stack(outs, axis=0) # [T, B, H]: the next layer's input if k < L - 1: seq = self.drop(seq) # between layers only, and only in train() h_n.append(h) c_n.append(c)return seq, (F.stack(h_n, axis=0), F.stack(c_n, axis=0))F.where routes the gradient only to the branch it selected, so a padded step’s h_new and c_new, and the padded inputs behind them, receive zero gradient. Because and the mask is monotone in , once a sequence ends its h, c are frozen at the last real step, which is the final state returned.
The full solution is lstm.py.
Why this design
Section titled “Why this design”Per step and layer the cell does two matmuls of cost , four times a vanilla RNN of the same ; parameters are per layer. Autograd keeps every intermediate of every step, memory.
The vanilla RNN wrote its backward by hand, so why autograd here? The LSTM’s backward is long (four gates, two states, two biases) and error-prone; autograd gets it from tested primitives, and the gradcheck and torch comparison confirm it. The manual backward was about seeing the carry; here the lesson is the cell path.
torch’s layout is non-negotiable because checkpoints are interchangeable only if names, shapes, gate order, and the two biases match. A single bias computes the same function, but a torch checkpoint then has an extra key and strict loading fails or drops half the bias. Masking is preferred to packing for simplicity: pack_padded_sequence saves compute by shrinking the batch, while masking computes padded steps and discards them. Masking keeps shapes fixed and gives the same outputs, states, and gradients. The forget bias of 1 and orthogonal blocks make the network trainable from step 0: without them the cell halves its memory each step at the start () and long dependencies are learned late or never.
The tradeoffs accepted are Python loops over time and layers, float32 parameters (the gradcheck uses float64 inputs), and no fused kernel.
What can go wrong
Section titled “What can go wrong”These are the checks a correct solution passes, with the reason each matters downstream.
| Case | Kind of check | Checks | Why it matters downstream |
|---|---|---|---|
| The hand example cell | unit | , , | the gates are in the right order with the right squashing |
| Matches torch’s cell | golden | torch.nn.LSTMCell outputs and every gradient | torch weights load and run |
| Matches torch’s sequence | golden | one layer with a state, two layers, a packed padded batch: outputs, , all gradients | the language model and the seq2seq encoder load torch checkpoints |
| State-dict names match torch | golden | torch’s parameter names, order, and shapes | the safetensors key contract |
| Gradcheck of the cell | gradcheck | autograd through the cell against central differences, float64 | the backward is right without torch |
| The cell state is a gradient highway | property | with , | the reason the LSTM remembers |
| Forget bias and orthogonal recurrence | unit | forget bias 1, others 0; orthogonal blocks; Xavier bound; seeding | trainable from step 0 |
| Lengths keep padding out | boundary | padded outputs 0; final state equals running the sequence alone; padding gets no gradient | batches of sentences |
| Dropout between layers only | unit | dropout between layers, in training mode only | evaluation is deterministic |
| Default state and bad arguments | boundary | no state means zeros; bad shapes, lengths, sizes raise | errors at the call site |
Six mistakes account for most broken LSTMs.
Warning: Putting the gates in another order ( is common in papers and other frameworks) trains fine from scratch, but torch weights load and produce garbage. The solution slices
zas .
Warning: The wrong squashing ( through a sigmoid, without the tanh, the input gate dropped, or the old cell squashed as ) means the cell can only add, grows without bound, or the gradient highway gains a tanh slope per step and vanishes again. The solution writes
g = F.tanh(...),c_new = f * c + i * g,h = o * F.tanh(c_new).
Warning: A forget bias of 0, or the on the wrong block, makes the cell forget half its memory per step at the start, so long dependencies are learned late or never.
_initsetsb_ih[d_h:2*d_h] = 1.0, the forget block.
Warning: Padding leaking into the state or the outputs makes the final state of a short sentence depend on how long its batch-mates are. The solution applies
F.wheretoh,c, and the output with the_mask.
Warning: One bias instead of two computes the same function, but a torch checkpoint has an extra key, and loading it drops half the bias. The solution registers and adds both
bias_ihandbias_hh.
Warning: Stacking mistakes (dropout after the last layer, layer starting from layer ‘s final state) make training and eval outputs differ in a 1-layer model and 2-layer outputs differ from torch. The solution guards dropout with
if k < L - 1and starts each layer fromh, c = h0[k], c0[k].
Where this leads
Section titled “Where this leads”Looking back, parameters are Tensors and x[t] and h0[k] are getitem; the cell uses F.matmul, F.transpose, F.sigmoid, F.tanh, F.where, and F.stack from the op library; Module registration order is the key order and Dropout sits between layers. xavier_uniform, orthogonal_init, and PCG32(0).substream("init") (used when no rng is given) come from Mathematics.
Looking forward, the bidirectional RNN builds bidirectional(LSTM, LSTM, x, lengths); the RNN language model builds RNNLM(cell='lstm'), trains it with stateful TBPTT, and expects it to beat the vanilla RNN on the same text; and Encoder-decoder with teacher forcing offers it as the seq2seq encoder.
Production systems refine each piece:
| This piece | Production equivalent | What it adds | Where to look |
|---|---|---|---|
lstm_cell | cuDNN’s fused LSTM | the input projection for all steps as one GEMM, the four gates fused into one kernel | aten/src/ATen/native/cudnn/RNN.cpp |
lengths masking | torch.nn.utils.rnn.pack_padded_sequence | sorts by length and shrinks the batch as sequences end, so no padded step is computed | torch/nn/utils/rnn.py |
| forget bias 1 | Keras unit_forget_bias=True | the same default, on by default | keras/layers/rnn/lstm.py |
| the LSTM itself | xLSTM (2024) | exponential gating and a matrix memory, parallelizable over time | NX-AI/xlstm |
Further reading
Section titled “Further reading”- Hochreiter and Schmidhuber, “Long Short-Term Memory” (1997).
- Gers, Schmidhuber, and Cummins, “Learning to Forget” (2000).
- Jozefowicz, Zaremba, and Sutskever, “An Empirical Exploration of Recurrent Network Architectures” (2015).
- Olah, Understanding LSTM Networks (2015).
Key terms
Section titled “Key terms”- Cell state : the LSTM’s memory, updated additively as .
- Forget gate : a sigmoid fraction of the old cell to keep; along the cell path it is the only factor on the gradient.
- Input gate , candidate , output gate : how much new content to write, the signed content itself, and how much of to expose as .
- Constant error carousel: the cell path along which the gradient is multiplied only by , so it survives many steps when .
- torch gate order: the block order in torch’s stacked weights.
- Padding mask: a per-step boolean that freezes a finished sequence’s state and zeros its output.
Summary
Section titled “Summary”- An LSTM keeps a second state, the cell , updated additively: . Along it the gradient is multiplied only by the forget gate, so with near 1 it survives many steps.
- The four gate blocks are stacked in torch’s order , with two biases, so torch weights load by name and give torch’s outputs and gradients.
- Initialization matters at step 0: a forget bias of 1 makes the cell remember by default, and orthogonal recurrent blocks keep the state’s scale.
- In a padded batch each sequence stops at its own length: its outputs past it are 0 and its final state is the state after its last real token.
- Autograd supplies the backward; a gradcheck and golden comparisons with torch confirm it.
Review questions
Section titled “Review questions”- In the hand example, . Which two factors make it, and why is there no weight matrix in it?
Answer
$\partial h_t / \partial c_t = o (1 - \tanh^2 c_t) = 0.490463$ and, along the cell path, $\partial c_t / \partial c_{t-1} = f = 0.75$. The cell update $c_t = f \odot c_{t-1} + i \odot g$ multiplies $c_{t-1}$ only by $f$ (and $W_{hh} = 0$ here, so the gates do not depend on $h_{t-1}$), so no matrix appears. - Why does appear on for the output but must not appear on in the update?
Answer
The output tanh bounds $h$, which feeds matmuls. Squashing the old cell ($f \odot \tanh c_{t-1}$) would put a factor $1 - \tanh^2$ on the cell path at every step, which is exactly the vanishing factor the LSTM was built to remove. - A model trained with gate order is loaded as . Why does it not fail loudly?
Answer
The shapes are identical, so every key loads. The third and fourth blocks swap roles: the candidate goes through a sigmoid and the output gate through a tanh. Every number changes (in the hand example, $c' = 0.775$ instead of 0.675) but nothing errors; only a golden comparison catches it. - Why does
F.where(m, h_new, h)make padding receive no gradient?Answer
The VJP of `where` sends the incoming gradient to the selected branch and zero to the other. At padded steps the old `h` is selected, so `h_new`, the cell computation behind it, and the padded input get zero gradient; the output there is the constant 0. - Why is dropout applied to every layer’s output except the last?
Answer
That is torch's convention: dropout regularises what one layer passes to the next. The last layer's output goes to whatever the caller stacks on top, which applies its own regularisation; applying it inside would make a 1-layer LSTM differ between `train()` and `eval()` and differ from torch.