What is an LSTM (long short-term memory) network?
An LSTM (long short-term memory) network is a recurrent neural network with a separate memory, the cell state, and three gates that decide what to forget, what to write, and what to output at each step. The gates are small sigmoid layers that output values between 0 and 1, and they multiply the information flowing through the cell. Because the cell state is updated by addition, an LSTM can keep information for hundreds of steps where a plain RNN loses it after a few.
LSTMs were the standard sequence model from the late 2000s until Transformers arrived. They were used in speech recognition, machine translation, handwriting recognition, and text prediction, and they are still used for time series forecasting, sensor data, and small on-device models. Understanding the cell state also makes residual connections and gated layers in newer architectures easier to follow.
Key ideas
- An LSTM adds a cell state that is updated by addition instead of being rewritten at each step.
- The forget gate scales old memory, the input gate scales new content, and the output gate scales what is exposed.
- Each gate is a sigmoid layer applied to [h_t−1, x_t], with one value per dimension.
- The cell update is C_t = f_t ⊙ C_(t−1) + i_t ⊙ c̃_t, and the output is h_t = o_t ⊙ tanh(C_t).
- Along the cell state, the gradient is multiplied only by forget gates, so it can survive long sequences.
- An LSTM has four times the weights of a plain RNN with the same hidden size.
Common questions
What is the difference between an LSTM and a GRU?
A GRU (gated recurrent unit) merges the cell state and hidden state into one vector and uses two gates, update and reset, instead of three. It has about three quarters of the parameters of an LSTM of the same size. In comparisons the two usually perform similarly, so the choice is often made by speed or by trying both.
Are LSTMs better than Transformers?
For most language tasks with plenty of data, Transformers are more accurate and train faster because they process all positions in parallel. LSTMs still do well on small datasets, streaming input, and devices with little memory, since each new step costs a fixed amount of compute. For time series both are used.
What do input size and hidden size mean in an LSTM?
Input size is the length of each x_t, for example the dimension of a word embedding or the number of sensor readings per step. Hidden size is the length of h_t and C_t, which sets how much the cell can remember. Libraries such as PyTorch take both as arguments, along with the number of stacked layers.
What is a bidirectional LSTM?
A bidirectional LSTM runs one LSTM from left to right and another from right to left, then concatenates their hidden states at each position. Each word's representation then reflects context on both sides. It is used for tagging and classification, but not for generating text, where the future is not yet known.
Related explainers
- Recurrent Neural Networks — How RNNs read and understand sequences
- Vanishing & Exploding Gradients — Why deep networks struggle and how to fix it
- Seq2Seq & Beam Search — Encode an input sequence and decode an output — the architecture behind translation
- Language Modeling — Predict the next token — the objective that powers GPT, BERT, and modern NLP
- Skip Connections & ResNets — Residual learning and why deeper networks work
- How Self-Attention Works — Query, key, value — the core of self-attention
References
- Hochreiter & Schmidhuber (1997), "Long Short-Term Memory", Neural Computation
- Gers, Schmidhuber & Cummins (2000), "Learning to Forget: Continual Prediction with LSTM", Neural Computation
- Greff et al. (2017), "LSTM: A Search Space Odyssey"
- Cho et al. (2014), "Learning Phrase Representations using RNN Encoder-Decoder for Statistical Machine Translation"
- Dive into Deep Learning, "Long Short-Term Memory (LSTM)"
Last updated