LSTMs & GRUs
Theme
Palette
Download
Caption (tap to copy)
📖 Deep dive (full written explanation)
This post answers the 'so what.' Knowing that LSTMs and GRUs add gates to an RNN is only useful once you understand why that change was a genuine turning point — the difference between recurrence as an academic curiosity and recurrence as the engine behind real translation, speech, and text systems.
The through-line is memory that survives. Vanilla RNNs could read sequences but couldn't hold information long enough to use it. Gating fixed that, and in doing so it unlocked an entire decade of practical sequence modeling.
The motivating problem is the fatal RNN flaw covered on Day 45: vanishing gradients. Because a vanilla RNN multiplies its gradient by the same weight matrix at every step during backpropagation through time, the signal reaching distant steps shrinks toward zero. The model literally cannot learn dependencies that span more than a handful of steps.
Gating fixes this structurally. The LSTM's cell state is updated by addition along a near-uninterrupted path, so both information and gradient survive across hundreds of steps. This is not a tuning trick or a bigger model — it is an architectural change that turned recurrence from a toy into a workhorse, which is why it deserves its own day.
Long-range memory is not an abstract nicety; it is the prerequisite for real tasks. A translation system must remember the subject of a clause to conjugate a verb many words later. A speech recognizer must carry acoustic context across a phrase. A code model must match an opening brace to its close. Each requires information from far back in the sequence.
Vanilla RNNs failed at all of these because the relevant context had already faded. Gated cells could hold it, which is precisely why problems that seemed stuck suddenly became solvable at production quality once LSTMs and GRUs were adopted. The capability and the applications are tightly linked.
The timeline puts the cells in historical context. The LSTM was introduced in 1997 by Hochreiter and Schmidhuber, well ahead of the hardware and data that would make it shine. The GRU arrived in 2014 alongside the sequence-to-sequence framework that powered a translation breakthrough. By 2016 Google's Neural Machine Translation system, built on stacked LSTMs, was in production. Then in 2017 attention and the Transformer began displacing recurrence.
Seeing the dates matters: gated RNNs were not a brief fad but the dominant approach to sequences for roughly a decade. They are the bridge between the toy RNNs of the early days and the attention-based models that followed, and understanding them is understanding how modern NLP got here.
The LSTM-versus-GRU choice is the most practical decision this day teaches. Reach for an LSTM when dependencies are very long, when you need maximum robustness, and when you have enough data and compute to train its larger parameter count — the separate long-term cell state genuinely helps in the hardest cases.
Reach for a GRU when you want fewer parameters and faster training, when your dataset is smaller (so a heavier model would overfit), or when latency and memory are tight. On a large fraction of real tasks the two are within noise of each other, so the GRU's efficiency wins by default. The skill is resisting the reflex to always grab the 'bigger' cell.
The products slide grounds the abstract argument in things people actually used. Before Transformers, gated RNNs were the backbone of Google's neural machine translation, of speech recognition on phones and assistants, of Gmail's Smart Reply suggestions, and of countless time-series forecasting systems in finance and operations.
For several years, 'state of the art on a sequence task' essentially meant 'a carefully tuned LSTM.' Recognizing this scale of real-world deployment is what separates treating LSTMs as a textbook curiosity from understanding them as the production technology that carried sequence modeling until attention matured.
Honesty about limits keeps the post credible and sets up the rest of the series. Gated cells still process time steps strictly in order, so they cannot be parallelized across the time dimension — training on long sequences is slow because step t must finish before step t+1 begins. They ease the vanishing-gradient problem but do not fully eliminate it; very long dependencies still degrade.
Transformers later addressed both weaknesses by replacing recurrence with attention, which connects any two positions directly and computes over the whole sequence in parallel. Framing gated RNNs as a major leap rather than the final answer is the accurate historical picture, and it motivates the attention material that comes next in the series.
The comparison crystallizes the tradeoff against Transformers. Gated RNNs run sequentially in O(T) steps, use constant memory per step, excel at streaming and low-latency settings, and reach long but not unlimited distances. Transformers process the whole sequence in parallel and link any two positions directly, giving them superior long-range reach, but they pay an O(T squared) attention cost and a heavier memory footprint.
The takeaway is that neither dominates universally. Sequence length, latency requirements, data volume, and hardware budget all push the decision one way or the other — which is exactly why gated RNNs remain relevant even now that attention is dominant.
This code example shows the pattern that gated cells unlocked: sequence-to-sequence. An encoder LSTM reads the source sequence and compresses it into its final state — a context vector summarizing the whole input. A decoder LSTM is then initialized with that state and generates the output conditioned on it.
The key idea is that the encoder's final (h, c) becomes the decoder's starting memory, threading meaning from input to output. This encode-then-decode pattern was the foundation of neural machine translation and, when later augmented with attention, became the direct ancestor of the Transformer. Seeing it in a few lines makes clear how gated memory translated into a real architecture.
Where gated RNNs still earn their keep is the practical, slightly contrarian closing point. Despite Transformers winning the headlines, LSTMs and GRUs remain strong wherever sequences are long but each step is cheap, where data arrives as a real-time stream, or where compute and memory are constrained.
Concretely that means on-device keyword spotting and wake-word detection, embedded sensor processing, low-latency forecasting, and small-data settings where a large Transformer would simply overfit. The constant per-step memory and natural streaming behavior of recurrent cells are genuine advantages there. The lesson is to match the architecture to the constraints rather than reflexively reaching for the largest model.
This recap gathers the argument into five points: gating fixed the vanishing-gradient flaw; that made long-range memory practical; the cells powered translation, speech, and smart reply; GRUs are lighter while LSTMs are more robust; and they still win for streaming, on-device, and small-data work.
Together these justify why gated RNNs mattered historically and why they still have a place. With the motivation established, the next post opens the cell and traces every gate.
The teaser sets up the mechanics post. With the motivation in hand, the next step is to open the cell and trace each gate, the additive cell-state update, and the precise reason the gradient survives where a vanilla RNN's would vanish. That derivation is where the intuition built here becomes concrete math.