ElemedexA pocket encyclopedia of AI architectures
No. 015

LSTM

Long Short-Term Memory

Conveyor Belt

It carries a conveyor belt inside and three gatekeepers who decide what gets on, what stays and what gets off. That is how it remembers things from hundreds of steps back, which its ancestors could not. It waited fifteen years for GPUs to make it famous, reigned for five, and in 2017 was dethroned by a certain Transformer that reads everything at once.

Height
1 recurrent (1997) · 4 + 4 (seq2seq, 2014) · 8 + 8 (GNMT, 2016)
Weight
≈ 1.5 GB (seq2seq, fp32)
Parameters
≈ 100 – 10 500 (1997) · 384 M (seq2seq, 2014)
Context
> 1,000 steps (synthetic tasks) · ≈ 200 effective tokens (LM)
Training data
12 M sentence pairs, 652 M words (seq2seq, WMT'14 en–fr)
Year
1997
Generation
2 · Classical machine learning
Creators
Sepp Hochreiter & Jürgen Schmidhuber (TU München / IDSIA); forget gate: Gers, Schmidhuber & Cummins (2000)
Kind
Architecture

Base stats

Total
196

Approximate, ordinal scales meant for comparing species with each other; the hard numbers are in the data sheet.

Weaknesses

  • It is sequential: computing step 100 requires computing the previous 99, so it makes poor use of GPUs.
  • Its effective memory in language is around 200 tokens, and word order matters to it only for the most recent ones.
  • Gradients can still explode: they have to be clipped.
  • Four sets of weights per unit (three gates and a candidate): costlier than a simple recurrent net.

Habitat

It spent its early years solving synthetic laboratory problems. Before 2013 it already ruled handwriting recognition; in 2013 it reached speech recognition, and in 2016 Google Translate. Today it survives in time-series forecasting (Chollet uses it to predict the temperature) and as an illustrious ancestor in every textbook.

Moves

Abilities

Constant error carousel
A linear cell connected to itself with weight 1: error flows through it without vanishing or exploding.
Multiplicative gates
Sigmoid units, with values between 0 and 1, that open or close access to the cell for writing and for reading.
Forget gate
Added in 2000: it lets the cell empty itself when what it stored is no longer useful.

Anatomy

tanh(cₜ)Input xₜPrevious statehₜ₋₁Forgetfₜ (σ)Inputiₜ (σ)Candidatec̃ₜ(tanh)Outputoₜ (σ)hₜ = oₜ · tanh(cₜ)Prediction or next layerPrevious cell cₜ₋₁× fₜ: forget+ iₜ · c̃ₜ: writeNew cell cₜGatesCell-state belt

A modern LSTM cell (with forget gate). The input and the previous state feed three sigmoid gates and a candidate. On the right, the belt: the old cell is multiplied by the forget gate, the new content filtered by the input gate is added, and the output gate decides how much of the cell is read out as the hidden state. Because the belt is updated by addition, the gradient travels along it without vanishing.

Evolution chain

015 LSTM
  • two LSTMs: one reads, one writes022 Seq2seq
  • a selective state space: recurrence that trains in parallel043 Mamba

The LSTM (long short-term memory) is a recurrent architecture that adds to each unit a protected memory cell and a set of multiplicative gates that decide what gets written into it, what is kept and what is read out. It was Sepp Hochreiter and Jürgen Schmidhuber's answer to the great defect of networks like Elman's: they could not learn relations between events many steps apart.

The problem

When a recurrent network is trained with backpropagation through time, the error signal traveling backward is multiplied at each step by factors that depend on the weights and on the derivative of the activation function. If those factors are below 1, the error decays exponentially; if above, it blows up (Hochreiter & Schmidhuber, 1997, secs. 1, 3). This is the vanishing gradient, which Hochreiter had analyzed in his 1991 thesis.

How it works

The 1997 solution is almost an engineering fix: a linear unit connected to itself with weight 1, the constant error carousel, through which error flows without decaying or exploding. To keep this memory from filling up with noise, an input gate shields it from irrelevant inputs and an output gate shields the rest of the network from contents that are not yet relevant (Hochreiter & Schmidhuber, 1997, secs. 4, 7). On synthetic tasks, the LSTM bridged time lags of more than 1000 steps, where earlier methods failed (1997, sec. 1).

The version used today also has a forget gate, which Gers et al. (2000) introduced so that the cell could reset itself when processing continuous streams. At each step, three sigmoid gates (with values between 0 and 1) and a candidate computed with tanh combine as follows:

  • cₜ = fₜ ⊙ cₜ₋₁ + iₜ ⊙ c̃ₜ: forget part of the old, write part of the new;
  • hₜ = oₜ ⊙ tanh(cₜ): read part of the cell out as the hidden state.

Chollet suggests picturing the cell as a conveyor belt running parallel to the sequence: information gets on at one point, travels intact, and gets off when needed (Chollet, 2021, sec. 10.3). Because the belt is updated by addition rather than by multiplication with a weight matrix, the gradient travels along it without vanishing (it can still explode).

Golden age

According to Chollet, the algorithm has barely changed since 1997 (Chollet, 2021, sec. 1.3): what was missing was data and GPUs. It already led cursive handwriting recognition when, in 2013, a deep LSTM achieved what its authors called the best recorded score on the TIMIT phoneme benchmark (Graves et al., 2013). In 2014, Sutskever et al. (2014) used two stacks of four LSTM layers, one reading the source sentence and one writing the translation, with 384 million parameters and 12 million English–French sentence pairs: Seq2seq was born (2014, secs. 3.1, 3.4). A curious trick: reversing the order of the source words improved results considerably (2014, sec. 3.3). The attention of Bahdanau et al. (2015) relieved the bottleneck of squeezing a whole sentence into a single vector (Alammar & Grootendorst, 2024, pp. 13–14; Buckner, 2024, p. 270), and in 2016 Google Translate moved to a network with eight encoder and eight decoder LSTM layers (Wu et al., 2016). The GRU of Cho et al. (2014) is a lighter form, with two gates.

The stats on this entry describe an LSTM from that era (Sutskever and colleagues' model), not the 1997 ones, which had between a hundred and ten thousand weights (Hochreiter & Schmidhuber, 1997, sec. 5.7).

Limits

Recurrence is sequential and wastes the parallelism of the hardware (Alammar & Grootendorst, 2024, p. 14). That is why the Transformer, which dispenses with it, displaced the LSTM from 2017 on (Vaswani et al., 2017; Buckner, 2024, p. 272). Effective memory is shorter than theoretical memory: Khandelwal et al. (2018) found that an LSTM language model used about 200 tokens of context, was highly sensitive to word order within the most recent sentence, and ignored order beyond about 50 tokens. Gradients still explode and must be clipped (Sutskever et al., 2014, sec. 3.4). And, as its authors admitted, counting steps precisely (did it happen 99 or 100 steps ago?) is beyond its reach (Hochreiter & Schmidhuber, 1997, sec. 6).

Sources

  • Alammar, J., & Grootendorst, M. (2024). Hands-on large language models: Language understanding and generation. O’Reilly Media. §
  • Bahdanau, D., Cho, K., & Bengio, Y. (2015). Neural machine translation by jointly learning to align and translate. In 3rd International Conference on Learning Representations (ICLR 2015). https://doi.org/10.48550/arXiv.1409.0473 §
  • Buckner, C. J. (2024). From deep learning to rational machines: What the history of philosophy can teach us about the future of artificial intelligence. Oxford University Press. https://doi.org/10.1093/oso/9780197653302.001.0001 §
  • Cho, K., van Merriënboer, B., Gulcehre, C., Bahdanau, D., Bougares, F., Schwenk, H., & Bengio, Y. (2014). Learning phrase representations using RNN encoder–decoder for statistical machine translation. In A. Moschitti, B. Pang, & W. Daelemans (Eds.), Proceedings of the 2014 Conference on Empirical Methods in Natural Language Processing (EMNLP) (pp. 1724–1734). Association for Computational Linguistics. https://doi.org/10.3115/v1/D14-1179 §
  • Chollet, F. (2021). Deep learning with Python (2nd ed.). Manning Publications. §
  • Chomsky, N. (1957). Syntactic structures. Mouton. https://doi.org/10.1515/9783112316009 §
  • Gers, F. A., Schmidhuber, J., & Cummins, F. (2000). Learning to forget: Continual prediction with LSTM. Neural Computation, 12(10), 2451–2471. https://doi.org/10.1162/089976600300015015 §
  • Graves, A., Mohamed, A.-R., & Hinton, G. (2013). Speech recognition with deep recurrent neural networks. In 2013 IEEE International Conference on Acoustics, Speech and Signal Processing (pp. 6645–6649). IEEE. https://doi.org/10.1109/ICASSP.2013.6638947 §
  • Gulordava, K., Bojanowski, P., Grave, E., Linzen, T., & Baroni, M. (2018). Colorless green recurrent networks dream hierarchically. In M. Walker, H. Ji, & A. Stent (Eds.), Proceedings of the 2018 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies, Volume 1 (Long Papers) (pp. 1195–1205). Association for Computational Linguistics. https://doi.org/10.18653/v1/N18-1108 §
  • Hochreiter, S., & Schmidhuber, J. (1997). Long short-term memory. Neural Computation, 9(8), 1735–1780. https://doi.org/10.1162/neco.1997.9.8.1735 §
  • Khandelwal, U., He, H., Qi, P., & Jurafsky, D. (2018). Sharp nearby, fuzzy far away: How neural language models use context. In I. Gurevych & Y. Miyao (Eds.), Proceedings of the 56th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers) (pp. 284–294). Association for Computational Linguistics. https://doi.org/10.18653/v1/P18-1027 §
  • Linzen, T., Dupoux, E., & Goldberg, Y. (2016). Assessing the ability of LSTMs to learn syntax-sensitive dependencies. Transactions of the Association for Computational Linguistics, 4, 521–535. https://doi.org/10.1162/tacl_a_00115 §
  • Sutskever, I., Vinyals, O., & Le, Q. V. (2014). Sequence to sequence learning with neural networks. In Advances in Neural Information Processing Systems (Vol. 27). Curran Associates. https://proceedings.neurips.cc/paper_files/paper/2014/hash/5a18e133cbf9f257297f410bb7eca942-Abstract.html §
  • Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., Kaiser, Ł., & Polosukhin, I. (2017). Attention is all you need. In Advances in Neural Information Processing Systems (Vol. 30). Curran Associates. https://proceedings.neurips.cc/paper_files/paper/2017/hash/3f5ee243547dee91fbd053c1c4a845aa-Abstract.html §
  • Wu, Y., Schuster, M., Chen, Z., Le, Q. V., Norouzi, M., Macherey, W., Krikun, M., Cao, Y., Gao, Q., Macherey, K., Klingner, J., Shah, A., Johnson, M., Liu, X., Kaiser, Ł., Gouws, S., Kato, Y., Kudo, T., Kazawa, H., . . . Dean, J. (2016). Google’s neural machine translation system: Bridging the gap between human and machine translation (arXiv:1609.08144). arXiv. https://doi.org/10.48550/arXiv.1609.08144 §
  • Yedetore, A., Linzen, T., Frank, R., & McCoy, R. T. (2023). How poor is the stimulus? Evaluating hierarchical generalization in neural networks trained on child-directed speech. In A. Rogers, J. Boyd-Graber, & N. Okazaki (Eds.), Proceedings of the 61st Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers) (pp. 9370–9393). Association for Computational Linguistics. https://doi.org/10.18653/v1/2023.acl-long.521 §