Skip to main content
Neural Networks 16 min read Published 2026-10-22

Understanding Recurrent Neural Networks and LSTM Cells

A comprehensive derivation of recurrent loops, vanishing gradients, constant error carousels, gating mechanisms, and mathematical formulations in Long Short Term Memory networks.

M
Bibhu Pradhan & Editorial Team LLM Matrix Lab Research & Editorial

The Need for Sequence Persistence

Humans do not reset their cognitive state from scratch with every passing second. As you read this sentence, you understand each incoming word based on the context established by the words that preceded it. You do not discard your working memory after every punctuation mark and start your comprehension from zero. Your thoughts possess persistence.

Traditional feed forward neural networks cannot maintain persistence. In a conventional multilayer perceptron or convolutional network, each input sample is processed entirely independently of all previous samples:

y=σ(Wx+b)y = \sigma(W x + b)

This architecture works well for static tasks such as classifying isolated photographs, but it represents a severe shortcoming when analyzing sequential information. Consider an artificial intelligence system trying to predict the next word in a conversational stream, transcribe audio phonemes from continuous speech, or summarize a paragraph of text. To make an accurate prediction at time step tt, the network must retain context from time steps t−1,t−2t-1, t-2, and earlier.

Recurrent Neural Networks (commonly abbreviated as RNNs) resolve this challenge. They are neural networks equipped with feedback loops, allowing information to cycle through internal states and persist across sequential transitions.

Recurrent neural networks have loops
Recurrent neural networks possess internal feedback loops allowing information to cycle and persist across transitions. Diagram credit Christopher Olah (2015).

In the diagram above, a modular block of neural network, labeled AA, receives an input vector xtx_t at step tt and emits an output activation vector hth_t. The feedback loop allows internal state signals to pass from one processing cycle to the next.

Unrolling Recurrent Neural Networks Through Time

These recurrent feedback loops can initially appear mysterious. However, unrolling the network through time reveals that a recurrent network is simply multiple copies of the exact same neural network layer chained together, with each copy passing its hidden state to its successor:

An unrolled recurrent neural network chain through time
An unrolled recurrent neural network chain passes sequential hidden states through identical network copies across discrete time steps. Diagram credit Christopher Olah (2015).

At any individual discrete time step tt, the standard Elman recurrent network computes its updated hidden state hth_t and emission prediction yty_t through two matrix transformations:

ht=tanh⁡(Whhht−1+Wxhxt+bh)h_t = \tanh(W_{hh} h_{t-1} + W_{xh} x_t + b_h)

yt=softmax(Whyht+by)y_t = \text{softmax}(W_{hy} h_t + b_y)

Here:

  • xt∈Rdx_t \in \mathbb{R}^d represents the input vector at time step tt.
  • ht−1∈Rhh_{t-1} \in \mathbb{R}^h represents the hidden state vector from the previous time step.
  • Wxh∈Rh×dW_{xh} \in \mathbb{R}^{h \times d} is the parameter weight matrix mapping inputs to hidden states.
  • Whh∈Rh×hW_{hh} \in \mathbb{R}^{h \times h} is the shared recurrent parameter weight matrix mapping prior hidden activations to new hidden activations.
  • bh∈Rhb_h \in \mathbb{R}^h is the hidden state bias vector.
  • tanh⁡\tanh is the hyperbolic tangent activation function, squashing values into the bounded range [−1,1][-1, 1].

This unrolled chain structure reveals that recurrent neural networks are fundamentally designed for sequences and ordered lists. They formed the dominant architecture for machine translation, language modeling, and voice recognition for over two decades.

However, standard recurrent networks suffer from a fundamental mathematical flaw that crippled their ability to solve complex, long sequence tasks.

The Dilemma of Long Distance Dependencies

The primary promise of recurrent networks is that they can connect earlier context to a downstream decision. If an autonomous agent reads a continuous text, previous paragraphs should inform current interpretations. Whether a basic RNN can accomplish this depends entirely on the distance between the context and the prediction.

Sometimes, only immediate recent context is required. For example, consider a language model predicting the final word in the phrase:

