Skip to content

Part 3: Recurrent Networks

Sequence models with state. A vanilla RNN with backpropagation through time written by hand, then the LSTM and GRU in torch’s gate order (so weights load from torch checkpoints), bidirectional RNNs with length-aware reversal, and an RNN language model trained with stateful truncated BPTT. ELMo’s bidirectional LM and scalar mix is optional.

Course passes: 4 (L3.1 to L3.6, milestone MS-L3, part of gate MS-P4)

Before you start: the matrix-calculus VJPs (M08.3) and gradient clipping (M10.4); the autograd engine of Part 0.

  • BPTT is reverse mode through time: the gradient of an early state is a product of Jacobians, which vanishes or explodes; clipping and gating are the cures.
  • Gates are learned multiplexers: the LSTM’s forget gate lets gradient flow through the cell state almost unchanged.
  • Truncation is a memory budget: stateful TBPTT carries the hidden state across batches but cuts the gradient every kk steps.
ModuleTopicKindPass
L3.1Vanilla RNN with manual BPTT and truncationbuild4
L3.2LSTM (torch gate order i, f, g, o)build4
L3.3GRU (torch gate order r, z, n)build4
L3.4Bidirectional RNN with length-aware reversalbuild4
L3.5ELMo: biLM, ScalarMix, linear probesbuild4, optional
L3.6RNN language model with stateful TBPTTbuild4

The worked example for L3.2 is lstm_cell.c from Neural Architectures, whose RNN and LSTM sections these chapters absorb.

#ModuleChapterKindPass
1L3.1Vanilla RNN with manual BPTT and truncationbuild4
2L3.2LSTM (torch gate order)build4
3L3.3GRU (torch gate order)build4
4L3.4Bidirectional RNN with length-aware reversalbuild4
5L3.5ELMo: biLM, ScalarMix, linear probesside4
6L3.6RNN language model with stateful TBPTTbuild4