AI State of the Art
Fundamentals/Deep Learning: Fundamental Architectures/31 min
Section 21

LSTM and GRU: Solving the Vanishing Gradient

Listen as a podcast β†’

The Critical Problem of Basic RNNs

A basic Recurrent Neural Network (RNN, see RNN: sequences and memory) has the memory of a goldfish: past a few dozen tokens (the unit the model manipulates, a word or a piece of a word) it has forgotten practically everything. The cause is the vanishing gradient: when you backpropagate the error through many time steps, the gradient is multiplied by the same weights over and over. If that factor is below 1, the gradient shrinks exponentially and eventually becomes nearly zero. Result: the network gets no usable signal about distant elements, so it can't learn long-term dependencies.

Concretely, in the sentence "The cat that crossed the street last night while it was raining is very hungry", a classic RNN has forgotten "cat" by the time it processes "hungry". It can't learn that it's the cat that is hungry.

Diagram of an LSTM cell with information flow
The repeating module in a standard RNN contains a single layer: one tanh mixing the current input with the previous state. Everything has to pass through it, which is why distant information gets diluted.Source : Christopher Olah, colah's blog

Two architectures solve this with the same mechanism: the LSTM (Long Short-Term Memory) and the GRU (Gated Recurrent Unit). The mechanism is gates: small learned controllers that decide, at each time step, what to keep in memory, what to forget, and what to add. Picture valves on an information pipe, opening and closing automatically according to context.

Where these architectures stand in 2026

The LSTM and the GRU dominated sequence processing until 2017, when the transformer replaced them for language, the comparison is made in Transformer vs RNN. They are still used on modest time series and in embedded systems, where their small size is an advantage. And the idea of learned control over information flow survived everywhere: you find it again in state space models such as Mamba.

LSTM: Long Short-Term Memory

The repeating module in an LSTM contains four interacting layers: three sigmoid gates and one tanh layer, with the horizontal cell-state line running right through the module.
The same module, LSTM version: four layers instead of one. Note the horizontal line running across the top of the diagram, that is the cell state, the highway along which information travels almost untouched. The three sigmoid gates only decide what gets added to it and what gets taken out.Source : Christopher Olah, colah's blog

Origins

Invented in 1997 by Hochreiter and Schmidhuber, the LSTM had to wait for graphics cards usable for training and for the large datasets of the early 2010s to show what it was worth. From roughly 2010 to 2017 it was the dominant architecture for sequences: machine translation, speech recognition, text generation, time series analysis.

The Architecture: Two States, Three Gates

The key to the LSTM is the separation between two states: the cell state CtC_t and the hidden state hth_t.

The cell state CtC_t is the true heart of the architecture. Think of it as a conveyor belt that runs through the network from end to end. Information flows through it via simple addition operations, without being multiplied by complex weight matrices at each step. Direct consequence: the gradient can flow on this conveyor belt without attenuating. It's mathematically more stable than the classic RNN, and that's exactly what solves the vanishing gradient.

The hidden state hth_t is what the LSTM "communicates" to the outside world, it's the visible output of the cell at each time step.

Three gates control how the cell state is modified.

How to read the formulas that follow

The five formulas below are all built from the same mould. Once these five symbols are clear, they read themselves.

  • Οƒ\sigma (the Greek letter sigma) is the sigmoid function. It takes any number and squashes it between 0 and 1. That is what makes it a gate: the output reads as how far a tap is open. 0 = shut, nothing passes; 1 = wide open, everything passes; 0.3 = 30% gets through.
  • [htβˆ’1,xt][h_{t-1}, x_t] is a concatenation: the previous hidden state vector and the current input vector laid end to end into one longer vector. It is just a compact way of writing "the gate looks at both what we remember and what is arriving". Writing Wfβ‹…[htβˆ’1,xt]W_f \cdot [h_{t-1}, x_t] and writing Wfhβ‹…htβˆ’1+Wfxβ‹…xtW_{fh} \cdot h_{t-1} + W_{fx} \cdot x_t are exactly the same thing: in the first case the big matrix WfW_f holds the two blocks side by side. Both notations are in circulation; they are not two different mechanisms.
  • βŠ™\odot is the element-wise product (Hadamard product): multiply the first slot of one vector by the first slot of the other, the second by the second, and so on. It is not a matrix product. This is how a gate "filters" a vector slot by slot, each dimension of the memory being controlled independently.
  • The WWs (with their subscripts: WfW_f, WiW_i, WCW_C, WoW_o) and the bbs are the learned parameters: tuned during training, then frozen. They are the same at every time step and for every sentence.
  • The ftf_t, iti_t, oto_t, C~t\tilde{C}_t, CtC_t, hth_t are computed values: recomputed at each time step, on the sequence at hand, and then discarded. They are results, not settings of the model.

That last distinction is the expensive one to miss: the forget gate is not a weight, it is a number recomputed at every word by a small calculation whose weights are what got learned.

Forget Gate

