LSTM and GRU Networks
Gated recurrent networks that keep a protected memory line running through time, with small learned valves deciding what to erase, what to add, and what to reveal. They are the standard fix for a plain RNN forgetting anything that happened more than a handful of steps ago.
Prerequisites: Recurrent Neural Networks, Backpropagation
A plain recurrent network is supposed to carry information forward through time: read one observation, update a summary of everything seen so far, read the next, update again. In practice it forgets almost immediately. Feed it a hundred minutes of order-flow data and by minute twenty the trace of minute one has been multiplied away to nothing. LSTM and GRU cells are the engineering answer to that failure, and they remain the default when you want a sequence model that fits on a laptop.
The analogy: a notebook with valves
Picture a conveyor belt running left to right through the whole sequence. On the belt sits a small notebook holding whatever the model currently believes is worth remembering. At every time step the belt passes three valves:
- an erase valve, which decides how much of the existing note to rub out,
- a write valve, which decides how much of a freshly drafted note to add,
- a read valve, which decides how much of the resulting note to show to the rest of the network right now.
Nothing forces the belt to be rewritten. If the erase valve stays shut, the note travels untouched across a hundred steps. A plain recurrent network has no belt: it copies the note through a squashing function every step, and copying degrades. That single structural difference is the whole idea.
Putting numbers on the valves
Write for the note on the belt (the cell state) and for what the cell shows the outside world (the hidden state). At each step the network sees the new observation and the previous output , and computes three valve openings:
In words: each valve is a small linear score of "what I just saw plus what I was thinking", squashed by the logistic function into a number between 0 and 1 — zero fully shut, one fully open. The weights , and biases are learned like any others.
It also drafts a candidate note, using to keep the draft between and :
Then the two updates that matter:
In words: keep an fraction of the old note, add an fraction of the new draft, reveal an fraction of the result. The symbol means elementwise multiply — each memory slot gets its own independent valve.
Every valve is a logistic curve. Drag the steepness below and watch how fast it goes from "shut" to "open" — that sharpness is what lets a gate behave like a switch rather than a dimmer.
The cell state is only ever multiplied by a gate and added to, never pushed through a squashing function. That is the entire trick: multiplication by a number near 1 preserves information across time, while repeated squashing destroys it.
Worked example: one step, by hand
Take one memory slot carrying , meaning "a strong uptrend is in force". A new bar arrives and the linear layers produce , , , . Squash each:
Now the belt update:
The cell kept 90 percent of what it had and topped it up. The output:
It believes the trend slightly more than before, but exposes only about 44 percent of that belief downstream. Now suppose the next bar looks like a regime break and drives the erase score to , so . The same arithmetic gives : the old belief is almost entirely wiped in one step. One learned number decides between "hold for a hundred bars" and "forget everything now".
Worked example: how much gradient survives
Here is why gates fix training. In a plain recurrent network the gradient flowing back through steps gets multiplied by roughly the same recurrent weight each time. Take that factor to be :
After fifty steps the learning signal has been divided by a hundred billion — indistinguishable from zero in 32-bit arithmetic. Its half-life is steps.
Along an LSTM cell state the multiplier is the erase gate instead. With :
Nearly eight percent survives fifty steps — a half-life of 13.5 steps, and about ten billion times more gradient than the plain network. Nothing exotic happened: 0.95 is simply much closer to 1 than 0.6 is, and the network can learn to keep it there.
The GRU: the same idea with fewer parts
The gated recurrent unit merges the erase and write valves. Whatever it does not keep, it writes:
In words: is a single dial between "keep the old state" and "take the new one", and the two sides always sum to one. A second valve, the reset gate , controls how much of the old state may influence the draft . There is no separate cell state; does both jobs.
That is three weight blocks instead of four — roughly 25 percent fewer parameters and a faster step. On financial sequences, which are short, noisy and data-poor, the GRU usually matches the LSTM and sometimes beats it precisely because it has less to overfit.
What this means in practice
On a desk these cells appear wherever you need a state rather than a snapshot: a volatility regime that persists, an inventory that accumulates, an order-book imbalance that builds. Three practical points matter more than the architecture choice:
- Initialise the forget bias positive. Setting starts every gate near , so the cell remembers by default and learns to forget. Starting at zero starts training from amnesia, and it often never recovers.
- Clip the gradients. Gates fix vanishing but not exploding — see Vanishing and Exploding Gradients. A global-norm clip at 1 costs nothing and stops one outlier bar wrecking the weights.
- Respect the time ordering. Shuffling whole sequences is fine; shuffling within one is not, and neither is a scaler fitted on the full history. Standardise on past data only, or you have built a look-ahead.
A gate is a learned number, not a guarantee of memory. If nothing in the training signal rewards remembering across a long gap, the network learns small forget gates and behaves exactly like the plain RNN it replaced. "I used an LSTM, so long-range dependence is handled" is the classic mistake — check the learned gate values first. And do not confuse the two states: is the protected memory, is only the part the read valve chose to expose.
Log the average forget gate of a trained model. Values near 1 mean it is genuinely carrying state; values near 0.5 mean its effective memory is a couple of steps and a far simpler model would do the same job.
Gated cells remain the sensible default at small data scale. Their one structural limit is that information still travels step by step along the belt — exactly the constraint the transformer removes by letting every position look at every other in a single hop.
Related concepts
Practice in interviews
Further reading
- Hochreiter & Schmidhuber, Long Short-Term Memory (1997)
- Cho et al., Learning Phrase Representations using RNN Encoder-Decoder (2014)
- Goodfellow, Bengio & Courville, Deep Learning (ch. 10)