LSTM & GRU
A plain recurrent network propagates its hidden state through a repeated weight-matrix multiply, and the Jacobian product that results vanishes or explodes long before a useful gradient can reach the early steps. Gated RNNs fix this with an additive memory path: a cell state that is carried forward almost unchanged, past which the gradient flows along a near-identity highway.
╌╌╌╌
A recurrent network folds a sequence into a single hidden state by applying the same transition at every step,
The defect is structural, not a tuning problem: information from step reaches step only by passing through multiplications by . Training backpropagates the loss along that same chain, and the chain's Jacobian is a product of matrices. Products of matrices do not stay bounded; they collapse to zero or blow up, so the gradient linking step to a much later target is effectively lost.1 This lesson builds the standard fix: a gated, additive memory path whose Jacobian is near the identity.
Why gating: the vanishing Jacobian product
Differentiate the plain recurrence. Write so that , and let be the diagonal Jacobian of the at step . One step back is
and the loss at the final step depends on the early state only through the long chain of these factors:
The whole problem lives in that product of matrices. Diagonalize and ignore the bounded factors for the magnitude argument: the product scales like , so each eigenvalue is raised to the power .
As a scalar caricature: with a single number and , the factor is : at over steps it is ; at it is . Either way the signal linking distant steps is destroyed.
| Quantity | Plain RNN | What goes wrong |
|---|---|---|
| state update | multiplicative, nonlinear | |
| one-step Jacobian | repeated factor | |
| -step Jacobian | eigenvalues raised to a power | |
| long-range gradient | vanishes or explodes |
The fix is to replace the multiplicative carry with an additive one. If the state is updated by adding rather than multiplying, the one-step Jacobian becomes (near) the identity, and a product of identities neither shrinks nor grows. That additive carry is the cell state, and the gates are what decide, per coordinate and per step, how much to add and how much to keep.
The LSTM cell
The long short-term memory (LSTM) cell threads a cell state through time along a near-uninterrupted highway, and surrounds it with three multiplicative gates — each a sigmoid-squashed linear map producing values in that act as soft, per-coordinate switches.2 Writing for the concatenation of the previous hidden state and the current input, the cell is six equations:
Here is the elementwise (Hadamard) product and the logistic sigmoid. The single load-bearing line is the cell update: the old cell survives, scaled by the forget gate , and a candidate is added in, scaled by the input gate . The structure is the signature diagram below.
Fix the dimensions once and every shape follows. Let the input be and the hidden and cell states be . The concatenation stacks the two. Every gate and the candidate read this same vector through its own weight block, so each weight matrix maps and each bias lives in :
Every intermediate — , , , , , — is a vector in , and the three products , , are elementwise, so shapes never leave after the concatenation is consumed. In practice the four blocks are stacked into one matrix and applied in a single matmul, then sliced into the four pre-activations — one dense layer of width does the whole gate computation.
A count follows immediately. Each of the four blocks holds weights and biases, so an LSTM layer has
parameters. For inputs and hidden units that is weights — dominated by the recurrent term, which is why hidden width, not input width, sets an LSTM's cost.
The structure is the signature diagram below.
The three gates differ only in what they multiply, not in form. Each is a learned, input-dependent decision applied elementwise to one signal in the cell.
| Gate | Formula | Role |
|---|---|---|
| forget | how much of to keep | |
| input | how much of the candidate to write | |
| candidate | the new content proposed for the cell | |
| output | how much of to expose as |
For a worked step, take a two-dimensional cell, , carried state , and suppose the gates and candidate come out as
The cell update runs coordinate by coordinate:
The first coordinate kept almost all of its old value () and added a little; the second nearly forgot its old value () and was overwritten by the candidate. The read-out then squashes and gates:
Two coordinates, two independent decisions — this is the per-coordinate control the gates provide.
The forward pass is one sweep of these six lines across the sequence, carrying from step to step.
- 1forget gate
- 2input gate
- 3candidate cell
- 4additive cell update
- 5output gate
- 6read-out
- 7return
The cell-state highway
The reason gating works is one derivative. Differentiate the cell update with respect to . The second term does not contain , and to first order the gates are functions of , not of directly, so
There is no weight matrix in this Jacobian, no repeated to raise to a power. Backpropagating the cell state across many steps multiplies these diagonal gate factors:
When the forget gates stay open, , the product is : the gradient passes through unattenuated, no matter how large is. This is the constant error carousel made precise.3
The contrast is the whole point of the architecture: two paths from back to , one near-identity and one decaying.
A practical corollary: LSTM forget-gate biases are initialized positive (often ), so that or higher at the start of training: the highway begins open, and the cell remembers by default until the network learns when to forget.
Gate behavior
The gate is a soft switch. Because it is a sigmoid, its value slides continuously in , but the two ends are what carry meaning: near it blocks its signal, near it passes it. Applied to the forget gate, this is precisely the choice between erasing and preserving a memory.
The gate values are not hand-set; each is the output of a learned linear map of , so the network discovers from data when a coordinate of the cell should be held, overwritten, or read out.4
The GRU cell
The gated recurrent unit (GRU) is a streamlined gating with two gates instead of three and a single state vector: it drops the separate cell state, letting serve as both memory and output.5 An update gate interpolates between carrying the old state and writing a candidate; a reset gate controls how much past state enters that candidate:
The convex combination in the last line is the GRU's highway: when the state is copied forward, , giving the same near-identity carry as the LSTM cell. A single gate does the job the LSTM splits between forget and input — here keep and write are tied, .
The dimensions mirror the LSTM with one block fewer. With and , the three weight blocks each map the concatenation into :
The candidate reads rather than — the reset gate masks the past before it enters — but the shape is unchanged, still . Three blocks instead of four gives
parameters, exactly three-quarters of the LSTM's . For the same , that is weights against the LSTM's .
The update-gate interpolation reads like the LSTM's cell update with the two gates fused. Take again, , and
Then
The first coordinate mostly carried its old value (, so on the past); the second mostly rewrote to the candidate (). The single gate must trade keeping against writing — the LSTM, with separate and , can keep and write the same coordinate, or do neither.
GRU and LSTM share the additive-highway principle; they differ in bookkeeping. The GRU has fewer parameters (three weight blocks rather than four) and one state to carry, which makes it lighter and often faster to train; the LSTM's separate cell and explicit output gate give it a finer-grained, sometimes stronger memory.
| LSTM | GRU | |
|---|---|---|
| gates | forget, input, output (3) | update, reset (2) |
| state | cell + hidden | single |
| highway | ||
| weight blocks | (4) | (3) |
| params (state dim , input dim ) | ||
| output exposure | gated read-out | full state |
| when to use | long, complex dependencies; capacity to spare | smaller data / compute; speed matters |
Persistence across time
Unrolled, both architectures are the same cell applied at every step, with the memory carried along the top from start to finish. The forget/update gate is what lets a coordinate survive intact across the whole span while the input rewrites others — the network holds some facts and revises the rest.
This is the architectural payoff. The plain RNN's hidden state was the only channel, and it decayed; the gated cell adds a second channel whose default behavior is to remember, and whose forgetting is a learned operation rather than an unavoidable side effect of repeated matrix multiplication. Gated RNNs carried most sequence modeling for years, until the parallelism and longer effective context of attention and transformers displaced recurrence for the largest models.
Which gates earn their keep
The LSTM in Goodfellow's Chapter 10 is the 1997 Hochreiter–Schmidhuber cell plus the forget gate (Gers et al., 2000). A few results since then sharpen which parts of it actually matter.
- Which gates matter. Greff et al.,
LSTM: A Search Space Odyssey
(IEEE TNNLS, 2017), ablated every component across thousands of runs. The finding is blunt: the forget gate and the output activation are essential — removing either collapses performance — while peephole connections and the input-gate/forget- gate coupling have little effect. This is the empirical justification for the GRU's design: it keeps the forget-like update gate and drops nearly everything the study found expendable. - The highway in numbers. With the forget gate held at , the cell-state Jacobian across steps is — the gradient shrinks by less than an order of magnitude. Compare the plain RNN from the recurrent-networks lesson, where gave . The gate's job is to keep that per-step multiplier as close to as the task allows; at the product is exactly and memory is perfect.
- Normalization inside the cell. Batch normalization does not fit a recurrence (statistics differ per step), but layer normalization applied to the gate pre-activations (Ba et al., 2016) stabilizes LSTM training and became standard for deep recurrent stacks — the same per-token normalization that later anchors the Transformer block.
The modern verdict is mixed rather than a clean replacement. The
Transformer won the
largest models on parallelism, but gated recurrence never fully left: it remains the
default for strict streaming and low-latency inference (constant memory per step, no
growing context), and recent work such as the xLSTM
(Beck et al., 2024) revisits
the cell with matrix memory and parallel training, so the additive-highway idea is
still live.
Takeaways
- The plain RNN propagates state through a repeated weight-matrix multiply; the backward Jacobian is a product that vanishes () or explodes (), killing long-range gradients.
- Gating replaces the multiplicative carry with an additive one. The LSTM cell update has Jacobian — no weight matrix, so with the gradient rides a near-identity highway.
- The LSTM has three gates (forget, input, output) and two state vectors (cell , hidden ); the GRU merges these into two gates (update, reset) and one state, with fewer parameters and a tied keep/write.
- Both are the same cell unrolled across time; the forget/update gate is the learned switch that lets a memory persist intact or be overwritten, step by step.
Footnotes
- Goodfellow, Deep Learning, §10.7 — The Challenge of Long-Term Dependencies: the repeated weight multiply whose Jacobian product vanishes or explodes, the failure gating is built to fix. ↩
- Goodfellow, Deep Learning, §10.10 — The Long Short-Term Memory and Other Gated RNNs: the cell state and the three sigmoid gates (forget, input, output) of the LSTM. ↩
- Goodfellow, Deep Learning, §10.10.1 — LSTM: the self-loop on the cell state (the
constant error carousel
) whose forget-gated Jacobian stays near the identity. ↩ - Chollet, Deep Learning with Python, §6.2 — Understanding Recurrent Neural Networks: a practitioner's reading of LSTM/GRU gates as learned, data-driven carry/write/read switches in Keras. ↩
- Goodfellow, Deep Learning, §10.10.2 — Other Gated RNNs: the GRU's single update gate that merges the LSTM's forget and input gates onto one state vector. ↩
╌╌ END ╌╌