TL;DR
A long short-term memory network (LSTM) is a recurrent cell with two states. The hidden state is its output at each step, and the cell state is a memory line that the cell changes only through gates. A forget gate decides how much of the old memory to keep, and an input gate decides how much of a new candidate to write. Because the memory is updated by addition and scaling rather than by repeated squashing, signals and gradients can survive over hundreds of steps.
Where you'll see it: in speech recognition and handwriting recognition systems, in early machine translation encoders and decoders, and in time-series forecasting.
Intuition
A notebook with three doors
Think of a notebook you carry through a long meeting. Each line you write can replace, extend or erase what came before. A plain recurrent network is different: it rewrites its whole memory at every step, through the same squashing function, so anything from early in the meeting is slowly diluted. The LSTM keeps a separate page, the cell state, and gives the cell three doors to it. One door erases old notes, one lets in new notes, and one decides what part of the page to read out right now.
The doors are not switches. Each one is a value between 0 and 1 that the cell computes from the current input and the recent output. A door at 1 lets everything through, a door at 0 blocks everything, and values in between let part of it through. Learning sets how the doors behave, so the network can decide, for each piece of information, whether to remember it.
Why does this matter? In a plain recurrent network, the learning signal passing back through time gets multiplied by a similar factor at every step, so it shrinks or grows exponentially, as the vanishing gradient problem describes. The LSTM's memory line is changed by a multiplication by the forget gate and an addition. When the forget gate is close to 1, the signal passes through many steps almost unchanged. That is the reason the design works.
flowchart LR
x["input x_t"] --> g["gates: forget, input, output"]
hp["previous hidden h_t-1"] --> g
cp["previous cell c_t-1"] -- "times forget gate" --> add(("+"))
g -- "input gate times candidate" --> add
add --> c["cell c_t"]
c -- "output gate times tanh" --> h["hidden h_t"]
The math
The cell
Let be the input at time , the hidden state from the previous step, and the cell state from the previous step. Write for the two vectors stacked into one. The gates and the candidate are computed from that stack:
Here is the sigmoid, which gives values between 0 and 1 for the gates, and gives the candidate values between and . The cell and hidden state are then updated:
The symbol means elementwise multiplication. The forget gate scales the old memory, the input gate scales the new candidate, and the two are added. The output gate decides how much of the squashed memory is visible as the hidden state.
Notice what the update does not do. The direct path from to contains no squashing function and no weight matrix. It is only a multiplication by the forget gate's value, plus an addition.
The gradient along the memory line
Consider the direct path of the cell state from one step to the previous one. Holding the gates fixed, the derivative is
Over steps the product becomes . If the forget gates stay near 1, the gradient passes back almost unchanged. If they are near 0, it is cut off, which is the right behaviour when the old information is no longer needed.
The full derivative is a little larger, because the gates also depend on , and depends on through the output gate. These extra paths are usually included in practice and automatic differentiation computes them. The direct path is the one that explains why gradients survive, so it is the one to keep in mind.
Where the forget gate came from
The forget-gate term was not in the original 1997 design. Gers, Schmidhuber and Cummins added it in 2000, because without it the cell state grew without bound on long sequences. The gate lets the network reset the memory when a new segment starts. The original design had the same effect only through the input and output gates, which made the memory harder to clear.
Worked examples
Use scalar values and set the pre-activations so that the gates come out to round numbers. Let the cell state before the step be , the hidden state , and the input . The pre-activations are , , and .
- Forget gate: .
- Input gate: .
- Candidate: .
- Output gate: .
- New cell state: .
- New hidden state: .
Interpretation: about 73 percent of the old memory survived the forget gate, and the cell took in roughly half of the candidate. The hidden state is the output gate's share of the squashed memory. Nothing in this step was forced; the gate values were chosen to make the arithmetic easy.
Compare two recurrent cells with no input, so that the only thing that changes is the decay of the signal. Suppose a learning signal enters at the last step and must travel back 50 steps.
- Plain recurrent cell, with a recurrent weight whose effective factor is per step: . The signal is about 0.5 percent of its original size when it reaches the start.
- LSTM cell with a forget gate near : . About 60 percent of the signal survives.
- The same comparison over 10 steps gives for the plain cell and for the LSTM. The gap grows exponentially with the length.
Interpretation: a forget gate of 0.99 is not a special setting. It is what a network learns when the information in the cell is worth keeping. The LSTM can afford to hold it, and the plain cell cannot.
A sensor log produces a stream of readings. At a session boundary, the network should forget the previous session. Suppose at that step the forget gate is , the input gate is , and the candidate is . The old cell state was .
- Old memory kept: .
- New memory written: .
- New cell state: .
The old value contributes only of the , so the cell has mostly forgotten the previous session in one step. The same cell can hold a value for many steps when the forget gate is near 1, so the decision is made per step and per unit.
Tradeoffs and failure modes
LSTMs are strong when dependencies span many steps, and the gates give a learned, inspectable control over memory. They are also heavy. Each cell has four weight blocks where a plain recurrent cell has one, so the parameter count and the cost per step are roughly four times larger at the same hidden width. The table below sorts the common situations.
| Situation | What goes wrong | Better choice |
|---|---|---|
| Short sequences with local patterns | Extra gates add cost without much gain | A plain recurrent cell, or a convolution over time |
| Long-range dependencies, moderate hardware | Vanilla RNN loses the early signal | An LSTM, or a [[gated-recurrent-unit |
| Very long contexts and large data | Recurrence is sequential, so training cannot run in parallel over time | A Transformer with attention |
| Need to read the sequence in both directions | A single pass sees only the past | A [[bidirectional-rnn |
| Forget gate saturates at 1 | The cell state grows without bound over long sequences | Careful initialisation of the forget bias, or normalisation |
Two failure modes come up often. First, the cell state can grow in magnitude over long sequences even with gates, and the tanh on the output then saturates. Second, the forget bias matters. Initialising it to a positive value, so the forget gate starts near 1, gives the cell a good starting memory. Initialising it at zero gives a forget gate of 0.5, which halves the memory at each step before learning begins.
History
Sepp Hochreiter and Jürgen Schmidhuber published the LSTM in 1997. Their aim was to fix the problem that recurrent networks could not learn to connect inputs that were far apart in time. Hochreiter had analysed the vanishing gradient problem in his 1991 diploma thesis, and the LSTM was a direct answer to it. The memory cell with an additive update, guarded by gates, was the central idea. The original design had only the input and output gates, and the forget gate was added by Gers, Schmidhuber and Cummins in 2000.
For over a decade the LSTM was known mainly in a small research community. Graves and Schmidhuber used it for handwriting recognition and speech, and its results on those tasks in the mid-2000s made it the standard recurrent cell. Around 2014 it became common in machine translation and speech systems in industry, where it was used in encoder-decoder models. It was then overtaken for most large language tasks by the Transformer, which replaces recurrence with attention while keeping the idea that a learned, content-dependent gate controls what is passed on.
Check yourself
QuestionName the three gates of an LSTM cell and say what each one controls.Show answer
The forget gate scales the old cell state, so it decides how much of the old memory to keep. The input gate scales the candidate , so it decides how much new information to write. The output gate decides how much of the squashed cell state becomes the hidden state that the rest of the network sees.
QuestionAn LSTM has forget gate , the old cell state is , input gate , and candidate . What is ?Show answer
.
QuestionWhy does the cell state in an LSTM let gradients survive over many steps when the plain recurrent update does not?Show answer
Along the direct path, the cell state is updated by a multiplication by the forget gate and an addition, so the derivative is rather than a product of squashing-function derivatives and recurrent matrices. If the forget gates stay near 1, the gradient passes back almost unchanged. A plain recurrent update passes the signal through a squashing function and a weight matrix at every step, which tends to shrink it exponentially.
QuestionA model with a forget gate fixed at 0.9 is trained on sequences of 100 steps. Roughly how much of a learning signal from the last step survives to the first step along the cell's direct path, and why is that a problem?Show answer
is about , so almost none of the signal survives. The early steps then receive essentially no learning signal, and the model cannot learn dependencies that span the sequence. The fix is for the network to learn a forget gate close to 1 when the information is worth keeping, rather than fixing it at 0.9.
QuestionApplied: a speech recogniser must run on a small device with a fixed memory budget, and its inputs are short utterances of a few seconds. Would you choose an LSTM, and what would you check first?Show answer
Probably not an LSTM as the first choice. Short utterances have limited long-range structure, and the LSTM's four weight blocks cost memory and time at every step. Try a smaller gated unit such as a GRU or a plain recurrent cell, and measure accuracy on utterances of the lengths the device will actually see. If accuracy drops clearly on the longest utterances, the separate cell state is earning its cost and the budget needs revisiting.