AI Glossary: Long Short-Term Memory
LSTM (Long Short-Term Memory) is a gated recurrent neural network that solved the vanishing gradient problem. Its intelligent gating mechanisms allowed neural networks to remember information over hundreds of time steps, revolutionizing NLP and time series analysis.
What is LSTM?
Long Short-Term Memory (LSTM) is a specialized recurrent neural network architecture introduced by Hochreiter and Schmidhuber in 1997. It was designed to solve the vanishing gradient problem that prevented vanilla RNNs from learning long-range dependencies. For over two decades, LSTMs were the dominant architecture for sequence modeling tasks including machine translation, speech recognition, and language modeling.
The core innovation of the LSTM is the introduction of a cell state and three gating mechanisms—the forget gate, input gate, and output gate. These gates act as neural controllers that decide what information to keep, what to discard, and what to output, giving the network precise control over its memory.
Core Innovation
Cell state (long-term memory) + three gates that control information flow, solving the vanishing gradient problem.
Historical Impact
Dominated NLP from 2015-2020 (Google Translate, speech recognition, language modeling).
Current Status
Largely replaced by transformers for NLP, but still widely used in time series, audio, and resource-constrained applications.
Why "Long Short-Term"?
The name reflects the LSTM's key capability: it can remember information for both short and long periods. The cell state can carry information across hundreds of time steps with minimal degradation, while the gates can also rapidly update it with new information. This combination of "long" memory (cell state) and "short-term" responsiveness (gates) is what makes LSTMs powerful.
The Three Gates
LSTM gates are neural networks that output values between 0 and 1 (using sigmoid activation), controlling how much information passes through. A value of 0 means "let nothing through," 1 means "let everything through."
Forget Gate
Decides what information to discard from the cell state. It looks at the previous hidden state and the current input, and outputs a number between 0 and 1 for each number in the cell state. 1 = "keep this," 0 = "forget this."
Input Gate
Decides what new information to store in the cell state. It has two parts: (1) a sigmoid layer that decides which values to update, and (2) a tanh layer that creates new candidate values that could be added to the cell state.
Output Gate
Decides what to output. The output is based on the cell state, but filtered through the output gate. The sigmoid layer decides which parts of the cell state to output, and the tanh normalizes the cell state values to between -1 and 1.
The three gates work together to manage the cell state: forget what's irrelevant, add new information, and output what's needed.
The Cell State: Long-Term Memory
The cell state is the LSTM's most important innovation. It runs like a conveyor belt through the entire chain of LSTM cells, with only minor linear interactions. Information can flow along this belt unchanged for many time steps, making it the perfect mechanism for long-term memory.
The cell state is modified by two operations:
- Forget gate × old cell state — Removes information that's no longer relevant
- Input gate × new candidate values — Adds new information that's important to remember
The result is a new cell state that carries the updated memory forward to the next time step. This is fundamentally different from vanilla RNNs, where the hidden state is completely overwritten at each step.
Step-by-Step LSTM Operation
- Receive input — The LSTM cell receives the current input x_t and the previous hidden state h_{t-1}.
- Forget gate — Computes which information to remove from the previous cell state: f_t = σ(W_f × [h_{t-1}, x_t] + b_f).
- Input gate — Computes what new information to add: i_t = σ(W_i × [h_{t-1}, x_t] + b_i) and candidate values: C̃_t = tanh(W_c × [h_{t-1}, x_t] + b_c).
- Update cell state — New cell state = forget × old cell state + input × candidate: C_t = f_t × C_{t-1} + i_t × C̃_t.
- Output gate — Computes what to output: o_t = σ(W_o × [h_{t-1}, x_t] + b_o).
- New hidden state — h_t = o_t × tanh(C_t). This is the output for this time step.
LSTM vs GRU vs RNN
| Feature | Vanilla RNN | LSTM | GRU |
|---|---|---|---|
| Gates | None | 3 (forget, input, output) | 2 (reset, update) |
| Cell State | No | Yes (separate) | No (merged with hidden) |
| Long-range dependency | Poor | Excellent | Very Good |
| Parameters | Fewest | Most | Medium |
| Training speed | Fastest | Slowest | Medium |
Frequently Asked Questions
Sigmoid (output range 0-1) is used for gates because it naturally models "how much to let through" — 0 means nothing, 1 means everything. Tanh (output range -1 to 1) is used for candidate values and the final output because it's zero-centered, which helps with gradient flow. The combination of both activation functions gives the LSTM precise control over information flow with good gradient properties.
Yes, stacked LSTMs (also called deep LSTMs) are common and often perform better than single-layer LSTMs. Each layer's hidden state becomes the input to the next layer. The lower layers learn lower-level temporal patterns, and higher layers learn more abstract patterns. Stacked LSTMs were the backbone of many state-of-the-art NLP systems before transformers.
Stateless LSTMs reset the cell state between batches, treating each batch independently. Stateful LSTMs preserve the cell state across batches, allowing the LSTM to maintain continuity across sequences that span multiple batches. Stateful LSTMs are useful when you have one very long sequence that you process in chunks, but they require careful batch construction.
A bidirectional LSTM (BiLSTM) runs two independent LSTMs: one processes the sequence forward (past to future), and the other processes it backward (future to past). Their outputs are concatenated at each time step, giving the model access to both past and future context. This is useful for tasks where the full sequence is available (classification, NER) but not for autoregressive generation.
Transformers replaced LSTMs primarily because: (1) They can be parallelized across all positions during training, making them much faster; (2) Self-attention provides direct connections between all positions, making long-range dependencies easier to learn; (3) They scale better to very large models and datasets. LSTMs' sequential nature makes them inherently slower to train and harder to scale to billions of parameters.
Continue your AI learning journey
Next, explore Autoencoders—neural networks that learn to compress and reconstruct data.
Read Next: Autoencoder →