“The clouds gather in the sky.”

The relevant context word “clouds” sits only four tokens before the blank. Because the distance is minimal, standard recurrent loops easily propagate the contextual signal forward.

Short term dependencies in recurrent neural networks
Short term dependencies: when the gap between the relevant context and the prediction target is small, standard RNNs learn easily. Diagram credit Christopher Olah (2015).

Now consider a case requiring distant contextual memory:

“I spent my early childhood in France… After twenty five years of living overseas, I can still converse fluently in French.”

Recent tokens such as “converse fluently in” suggest that the missing word is the name of a language. But to determine which language, the network must recall the location “France” mentioned dozens of words earlier.

As the temporal distance between the relevant evidence and the target prediction increases, standard recurrent neural networks become mathematically incapable of connecting the information.

Long term dependencies problem in recurrent networks
Long term dependencies: as the sequence gap widens, standard RNNs become unable to connect distant information due to gradient decay. Diagram credit Christopher Olah (2015).

The Mathematics of Vanishing and Exploding Gradients

To understand why standard RNNs fail over long distances, we examine how gradients flow backward during training. In Backpropagation Through Time (abbreviated as BPTT), the total loss LL is calculated across sequence steps, and gradients are propagated backward from step TT to earlier step kk:

∂L∂hk=∂L∂hT∏j=k+1T∂hj∂hj−1\frac{\partial L}{\partial h_k} = \frac{\partial L}{\partial h_T} \prod_{j=k+1}^T \frac{\partial h_j}{\partial h_{j-1}}

Let us compute the Jacobian matrix ∂hj∂hj−1\frac{\partial h_j}{\partial h_{j-1}} representing the rate of change of state hjh_j with respect to state hj−1h_{j-1}:

∂hj∂hj−1=diag(1−tanh⁡2(Whhhj−1+Wxhxj+bh))WhhT\frac{\partial h_j}{\partial h_{j-1}} = \text{diag}\left(1 - \tanh^2(W_{hh} h_{j-1} + W_{xh} x_j + b_h)\right) W_{hh}^T

Notice what occurs across the chain rule product:

∏j=k+1T∂hj∂hj−1=∏j=k+1Tdiag(1−hj2)WhhT\prod_{j=k+1}^T \frac{\partial h_j}{\partial h_{j-1}} = \prod_{j=k+1}^T \text{diag}(1 - h_j^2) W_{hh}^T

This expression contains two critical terms:

  1. The derivative of the hyperbolic tangent function 1−tanh⁡2(z)1 - \tanh^2(z), which achieves a maximum value of 1.01.0 at z=0z=0 and decays rapidly toward 0.00.0 as activations saturate.
  2. The recurrent weight matrix WhhTW_{hh}^T multiplied repeatedly by itself T−kT - k times.

If the largest eigenvalue of the recurrent weight matrix is less than 1.01.0 (or if activations saturate), the continuous matrix multiplication causes gradient signals to decay exponentially toward zero as they travel backward. Over a modest sequence length of 50 steps, a gradient factor of 0.80.8 shrinks to:

0.850≈0.0000140.8^{50} \approx 0.000014

The error signal vanishes. The weights in early layers receive virtually zero gradient updates, rendering the network blind to long range historical dependencies.

Conversely, if the eigenvalues exceed 1.01.0, the gradient values explode exponentially, causing numerical instability, arithmetic overflow, and divergence during gradient descent updates.

The Core Architecture of Long Short Term Memory

Long Short Term Memory networks (commonly known as LSTMs) were invented in 1997 by Sepp Hochreiter and Jürgen Schmidhuber specifically to resolve the vanishing gradient problem.

While a standard recurrent neural network contains a simple repeating module with a single hyperbolic tangent layer, an LSTM cell features an intricate internal control structure containing four separate neural network layers that interact in a carefully choreographed manner.

