Introduction

A vanilla recurrent cell cannot learn dependencies that span many time-steps because it suffers from the vanishing gradient problem.

In vanilla recurrent neural networks, a recurrent cell processes a sequence one element at a time and uses a hidden state $h_t$ that is updated at every time-step using the same weight matrix $W_h$. While this allows one set of weights to process a sequence of any length, it also makes training very hard on long sequences.

To update the weights, the gradient flows back through the recurrence. At each step back, the gradient is multiplied by $W_h^{\mathsf{T}}$. The gradient at an earlier time-step $k$ is therefore the gradient at the final step, multiplied by one factor for each step back:

$$ \frac{\partial L}{\partial h_k} = \left( \prod_{j=k+1}^{T} W_h^{\mathsf{T}} \cdot \mathrm{diag}(1 - h_j^2) \right) \frac{\partial L}{\partial h_T} $$

Each factor is $W_h^{\mathsf{T}}$ times a term $\mathrm{diag}(1 - h_j^2)$ whose magnitude is at most $1$, and these factors are applied in the order the chain rule dictates. When the spectral norm of $W_h$ is less than $1$, the product becomes very small over a long sequence, so the gradient becomes almost zero. As a result, the gradient does not reach the early time-steps and the network cannot update the weights using information from the start of the sequence. This is the vanishing-gradient problem. It is why a vanilla RNN cannot learn long-range dependencies.

The trouble starts with the single state vector. The hidden state $h_t$ is asked to do two jobs at once:

  • Be the memory. It carries everything the network has seen so far.
  • Be the output. It is projected to a prediction and fed into the next time-step.

Anything squashed on the way through the recurrence is lost to both jobs. LSTM answers by keeping two states where the RNN keeps one. A cell state $c_t$ acts as the long-term memory, carried through time and updated gently. A hidden state $h_t$ has the job of reading out from the cell and passing a projection to the rest of the network. The cell state is updated additively, and its direct dependence on the previous cell state is an elementwise multiplication by a gate, not a matrix multiplication. That protects the gradient flow and is the core idea behind the LSTM architecture.

The architecture

LSTM processes a sequence one element at a time, as an RNN does. A vanilla RNN carries a single state vector through the sequence: a representation of what the network has seen so far. The LSTM cell keeps two state vectors instead of one.

Two states: cell state and hidden state

At each time-step $t$, the LSTM cell takes the current input $x_t$, the previous hidden state $h_{t-1}$, and the previous cell state $c_{t-1}$. It maintains two states:

  • Hidden state $h_t \in \mathbb{R}^{d}$: the output of the LSTM cell. It behaves like the RNN’s state. It is projected to predictions, and it feeds the next time-step.
  • Cell state $c_t \in \mathbb{R}^{d}$: the long-term memory. It is carried through time, and it is updated more gently. It is the gradient highway. The vanishing-gradient fix relies on this state.

The hidden state is derived from the cell state and the output gate. The cell state is updated by the forget gate, the input gate, and the candidate, which we explore below.

The four gates

LSTM uses four learned transformations. Each transformation has an input weight matrix $W_\ast \in \mathbb{R}^{d \times d_{in}}$, a recurrent weight matrix $U_\ast \in \mathbb{R}^{d \times d}$, and a bias $b_\ast \in \mathbb{R}^{d}$. Three of them output values in $(0, 1)$ through a sigmoid $\sigma$ activation. One outputs the candidate content through a $\tanh$ activation:

$$ \begin{aligned} f_t &= \sigma(W_f x_t + U_f h_{t-1} + b_f) \\ i_t &= \sigma(W_i x_t + U_i h_{t-1} + b_i) \\ o_t &= \sigma(W_o x_t + U_o h_{t-1} + b_o) \\ \tilde{c}_t &= \tanh(W_c x_t + U_c h_{t-1} + b_c) \end{aligned} $$
  • $f_t$ is the forget gate. Its values are between 0 and 1, and it controls how much of the previous cell state to keep.
  • $i_t$ is the input gate. Its values are between 0 and 1, and it controls how much of the candidate to write into the cell state.
  • $o_t$ is the output gate. Its values are between 0 and 1, and it controls how much of the cell state to expose as the hidden state.
  • $\tilde{c}_t$ is the candidate. Its values are between -1 and 1, so it can be positive or negative. Its sign decides whether to add to or subtract from the previous time-step’s cell state.

The gates have the same shape as the cell state and the hidden state, so $f_t, i_t, o_t, \tilde{c}_t \in \mathbb{R}^{d}$. The gating is elementwise: dimension $k$ of a gate multiplies dimension $k$ of the hidden or cell state, so each dimension is controlled independently.

The cell state carries the long-term memory, and it is updated in place:

$$ c_t = f_t \odot c_{t-1} + i_t \odot \tilde{c}_t $$

