Recurrent Sequence Models: RNN, LSTM, GRU & BPTT
A recurrent model reuses one state transition across time, so its memory and its gradient are both governed by repeated Jacobian products. LSTM and GRU create gated, partly additive state paths that make useful time scales easier to learn, while masking, state resets, teacher forcing, decoding, and truncation determine what information the deployed model can actually use.
A recurrent neural network (RNN) processes an ordered stream by carrying a state forward. Unlike a fixed context window, recurrence can run for any number of steps and can operate online with bounded state. The same reuse that gives this flexibility also couples memory, stability, and optimization: the transition is applied repeatedly in the forward pass, and its Jacobian is multiplied repeatedly in the backward pass.
State is a learned summary, not a lossless history #
A basic recurrent transition is
The model shares and across time. Its state can be interpreted as a task-dependent compression of the prefix , but the network has no guarantee of retaining every earlier fact. Training decides what survives because state capacity, activation saturation, noise, and the loss all favor some summaries over others.
The output arrangement follows the task:
| Task shape | Readout | Typical example |
|---|---|---|
| sequence to one | final or pooled states | classify a recording |
| sequence to aligned sequence | one output per input step | frame labeling |
| sequence to unaligned sequence | encoder state plus autoregressive decoder | translation or transcription |
| unconditional sequence | next-item distribution | text or music generation |
Causality is separate from recurrence. A forward RNN is causal; a bidirectional RNN combines past and future context and therefore cannot be used unchanged in a strict streaming system.
BPTT is ordinary backpropagation through shared transitions #
Backpropagation through time (BPTT) unfolds the computation graph and accumulates parameter gradients from every use of the shared transition. The influence of state on a later state contains
Products of matrices can contract most directions toward zero or expand some directions rapidly. A vanishing long-range contribution means the loss cannot tell an early transition how it should change. An exploding contribution produces a steep update dominated by a few directions and may make the loss non-finite.
The spectral radius of alone is not a universal test: nonlinear derivatives and the visited states also enter every Jacobian. It is nevertheless a useful linearized intuition. Saturated sigmoid or tanh units multiply by small derivatives; a transition with an expanding singular direction can amplify errors.
Global-norm clipping rescales a gradient when :
This preserves direction while limiting one update's magnitude. It contains exploding gradients; it does not recover a long-range signal that already vanished, and repeated clipping is a diagnostic that the dynamics or learning rate need inspection.
Truncated BPTT detaches the graph after a finite number of steps. State may continue forward beyond that boundary, but credit cannot travel backward through it. “The model saw 10,000 steps” is therefore not equivalent to “the loss trained a 10,000-step dependency.”
LSTM builds a gated additive state path #
One common LSTM formulation computes
The cell update is additive. The forget gate controls retention, the input gate controls writing, and the output gate controls exposure of the stored state. Along the direct cell path,
before accounting for indirect recurrent dependencies. When relevant forget gates remain near one, information and gradient can persist without repeatedly crossing a saturating candidate-state nonlinearity. This makes long time scales learnable; it does not make memory unlimited or eliminate explosions elsewhere in the graph.
GRU merges the storage and exposed state #
A gated recurrent unit (GRU) uses an update gate and reset gate :
The interpolation supplies an additive carry path without a separate cell/output pair. LSTM versus GRU is not a general quality ranking. Compare validation quality, latency, memory, state behavior, and run-to-run variation under the same data and budget.
Encoder–decoder changes the sequence boundary #
An encoder can consume and initialize a decoder that factorizes
The original fixed-vector sequence-to-sequence formulation compressed the source into the encoder's final state. Attention later provided a direct, content-dependent path to all encoder states and relieved that bottleneck. The historical fixed-vector model remains useful for understanding conditioning and autoregressive decoding, but it is not a default current architecture for high-capacity translation.
During training, teacher forcing supplies the true previous target to predict the next one. During generation, the model consumes its own sampled or selected output. That train–inference mismatch is exposure bias: early errors move the decoder into prefixes absent from training. Beam search changes the search approximation, not the learned probability model, and a larger beam can prefer short sequences unless length treatment is explicit.
CTC is a different contract for unsegmented inputs: it sums over monotonic alignments containing blanks and repeated labels. It suits speech, handwriting, and other ordered alignments, but cannot represent arbitrary output reordering.
State handling is part of model correctness #
- Reset state between independent sequences; otherwise one user's or recording's tail contaminates the next.
- When carrying state across batches, preserve sequence identity and detach it at the intended BPTT boundary.
- Mask padded timesteps in both loss and recurrence when padding would otherwise update state.
- Never deploy a bidirectional or unmasked model when future inputs are unavailable.
- Define how state survives gaps, reconnects, missing samples, and changes of sampling rate.
- Evaluate by dependency length and streaming conditions, not only aggregate accuracy.
Useful diagnostics include gradient-norm distributions, clipping frequency, gate saturation, performance versus sequence length, perturbation of early inputs, hidden-state drift across long blank spans, and explicit reset/no-reset tests. The right question is not whether an architecture “has memory,” but which information path carries a particular dependency and whether training provides usable credit along that path.