The repeating module in a standard RNN containing a single tanh layer
The repeating module in a standard recurrent neural network contains a single hyperbolic tangent activation layer. Diagram credit Christopher Olah (2015).
The repeating module in an LSTM containing four interacting layers
The repeating module in an LSTM cell features four interacting neural network layers that regulate information flow. Diagram credit Christopher Olah (2015).

To understand how vectors flow through these interacting layers, let us review the diagram notation guide:

Diagram notation key: neural network layer, pointwise operation, vector transfer, concatenate, and copy
Diagram notation guide: yellow boxes represent neural net layers, pink circles represent pointwise tensor operations, lines transfer vectors, merges indicate concatenation, and forks represent vector copying. Diagram credit Christopher Olah (2015).

The central breakthrough of the LSTM is the cell state, denoted as CtC_t.

The cell state runs straight along the top of the unrolled chain, functioning like a physical conveyor belt or memory bus. Information can travel along this linear channel with minimal multiplicative interference.

The cell state conveyor belt in an LSTM cell
The cell state ($C_t$) acts as a continuous conveyor belt running straight along the top of the chain with minimal linear interactions. Diagram credit Christopher Olah (2015).

While a standard RNN continuously multiplies hidden states by the weight matrix WhhW_{hh}, the LSTM updates its cell state through linear additions and subtractions. In calculus, the derivative of a linear addition is identity:

∂(A+B)∂A=1.0\frac{\partial (A + B)}{\partial A} = 1.0

By preserving a linear pathway where error signals can flow backward through time without continuous matrix decay, Hochreiter and Schmidhuber created the Constant Error Carousel (CEC), allowing neural networks to maintain memories across hundreds or thousands of steps.

The Role of Gating Layers

To regulate what information enters, remains in, or exits the cell state, LSTMs utilize specialized mechanisms called gates.

A gate consists of an artificial neural network layer with a sigmoid activation function followed by a pointwise elementwise multiplication operation:

σ(z)=11+e−z\sigma(z) = \frac{1}{1 + e^{-z}}

The sigmoid function maps any real numbered input into a continuous range between zero and one:

  • An activation value of 0 represents a closed gate: “let nothing pass through”.
  • An activation value of 1 represents an open gate: “let everything pass through”.
An LSTM gate consisting of a sigmoid layer and pointwise multiplication
An LSTM gate combines a sigmoid neural network layer ($\sigma$) with a pointwise elementwise multiplication ($\odot$) to control information transfer. Diagram credit Christopher Olah (2015).

An LSTM contains three fundamental gates:

  1. The Forget Gate (ftf_t): Decides what information to purge from the cell state.
  2. The Input Gate (iti_t): Decides what incoming information to write into the cell state.
  3. The Output Gate (oto_t): Decides what filtered version of the cell state to emit as the hidden state hth_t.

Step by Step Mathematical Derivation of LSTM Gating

Let us walk through the four mathematical operations executed within an LSTM cell during time step tt.

Step 1: The Forget Gate Layer

The first decision in the LSTM cell is to identify which information from historical memory should be discarded. This choice is calculated by the forget gate layer:

ft=σ(Wf⋅[ht−1,xt]+bf)f_t = \sigma(W_f \cdot [h_{t-1}, x_t] + b_f)

Step 1: The forget gate layer deciding what to discard from the cell state
Step 1: The forget gate layer $f_t$ looks at $h_{t-1}$ and $x_t$ and outputs values in $[0, 1]$ for each number in cell state $C_{t-1}$. Diagram credit Christopher Olah (2015).

In this equation:

  • [ht−1,xt][h_{t-1}, x_t] denotes the concatenation of the previous hidden state vector ht−1∈Rhh_{t-1} \in \mathbb{R}^h and the current input vector xt∈Rdx_t \in \mathbb{R}^d, producing a combined vector in Rh+d\mathbb{R}^{h + d}.
  • Wf∈Rh×(h+d)W_f \in \mathbb{R}^{h \times (h + d)} is the learned forget gate weight matrix.
  • bf∈Rhb_f \in \mathbb{R}^h is the forget gate bias vector.
  • σ\sigma is the sigmoid activation applied element by element.