The forget gate $f_t$ controls how much of the old state to keep, and the input gate $i_t$ controls how much of the candidate to write in.

Two extremes make this clear:

  • If $f_t = 1$ and $i_t = 0$, the old state passes through unchanged: $c_t = c_{t-1}$. The cell is pure memory.
  • If $f_t = 0$ and $i_t = 1$, the candidate replaces the state: $c_t = \tilde{c}_t$. The cell is reset.

The gates take continuous values in $(0, 1)$, so real behaviour sits between these two extremes.

The hidden state is the cell state, squashed by a $\tanh$ and scaled by the output gate $o_t$:

$$ h_t = o_t \odot \tanh(c_t) $$

Both the hidden state $h_t$ and the cell state $c_t$ are carried into the next time-step. Only the hidden state $h_t$ is passed to the rest of the network and used for the prediction; the cell state $c_t$ stays internal to the cell.

gate symbol range what it controls
forget $f_t$ $(0, 1)$ how much of $c_{t-1}$ to keep (erase old memory)
input $i_t$ $(0, 1)$ how much of the candidate to write into $c_t$
output $o_t$ $(0, 1)$ how much of $c_t$ to expose as $h_t$ (read memory)
candidate $\tilde{c}_t$ $(-1, 1)$ the new content that may be stored

Time complexity

The asymptotic cost of an LSTM recurrence is of the same order as that of a vanilla recurrent cell, with a constant factor that scales with the number of gated transformations. We analyse the per-time-step computational cost and the cost over an entire sequence.

Per-time-step computational cost

Each of the four transformations $f_t, i_t, o_t, \tilde{c}_t$ evaluates an affine transformation of the pair $(x_t, h_{t-1})$. The recurrent term $U_\ast h_{t-1}$ is a matrix-vector product between a $d \times d$ matrix and a $d$ dimensional vector, which requires $d^2$ multiply-accumulate operations, i.e. $\mathcal{O}(d^2)$. The input term $W_\ast x_t$ is a matrix-vector product between a $d \times d_{in}$ matrix and a $d_{in}$ dimensional vector, requiring $\mathcal{O}(d \cdot d_{in})$ operations. The nonlinear activation ($\sigma$ or $\tanh$) and the bias addition are elementwise and cost $\mathcal{O}(d)$ each.

Accounting for the four transformations, the per-time-step cost of a single cell is

$$ T_{\text{cell}} = 4 \left( d^2 + d \cdot d_{in} + d \right) = \mathcal{O}(d^2) $$

When the hidden dimensionality dominates the input dimensionality (i.e. $d \gtrsim d_{in}$), the quadratic term dominates and the per-time-step complexity is $\mathcal{O}(d^2)$, with a constant factor approximately four times that of a vanilla recurrent cell, whose recurrence is a single $\mathcal{O}(d^2)$ matrix-vector product.

Complexity over a full sequence

An LSTM applies the cell once per time-step. For an input sequence of length $n$, the total computational cost is therefore

$$ T_{\text{LSTM}} = n \cdot \mathcal{O}(d^2) = \mathcal{O}(n d^2) $$

The asymptotic order of the recurrence, $\mathcal{O}(n d^2)$, matches that of a vanilla RNN up to a constant factor. The recurrence is sequential: $c_t$ depends on $c_{t-1}$ and $h_t$ depends on $h_{t-1}$, so the time-steps cannot be processed in parallel.

In summary, an LSTM is not computationally cheaper than a vanilla RNN. It has the same asymptotic time complexity, $\mathcal{O}(n d^2)$, with a constant factor around four times larger due to the four gated transformations. Its advantage lies not in efficiency but in the gated, additive cell state, which preserves the gradient over long sequences.

How LSTM tames the vanishing gradient

In the RNN, the gradient is multiplied by the same weight matrix $W_h^{\mathsf{T}}$ at every time-step back. If the spectral norm of $W_h$ is less than $1$, the gradient shrinks at every time-step and collapses over a long sequence. In the LSTM, the multiplier on the cell-state path is the forget gate, a learned value in $(0,1)$. The network can set it close to $1$ for the dimensions it wants to remember, so the old cell state passes through nearly unchanged and the gradient survives. We derive this in the rest of this section, and then look at where it still breaks down.

The gradient through the cell state

The RNN recurrence multiplies $h_{t-1}$ by a weight matrix and pushes it through a $\tanh$. In LSTM, the cell-state update does not do that. It is additive, so the dependence of $c_t$ on $c_{t-1}$ is an elementwise multiplication by a gate, not by a matrix. That is the structural reason the cell state protects the gradient.

Let’s start from the cell update:

$$ c_t = f_t \odot c_{t-1} + i_t \odot \tilde{c}_t $$