ft=Οƒ(Wfβ‹…[htβˆ’1,xt]+bf)f_t = \sigma(W_f \cdot [h_{t-1}, x_t] + b_f)

This gate answers the question: "what to delete from memory?" It takes the previous hidden state htβˆ’1h_{t-1} and the current input xtx_t, and produces ftf_t, a vector of values between 0 and 1, one per dimension of the memory. Close to 0 = forget this information. Close to 1 = keep it intact.

Concrete example: in a language model, when the subject of the sentence changes ("And now, let's talk about Mary..."), the forget gate erases information related to the old subject (its gender, number, properties) to make room for the new one.

Input Gate

it=Οƒ(Wiβ‹…[htβˆ’1,xt]+bi)i_t = \sigma(W_i \cdot [h_{t-1}, x_t] + b_i) C~t=tanh⁑(WCβ‹…[htβˆ’1,xt]+bC)\tilde{C}_t = \tanh(W_C \cdot [h_{t-1}, x_t] + b_C)

This gate answers: "what new information to add?" Two calculations work together, and it matters not to conflate them. iti_t (the gate proper, a sigmoid) decides which dimensions to update and by how much: values between 0 and 1. C~t\tilde{C}_t (the candidate values, a tanh so between -1 and 1) proposes the content to write; the tilde marks it as a proposal, not yet actual memory. The tanh allows negative values here, so content can be subtracted from memory, not only added. The element-wise product itβŠ™C~ti_t \odot \tilde{C}_t combines the two: "here is what we propose to write, and here is how strongly we write it".

Cell Update

Ct=ftβŠ™Ctβˆ’1+itβŠ™C~tC_t = f_t \odot C_{t-1} + i_t \odot \tilde{C}_t

This is where it all happens. In one sentence: the new memory is the old memory with whatever the forget gate designates erased, plus the new content in whatever proportion the input gate allows.

The remarkable part is what is not in this formula. The cell state Ctβˆ’1C_{t-1} is not multiplied by a weight matrix and passes through no activation function: it is merely multiplied by ftf_t, a number between 0 and 1, and added to something else. It's precisely this simplicity that allows the gradient to flow without vanishing: when ftf_t is close to 1, it crosses this time step intact.

Output Gate

ot=Οƒ(Woβ‹…[htβˆ’1,xt]+bo)o_t = \sigma(W_o \cdot [h_{t-1}, x_t] + b_o) ht=otβŠ™tanh⁑(Ct)h_t = o_t \odot \tanh(C_t)

Final step: "what to reveal to the outside world?" The cell state potentially contains a lot of accumulated information, but not all of it is relevant at the current time step. First CtC_t goes through a tanh to bring it back between -1 and 1, then the output gate oto_t filters it dimension by dimension. The result is hth_t, the hidden state, the only thing subsequent layers will see. The full memory stays private inside the cell.

Why this saves gradients

During backpropagation, the gradient crosses the cell state multiplied by the forget gate. If ftβ‰ˆ1f_t \approx 1, the gradient passes intact, no attenuation. If ftβ‰ˆ0f_t \approx 0, the gradient vanishes, but that is then a deliberate, learned choice: the network has decided this information should no longer influence what follows. That is the whole difference. In a basic RNN, attenuation is an accident of weight magnitude, suffered and uncontrollable; in an LSTM it is a decision the network makes word by word.

GRU: Gated Recurrent Unit

Diagram of a GRU cell
This diagram illustrates how a GRU (Gated Recurrent Unit) cell works, a simplified type of recurrent neural network compared to LSTM. It shows how the reset gate and update gate control information flow to compute the hidden state.Source : Dive into Deep Learning

The Simplified Version

Proposed in 2014 by Cho and colleagues, the GRU simplifies the LSTM elegantly: 2 gates instead of 3, and no separate cell state. Everything goes through a single hidden state hth_t.

Where does the "about 25% fewer parameters" figure come from? Count it directly. At each time step an LSTM computes four things from [htβˆ’1,xt][h_{t-1}, x_t]: three gates plus the candidate content, so four weight matrices of the same size. A GRU computes three: two gates plus the candidate. Three out of four is a quarter fewer. Hence also faster training, for often comparable performance.

Update Gate

zt=Οƒ(Wzβ‹…[htβˆ’1,xt]+bz)z_t = \sigma(W_z \cdot [h_{t-1}, x_t] + b_z)

The update gate combines the roles of the LSTM's forget gate AND input gate into a single gate. It simultaneously answers: "how much of the past to keep?" and "how much new to integrate?" When ztz_t is close to 0, we keep the old state. When ztz_t is close to 1, we replace it with new content.

Reset Gate

rt=Οƒ(Wrβ‹…[htβˆ’1,xt]+br)r_t = \sigma(W_r \cdot [h_{t-1}, x_t] + b_r)

This gate controls the influence of the past on the candidate. If rβ‰ˆ0r \approx 0, the network "resets" by completely ignoring the previous state to compute the new candidate. It starts from scratch, as if the sequence began at this instant. If rβ‰ˆ1r \approx 1, the past is fully taken into account.