The resulting vector ft∈(0,1)hf_t \in (0, 1)^h outputs a value between 0 and 1 for each coordinate in the cell state Ct−1C_{t-1}. For example, in a language model tracking the grammatical gender of a subject, when a new subject noun appears, the forget gate can emit near zero values to purge the gender of the former subject.

Step 2: The Input Gate and Candidate Memory

The second phase determines what new information should be stored in the cell state. This process consists of two coordinated sub steps:

First, the input gate layer applies a sigmoid activation to decide which memory dimensions should be updated:

it=σ(Wi⋅[ht−1,xt]+bi)i_t = \sigma(W_i \cdot [h_{t-1}, x_t] + b_i)

Second, a candidate state layer applies a hyperbolic tangent activation to synthesize a vector of new candidate values, denoted as C~t\tilde{C}_t, that could be added to the cell state:

C~t=tanh⁡(Wc⋅[ht−1,xt]+bc)\tilde{C}_t = \tanh(W_c \cdot [h_{t-1}, x_t] + b_c)

Step 2: The input gate and candidate state layer
Step 2: The input gate $i_t$ decides which values to update while the tanh layer creates candidate vector $\tilde{C}_t$. Diagram credit Christopher Olah (2015).

Here:

  • Wi∈Rh×(h+d)W_i \in \mathbb{R}^{h \times (h + d)} and bi∈Rhb_i \in \mathbb{R}^h parameterize the input gate.
  • Wc∈Rh×(h+d)W_c \in \mathbb{R}^{h \times (h + d)} and bc∈Rhb_c \in \mathbb{R}^h parameterize the candidate memory layer.
  • The tanh⁡\tanh function bounds candidate values to [−1,1][-1, 1], preventing unchecked numerical growth.

Together, iti_t and C~t\tilde{C}_t determine both what to write and how strongly to write it.

Step 3: Updating the Cell State

We now combine the forget gate decisions and candidate updates to compute the new cell state CtC_t:

Ct=ft⊙Ct−1+it⊙C~tC_t = f_t \odot C_{t-1} + i_t \odot \tilde{C}_t

Where ⊙\odot represents the Hadamard product (element by element pointwise vector multiplication).

Step 3: Updating the old cell state to the new cell state
Step 3: Updating old cell state $C_{t-1}$ into new cell state $C_t$ by multiplying by $f_t$ and adding $i_t \odot \tilde{C}_t$. Diagram credit Christopher Olah (2015).

Let us break down the physical meaning of this equation:

  1. ft⊙Ct−1f_t \odot C_{t-1}: We scale the old memory state by our forget factors. Any dimension where ft≈0f_t \approx 0 is cleared; any dimension where ft≈1f_t \approx 1 is retained intact.
  2. it⊙C~ti_t \odot \tilde{C}_t: We scale our new candidate values by our input gate factors, determining how much fresh evidence to inject.
  3. The ++ operator: We combine the preserved old state with the new filtered input through simple vector addition.

This additive update is what prevents gradients from vanishing. During backpropagation, the derivative of CtC_t with respect to Ct−1C_{t-1} contains the additive term ftf_t:

∂Ct∂Ct−1=ft+…\frac{\partial C_t}{\partial C_{t-1}} = f_t + \dots

As long as the network learns to keep the forget gate open (ft≈1f_t \approx 1), gradient error signals can backpropagate indefinitely through the sequence without exponential decay.

Step 4: The Output Gate and Hidden State

The final phase produces the outgoing hidden state hth_t, which serves two purposes: it is passed to time step t+1t+1 as recurrent context, and it is passed to downstream prediction layers (such as a classification head or vocabulary projection).

The output is based on the updated cell state, but passes through an active filter:

First, the output gate layer runs a sigmoid function to decide which parts of the cell state to expose:

ot=σ(Wo⋅[ht−1,xt]+bo)o_t = \sigma(W_o \cdot [h_{t-1}, x_t] + b_o)

Second, we pass the cell state CtC_t through a hyperbolic tangent function to squash values into the range [−1,1][-1, 1], and multiply it elementwise by the output gate:

