LSTM and GRU

LSTMs and GRUs are gated variants of recurrent neural networks. A vanilla RNN keeps one hidden state and repeatedly overwrites it. LSTMs and GRUs add learned gates so the model can preserve, erase, or expose information over time instead of forcing every new input through the same update.

LSTM and GRU gating diagram

This matters for temporal learning. A sequence model may need to remember a negation word, a market regime, a sensor event, or a speaker state for many later steps. Gates give the model a differentiable way to decide which information should persist and which information should be replaced.

The Problem They Solve

Training a recurrent model uses backpropagation through time. If a loss at the end of a sequence depends on a hidden state many steps earlier, the gradient contains a product of recurrent Jacobians:

If the factors in that product usually have norm below , gradients vanish. If they usually have norm above , gradients explode. Exploding gradients can be limited with gradient clipping, but vanishing gradients are harder because the learning signal for early events becomes too small to change the parameters.

LSTMs address this by adding a cell state with an additive update:

The forget gate creates a controlled path from to . When is near , information and gradients can pass forward with little change. When is near , the model deliberately forgets that part of memory.

LSTM Cell

An LSTM has a hidden state and a separate cell state . The cell state is the memory path; the hidden state is the exposed representation passed to the next layer or prediction head.

For input and previous hidden state :

The gates have separate roles:

LSTM partRangeRole
Forget gate to keeps or erases old cell memory
Input gate to controls how much candidate memory is written
Candidate to proposes new content for the cell
Output gate to controls how much memory is exposed as hidden state

If and for one memory dimension, the LSTM mostly preserves old information. If and , it mostly overwrites that dimension with new evidence. These values are learned from data; they are not hand-coded rules.

GRU Cell

A GRU simplifies the design by merging memory and hidden state into one vector. It uses an update gate and a reset gate:

The update gate decides how much of the old state to keep. The reset gate decides how much previous state is used when proposing the new candidate. GRUs are often cheaper than LSTMs because they have fewer gates and no separate cell state, while still preserving an additive path for temporal information.

Loss Over Time

For sequence labeling, the model may emit a prediction at every step:

For sequence classification, it may emit only at the end:

Both cases unfold the recurrent cell over time. Longer sequences increase memory use during training because intermediate states are needed for backpropagation. Truncated backpropagation through time reduces compute by backpropagating over shorter windows, but then credit assignment is also limited to those windows.

Gating does not make long-range learning free. It gives gradients a better path through memory, while practical training still uses clipping, normalization, careful initialization, masking, packed sequences, and hidden-state resets.

LSTM, GRU, and Transformer Learning

Gated RNNs and transformers learn temporal structure in different ways:

QuestionLSTM / GRUTransformer
How does information move?sequential hidden-state updatedirect attention between token positions
Can training parallelize across time?limited, because depends on yes within a layer, because token states are updated together
Main bottleneckfixed-size recurrent state and long credit assignmentattention cost and context-window limits
Natural deployment modestreaming, low-latency, one step at a timebatch processing over a context window
Long-range accesscarried through memory gatesretrieved by attention weights over visible positions

An LSTM reading tokens left to right must carry old evidence forward through its state. A transformer can let token attend directly to token if the mask allows it. That direct path is a major reason transformers displaced RNNs for large-scale language modeling. Gated RNNs still fit streaming sensor, speech, time-series, and edge deployments where incremental state updates are cheaper than recomputing attention over a long context.

Caveats

Gates are learned and can fail. If the task, data, or optimization encourages the model to overwrite memory too often, long dependencies still disappear. If hidden states are not reset between independent examples, information leaks across batch items. If padding masks are wrong, the model learns from fake timesteps. These bugs are common because recurrent models make time part of the computational graph.

Connections

  • Recurrent Neural Networks explains the vanilla recurrence and the product-of-Jacobians problem.
  • Backpropagation explains the gradient mechanics used when the recurrent cell is unfolded through time.
  • Attention and Transformers explain the non-recurrent alternative that gives positions direct access to one another.

References