The direct dependence of $c_t$ on $c_{t-1}$ is the term $f_t \odot c_{t-1}$, whose derivative is a diagonal matrix. Written coordinate by coordinate, $(f_t \odot c_{t-1})_j = f_{t,j} \cdot c_{t-1,j}$. Entry $j$ of the product therefore depends on coordinate $j$ of $c_{t-1}$ and on nothing else. Differentiating entry $j$ with respect to $c_{t-1,k}$ gives $f_{t,j}$ when $k = j$ and $0$ when $k \neq j$. Stacked into a matrix:

$$ \frac{\partial (f_t \odot c_{t-1})}{\partial c_{t-1}} = \mathrm{diag}(f_t) $$

So one time-step back, the gradient is multiplied elementwise by $f_t$, not by a weight matrix. Along the cell-state highway, the gradient flowing from $c_t$ back to $c_{t-k}$ therefore carries an elementwise product of the forget gates. Each step back contributes a diagonal factor $\mathrm{diag}(f_{t-j})$, and diagonal matrices multiply entrywise, so the product across $k$ steps is the single diagonal matrix $\mathrm{diag}(\prod_{j=0}^{k-1} f_{t-j})$:

$$ \frac{\partial L}{\partial c_{t-k}} = \left( \prod_{j=0}^{k-1} f_{t-j} \right) \odot \frac{\partial L}{\partial c_t} $$

Here the $\prod$ is elementwise multiplication. This is the key equation.

Why the forget gate fixes vanishing

Contrast this with the RNN. Each step back multiplies the gradient by the same matrix $W_h^{\mathsf{T}}$, so the gradient from $k$ time-steps back scales like $\|W_h^{\mathsf{T}}\|_2^{\,k}$. The network learns this matrix, but it is a single matrix shared by all time-steps, so one global spectral norm controls the decay of everything the state carries.

In the LSTM the analogous factor is the forget gate $f_t$, a vector with one value in $(0, 1)$ per dimension. Unlike $W_h$, which stays the same matrix at every time-step, $f_t$ is recomputed at every time-step from the current input and hidden state, $f_t = \sigma(W_f x_t + U_f h_{t-1} + b_f)$. For a dimension that must carry a long-range dependency, the network can push that dimension’s forget gate close to $1$, so the corresponding entry of the product $\prod_{j=0}^{k-1} f_{t-j}$ stays near $1$ and the gradient survives for a large number of time-steps. For a dimension that should forget, it can drive that dimension’s forget gate toward $0$. The gradient flow is no longer at the mercy of one global spectral condition; it is governed by per-dimension values that the network recomputes on every time-step. That is the essence of the fix: LSTM turns a single matrix, shared across all time-steps, into a per-dimension gate recomputed at each time-step.

Where it can still fail

LSTM does not fully solve the vanishing-gradient problem. It mitigates it, and it relocates the difficulty. There are still ways for gradients to vanish or explode.

The hidden-state path

The highway protects the cell state. It does not protect the hidden state. The hidden state is a projection of the cell state and is given by the formula

$$h_t = o_t \odot \tanh(c_t)$$

The $j$-th entry of the hidden state is

$$ h_{t,j} = o_{t,j} \cdot \tanh(c_{t,j}) $$

If we take the derivative of the hidden state with respect to the cell state $c_t$, we will observe that the output gate $o_{t,j}$ does not depend on $c_t$. It depends on $x_t$ and on $h_{t-1}$. Therefore, $o_{t,j}$ is constant with respect to $c_t$.

Since $o_{t,j}$ is constant with respect to $c_t$ and $\tanh' = 1 - \tanh^2$, the product rule gives

$$ \frac{\partial h_{t,j}}{\partial c_{t,j}} = o_{t,j} \cdot \left(1 - \tanh^2(c_{t,j})\right) $$

For $k \neq j$, the partial derivative is zero:

$$ \frac{\partial h_{t,j}}{\partial c_{t,k}} = 0 $$

The coordinate $j$ does not depend on any other coordinate of $c_t$. Therefore, the derivative is a diagonal matrix:

$$ \frac{\partial h_t}{\partial c_t} = \mathrm{diag}\!\left(o_t \odot (1 - \tanh^2(c_t))\right) $$

Each diagonal entry is a product of two factors. Each factor is at most $1$. The output gate $o_t$ is in $(0, 1)$. The $\tanh$ derivative $1 - \tanh^2$ is in $(0, 1]$.

The $\tanh$ factor here is not a free, learned knob like the output gate; it is tied to the content of the cell. $(1 - \tanh^2(c_t))$ is near $1$ only when $c_t$ is near $0$, and it falls toward $0$ as the cell saturates. So this path has a high gradient exactly when the cell state is empty, and it dies exactly when the cell state is holding something. You cannot store a large value in $c_t$ and have the gradient flow through this path unattenuated. Even if you push the learned output gate $o_t$ to $1$, the $\tanh$ squash still kills the signal when the cell is loaded.

