What an LSTM is
The RNN tab ends with gradients that vanish within a few dozen characters. The long short-term memory (LSTM) adds a second vector, the memory cell , which is updated only by multiplying and adding, and three gates that decide what to write into it, what to keep and what to show.
The network on this page has the RNN tab's shape: character vectors of 64 numbers, one LSTM layer of 256 units and a softmax over 65 characters, 350,593 parameters. It was trained here on Tiny Shakespeare for 4,000 steps of 64 sequences of 128 characters, with Adam at learning rate 0.002, gradients clipped to norm 1 and seed 0, in 41 seconds on the Mac's GPU.
Four gates
At each step one matrix product of gives four vectors of 256:
The input gate , the forget gate and the output gate go through a sigmoid, so each entry lies between 0 and 1 and acts as a soft switch. The candidate goes through tanh: it is the new content the cell may take in. The Four gates panel draws them for the selected character and counts the units that keep more than 90 % of their memory (), the units that drop almost all of it, and the units that let in more than half of their candidate.
The memory cell
The cell keeps what the forget gate lets through and adds what the input gate lets in; the output is a gated, squashed view of it:
There is no matrix and no squashing on the path from to . A unit with and carries its value unchanged from step to step, and is unbounded while stays inside . The memory cell panel shows this sum for the current character, and the heatmaps below it show and over the whole speech.
Why the gradient survives
Along the cell, going back one step multiplies the gradient by the forget gate and nothing else:
Where is near 1 the gradient passes almost unchanged, and since is computed from the input the network learns when to hold and when to let go. Hochreiter and Schmidhuber's original cell (1997) had no forget gate: its self-connection was fixed at 1, a constant error carousel, so a cell could never be reset. Gers, Schmidhuber and Cummins (2000) added the forget gate; that is the LSTM used here and almost everywhere since. The RNN tab measures the difference on the same validation windows: 100 characters back the LSTM's cell keeps of the last step's gradient, the RNN's hidden state .
Cells that track the text
With a path that can hold a value, some cell units become counters. Over 5,000 validation characters, the cell value of unit 205 follows the number of characters since the start of the line with ; the next best unit reaches . Others follow whether the text is inside a speaker's name (unit 73, ) and the characters since the last space (unit 192, ). Nobody told the network to count: knowing how far into a line it is helps it predict the line break. The Cell units that track the text panel plots the chosen unit beside its counter. Karpathy, Johnson and Fei-Fei found cells of the same kind, tracking line length, quotes and brackets, in character LSTMs.
Training
The loss is the cross-entropy of every next character, as in the RNN tab. Guessing uniformly over 65 characters costs nats, and the validation loss starts at 4.179. It is lowest, 1.541, at step 2,700; after that the training loss keeps falling while the validation loss rises a little, to 1.564 at step 4,000, the start of overfitting. Adam (Kingma and Ba) scales each weight's step by running averages of its gradient and of the gradient's square. The Sampled text panel shows text sampled at temperature 0.8 from several checkpoints: random characters before training, and by step 4,000 speeches with speakers' names, line breaks and mostly real words.
Where it came from
Hochreiter's 1991 thesis diagnosed the vanishing gradient, and the LSTM (1997) was the answer to it; the forget gate (2000) and other refinements followed. Graves (2013) showed LSTMs generating text and handwriting, and in 2014 LSTMs and GRUs powered the first neural translation systems, the encoder–decoders of the next tab. Greff et al. compared eight LSTM variants on three tasks and found none significantly better than the standard cell, with the forget gate and the output activation its most critical parts. The GRU, a lighter gated cell with no separate memory, appears in the seq2seq tab. Transformers later replaced recurrence altogether.
Papers
- Long Short-Term Memory The LSTM: a memory cell with a constant error carousel and multiplicative input and output gates.
- Learning to Forget: Continual Prediction with LSTM Adds the forget gate, which lets a cell reset itself; the cell this tab trains.
- Generating Sequences With Recurrent Neural Networks LSTMs that predict the next character or pen position, sampled to generate text and handwriting.
- Visualizing and Understanding Recurrent Networks Interpretable cells in character LSTMs, such as line-length counters, like the units on this page.
- LSTM: A Search Space Odyssey A large comparison of LSTM variants: the forget gate and the output activation matter most.
- Adam: A Method for Stochastic Optimization The optimiser this tab, the RNN tab and the seq2seq tab train with.