State Update

h~t=tanh⁑(Wβ‹…[rtβŠ™htβˆ’1,xt]+b)\tilde{h}_t = \tanh(W \cdot [r_t \odot h_{t-1}, x_t] + b) ht=(1βˆ’zt)βŠ™htβˆ’1+ztβŠ™h~th_t = (1 - z_t) \odot h_{t-1} + z_t \odot \tilde{h}_t

Two new symbols here. h~t\tilde{h}_t is the candidate state: what the hidden state would be if you chose to renew everything. And rtβŠ™htβˆ’1r_t \odot h_{t-1} shows where the reset gate acts: it filters the past before it is used to compute that candidate, without touching the past itself. The WW and bb without subscripts on the first line are, like the others, learned parameters.

The GRU's intuition is crystal clear: it's a slider. The slider ztz_t moves between "keep all the past" (zt=0z_t = 0, and then 1βˆ’zt=11 - z_t = 1, so ht=htβˆ’1h_t = h_{t-1}) and "replace everything with new" (zt=1z_t = 1, and then ht=h~th_t = \tilde{h}_t). The elegance of the design is that the two coefficients (1βˆ’zt)(1 - z_t) and ztz_t always sum to 1: the more you keep, the less you replace. That constraint is what makes the GRU simpler and more stable than two independent gates, where nothing would stop the network from keeping everything and replacing everything at once.

Quick intuition

GRU = a single trade-off per dimension: "do I keep the past or do I replace it?" Simpler to understand, and often enough.

LSTM vs GRU: Who Wins?

AspectLSTMGRU
Gates3 (forget, input, output)2 (update, reset)
States2 (CtC_t and hth_t)1 (hth_t only)
ParametersMore~25% fewer
Training speedSlowerFaster
Long-term memoryVery goodVery good (comparable in practice)

The Empirical Truth

Surprise: across most published comparisons, performance is nearly identical. Neither wins systematically.

  • GRU better suited when you have little data (fewer parameters = less overfitting risk) or when training speed matters
  • LSTM better suited when you have lots of data to train the extra parameters
  • Practical recommendation: test both, choose whichever gives the best results on your validation data. Start with GRU, faster to iterate
Explored variants

Many variants have been proposed: peephole LSTM (gates look directly at the cell state), coupled LSTM (forget and input gates are tied together as in a GRU), and others. Greff and colleagues compared eight variants systematically in 2015. Their conclusion, in their own terms: none of them improves significantly upon the standard LSTM, and the two most critical components are the forget gate and the output activation function. The standard LSTM therefore remains the most robust choice.

LSTM: A Search Space Odyssey (Greff et al., 2015)

Practical Usage

Stacking Layers

In practice, you often stack 2 to 4 layers of LSTM or GRU. The output of layer 1 at each time step becomes the input to layer 2, and so on. It's like stacking levels of abstraction: layer 1 captures local patterns, layer 2 captures higher-level patterns. Beyond 4 layers, gains are generally small and the risk of overfitting increases.

Bidirectionality

BiLSTMs and BiGRUs (the "Bi" prefix for bidirectional) run two networks in parallel: one that reads the sequence left to right, the other right to left. Both outputs are laid end to end. This is particularly effective for text classification, NER (Named Entity Recognition: spotting names of people, places and organizations), and any task where the entire sequence is available at prediction time. Warning: this doesn't apply to text generation, since you don't know the future.

Regularization

  • Dropout (randomly switching off connections during training): applied between stacked layers, but not between time steps within the same layer, which would break memory continuity
  • Variational dropout: uses the same switch-off mask at every time step of a given sequence, preserving temporal coherence
  • L2 regularization: adds a penalty proportional to the squared weights to the loss, pushing the network toward small weights and limiting overfitting
A lasting legacy

The transformer replaced the LSTM and the GRU for language processing from 2018 onward (see Transformer vs RNN). But the gating idea survives everywhere: in state space models such as Mamba, in gated MLPs (Multi-Layer Perceptrons, see The perceptron), and in several transformer variants. The LSTM and the GRU lost the architectural battle, but their principle won the war.

Summary

The LSTM and the GRU are RNNs given a memory controlled by gates. Those gates learn what to keep and what to forget, which solves the vanishing gradient that paralyzed basic RNNs: memory travels along a path where it is only multiplied by a number between 0 and 1 and added to, never transformed by a weight matrix. The LSTM separates cell state and hidden state with 3 gates; the GRU simplifies to 2 gates and a single state, for comparable performance. Even though the transformer dominates today, these two architectures established a result that held: a network can learn to regulate what it retains.

Check your understanding0 / 5

1. Is the LSTM forget gate f_t a weight of the model, learned during training?

2. What is it about the LSTM cell-state update that solves the vanishing gradient?

3. In a GRU, what exactly does the reset gate r_t do?

4. LSTM or GRU: which one gives better results?

5. Where do the LSTM and GRU stand in 2026?