ht=ot⊙tanh⁡(Ct)h_t = o_t \odot \tanh(C_t)

Step 4: The output gate and hidden state emission
Step 4: The output gate $o_t$ filters the tanh transformed cell state to emit output activation $h_t$. Diagram credit Christopher Olah (2015).

Here:

  • Wo∈Rh×(h+d)W_o \in \mathbb{R}^{h \times (h + d)} and bo∈Rhb_o \in \mathbb{R}^h are the output gate parameters.
  • ht∈Rhh_t \in \mathbb{R}^h represents the visible hidden state emitted at step tt.

For example, if the cell state remembers that the current grammatical subject is plural, the output gate can expose this property when encountering a verb position so the language model correctly predicts a plural verb form.

Architectural Variants of the Standard LSTM

While the four step formulation described above represents the standard LSTM architecture (often credited to Graves and Schmidhuber), researchers developed several influential architectural variations.

Variant 1: Peephole Connections

Introduced by Felix Gers and Jürgen Schmidhuber in 2000, peephole connections allow the gate layers to inspect the internal cell state Ct−1C_{t-1} directly, rather than relying exclusively on the external hidden state ht−1h_{t-1}:

ft=σ(Wf⋅[ht−1,xt]+Wcf⊙Ct−1+bf)f_t = \sigma(W_f \cdot [h_{t-1}, x_t] + W_{cf} \odot C_{t-1} + b_f)

it=σ(Wi⋅[ht−1,xt]+Wci⊙Ct−1+bi)i_t = \sigma(W_i \cdot [h_{t-1}, x_t] + W_{ci} \odot C_{t-1} + b_i)

ot=σ(Wo⋅[ht−1,xt]+Wco⊙Ct+bo)o_t = \sigma(W_o \cdot [h_{t-1}, x_t] + W_{co} \odot C_t + b_o)

Peephole connections allowing gate layers to look directly at the cell state
Peephole connections (Gers & Schmidhuber, 2000) allow gate layers to inspect the internal cell state directly. Diagram credit Christopher Olah (2015).

Notice that the forget and input gates inspect the previous cell state Ct−1C_{t-1}, while the output gate inspects the newly updated cell state CtC_t. Peephole connections allow the network to time intervals and measure sequence durations with higher precision.

Variant 2: Coupled Forget and Input Gates

Another common variation ties the forget decision and input decision together into a single choice. Instead of computing independent gates ftf_t and iti_t, the network only decides what to forget, and automatically fills that freed capacity with new input:

Ct=ft⊙Ct−1+(1−ft)⊙C~tC_t = f_t \odot C_{t-1} + (1 - f_t) \odot \tilde{C}_t

Coupled forget and input gates deciding what to forget and what to update simultaneously
Coupled forget and input gates: the network only forgets when it has new information to input, and only inputs when it forgets older memory. Diagram credit Christopher Olah (2015).

Here, the factor 1−ft1 - f_t replaces iti_t. The network only writes new information to a dimension when it is actively forgetting older information.

Variant 3: The Gated Recurrent Unit (GRU)

Perhaps the most prominent variation is the Gated Recurrent Unit (GRU), introduced by Kyunghyun Cho and colleagues in 2014. The GRU simplifies the LSTM cell by eliminating the separate cell state CtC_t, merging all memory into a single hidden state hth_t:

The Gated Recurrent Unit cell combining cell state and hidden state
The Gated Recurrent Unit (Cho et al., 2014) combines forget and input gates into an update gate, merging cell state and hidden state into a single representation. Diagram credit Christopher Olah (2015).

The GRU uses two gates:

  1. The Reset Gate (rtr_t): Controls how much previous memory to disregard when computing new candidate states: rt=σ(Wr⋅[ht−1,xt]+br)r_t = \sigma(W_r \cdot [h_{t-1}, x_t] + b_r)
  2. The Update Gate (ztz_t): Combines the functions of the forget and input gates: zt=σ(Wz⋅[ht−1,xt]+bz)z_t = \sigma(W_z \cdot [h_{t-1}, x_t] + b_z)