A gradient that flows from $h_t$ back to $c_t$ is multiplied by a number that is $\le 1$ at every coordinate. In practice, the output gate rarely sits at $1$, so this number is usually much smaller than $1$. Therefore, the projection squeezes the gradient at each time-step.

To carry the recurrence backward, the gradient must also flow back to the previous hidden state $h_{t-1}$. It reaches $h_{t-1}$ through the four gates, because each of them depends on $h_{t-1}$ through its own recurrent weight matrix: the forget gate $f_t$ through $U_f$, the input gate $i_t$ through $U_i$, the output gate $o_t$ through $U_o$, and the candidate $\tilde{c}_t$ through $U_c$.

In backprop, these matrices appear as the transposes $U_f^{\mathsf{T}}, U_i^{\mathsf{T}}, U_o^{\mathsf{T}}, U_c^{\mathsf{T}}$. Therefore, the path contains four matrix multiplications. This path behaves much like an RNN. It can still be attenuated or dominated by the spectral radii of those matrices.

Forgetting too much

The forget gate makes per-dimension decay controllable, but control does not imply that the network chooses well. The product of forget gates along the cell-state path, derived in the previous section, stays near $1$ only if the network learns to keep the relevant gates close to $1$. It is equally free to drive a gate toward $0$, and the difficulty arises when it does so on a coordinate that still needs its memory. Retaining a long-range dependency is therefore a requirement on the optimization, not an automatic property of the architecture.

Suppose a coordinate must carry a long-range dependency but is assigned a forget gate near $0$. Over the intervening time-steps the product of forget gates collapses, so the corresponding entry of the cell state decays toward zero before it can influence the output. Once that happens, the error signal that would flow back through this coordinate is itself scaled by the collapsed product of forget gates, so the gradient of the loss with respect to the parameters that produce it is essentially zero. Backpropagation therefore returns no error signal to push the forget gate back up, and gradient descent cannot correct it. The limitation is not that the architecture cannot hold the memory, but that the optimization has no path to learn it.

Closing a dimension is appropriate when that dimension has served its role and should reset its context. The network does not know in advance which dimensions will be needed over long horizons, so it must discover this during training. A poor initialization or a difficult optimization landscape can keep a needed forget gate small, in which case the gradient does not carry the signal that would correct it. The standard mitigation is to initialize the forget gate bias near $1$, so training starts with the forget gates close to $1$ and lowers the ones it no longer needs.

Exploding gradients

The gate-weight matrices $U_f, U_i, U_o, U_c$ can have spectral radius greater than $1$, and the gradient paths that run through them can blow up. So exploding gradients are still a real concern for LSTMs, and gradient clipping (clipping the gradient’s norm before the parameter update) is still routinely used.

Conclusion

The plain RNN fails on long sequences because the gradient is a product of as many $W_h^{\mathsf{T}}$ factors as there are time-steps back, so it decays or explodes geometrically depending on the spectral norm of $W_h^{\mathsf{T}}$. The network has no way to slow the decay for the information it wants to keep and speed it up for the information it wants to drop; one spectral norm sets a single rate for everything. Avoiding both vanishing and exploding would require that norm to sit exactly at $1$, a knife edge that is impractical, because any deviation decays or blows up the gradient over a long sequence.

LSTM addresses this by introducing a cell state whose update is additive and gated. The gradient along the cell state is multiplied by a learned forget gate each time-step rather than by the same weight matrix at every time-step, so the network can keep the factor near $1$ for the information it needs to remember. This does not guarantee that the gradient survives, but it makes survival a decision the network controls during the forward pass. That turns a uniform, unavoidable decay into a controllable decay per dimension, which is a large practical step: an LSTM can bridge hundreds of time-steps where a vanilla RNN effectively cannot.

The gates also give adaptive memory: the LSTM can decide what to forget, what to write, and what to read, whereas the RNN overwrites its single hidden state at every time-step. But the fix is a mitigation, not a cure. The hidden-state path still attenuates the gradient; a too-small forget gate can still close the highway on a dimension the network needed (a training concern, not a design flaw, usually mitigated by initialising the forget-gate bias near $1$); and the gate weights can still make the gradient explode, which gradient clipping controls. So LSTM offers the architectural possibility of vanishing-free flow, and the training dynamics have to realize it. That distinction is why LSTM is best described as a dependable, standard recurrent unit for long sequences rather than a guaranteed fix.

That additive, gated memory is LSTM’s real contribution, and it is why recurrence still has a place. Streaming and time-series models want an explicit state and cheap online inference, and modern variants like xLSTM keep the idea alive. Most sequence models today are transformers, built on attention, and they make a different trade: they train in parallel and handle long context, but pay quadratic memory. Recurrence trains serially, yet gives constant-memory, linear-time inference and an explicit state.

References