The candidate hidden state is computed by applying the reset gate to the prior hidden state:

h~t=tanh⁡(W⋅[rt⊙ht−1,xt]+b)\tilde{h}_t = \tanh(W \cdot [r_t \odot h_{t-1}, x_t] + b)

The final hidden state interpolation is:

ht=(1−zt)⊙ht−1+zt⊙h~th_t = (1 - z_t) \odot h_{t-1} + z_t \odot \tilde{h}_t

Because the GRU features fewer parameter matrices and requires fewer tensor multiplications per step, it computes faster and requires less memory while achieving competitive performance on many sequential modeling benchmarks.

Summary Comparison of Recurrent Architectures

Metric / DimensionStandard RNNLSTM CellGated Recurrent Unit (GRU)
Introduced ByRumelhart, Elman (1986, 1990)Hochreiter, Schmidhuber (1997)Cho et al. (2014)
Persistent StatesSingle hidden state hth_tDual states: Cell CtC_t & Hidden hth_tSingle hidden state hth_t
Gating MechanismsNone3 gates (ft,it,otf_t, i_t, o_t)2 gates (rt,ztr_t, z_t)
Parameter Matrices2 (Wxh,WhhW_{xh}, W_{hh})4 gate matrices (Wf,Wi,Wc,WoW_f, W_i, W_c, W_o)3 gate matrices (Wr,Wz,WW_r, W_z, W)
Vanishing GradientsSevere over >15>15 stepsMinimal over hundreds of stepsMinimal over hundreds of steps
Step ExecutionHighly lightweightModerately heavyLightweight to moderate

From Recurrence to Self Attention: The Evolution to Transformers

For more than a decade, Long Short Term Memory networks powered state of the art sequence applications, from Google Translate to Apple Siri and voice dictation.

However, as dataset sizes surged from millions to billions of tokens, recurrent architectures collided with two fundamental hardware and algorithmic constraints:

1. Sequential Execution Bottleneck

To compute step tt, an LSTM strictly requires the output of step t−1t-1:

ht=LSTM(ht−1,xt)h_t = \text{LSTM}(h_{t-1}, x_t)

Because step tt depends on step t−1t-1, recurrent networks cannot parallelize training across sequence positions. Graphics Processing Units (GPUs) and Tensor Processing Units (TPUs) achieve their extraordinary computational throughput by running thousands of matrix multiply operations simultaneously across large parallel cores. When training an RNN, these cores must wait sequentially for each token transition, creating severe training latency.

2. Maximum Signal Path Length

In any recurrent architecture, information from token 11 must travel through T−1T - 1 physical network steps to reach token TT. Even with constant error carousels, memory compression remains lossy; squeezing an entire sequence into a fixed dimension hidden vector inevitably creates an information bottleneck.

In 2017, Ashish Vaswani and colleagues published the landmark paper “Attention Is All You Need”, introducing the Transformer architecture. The Transformer completely dismantled recurrence:

  • Instead of processing sequentially in O(T)O(T) operations, the Transformer calculates pairwise relationships across all tokens simultaneously using Self Attention.
  • The path length between any two arbitrary tokens in a sequence drops from O(T)O(T) in an RNN to O(1)O(1) in a Transformer layer.
  • Training is fully parallelized across all sequence tokens, allowing modern models to train across trillions of words.

Understanding recurrent neural networks and LSTM gating equations illuminates why transformers were designed as they were. The core quest of deep learning sequence modeling has always been the same: preserving long distance contextual relationships without losing gradients or hitting hardware limits.

To explore how weights, bias terms, and backpropagation gradients behave in real time, launch our interactive laboratory visualizers below.

Topic Tags & Keywords

#LSTM #RNN #Recurrent Networks #Deep Learning #Neural Networks #Gated Units
M

About LLM Matrix Lab Editorial

LLM Matrix Lab publishes open access, reproducible machine learning articles designed to demystify neural network architectures, subword tokenizers, and matrix operations through rigorous mathematics and interactive simulations.

Continue Reading

matrix