Inside an LSTM
Give a network one thing to remember, then distract it. See the gates keep what a plain RNN forgets.
Give a network one thing to remember, then distract it. See the gates keep what a plain RNN forgets.
A memory test for small networks. Each one sees a letter to remember (A, B, C or D), then a run of random digits to distract it, then a question mark. At the “?” it must say which letter came first. They read one symbol at a time.
One is a plain RNN, like the one in Inside an RNN: its only memory is 8 numbers, h. The other is an LSTM (long short-term memory): it keeps 8 numbers in h and 8 more in a second memory, the cell state c, and three gates decide what to forget, what to write and what to show. Both trained on tests with 1 to 200 digits. The question: what do the LSTM’s gates do to carry the letter through a long run of distractions, and why did the plain RNN, trained the same way, fail to learn the test?
The test: 22 symbols (20 digits)
LSTM at the “?”right
Plain RNN at the “?”wrong
Up: % of tests right. Along: digits before the “?”. Up to 5 digits, every possible test; after that, 1,000 new random tests at each length. Shaded: 1 to 200 digits, the lengths the LSTM and the plain RNN trained like it saw in training; dotted: a blind guess (25%).
From 4 digits on, the plain RNN trained like the LSTM is within 5 points of a blind guess. The one trained on short tests gets every test we ran right up to 74 digits, at least 95% of them up to 122, and still 76% at 300. The LSTM got every one of the 295,000 random tests (and every possible test up to 5 digits) right, at every length up to 300 digits, though it trained only up to 200.
The plain RNNs. A plain RNN can hold the letter: one trained on short tests (1 to 40 digits) gets every test we ran right up to 74 digits, at least 95% of them up to 122, and still 76% at 300 (the dashed orange line). But trained like the LSTM, on 1 to 200 digits, it hardly learnt the test: even with 1 digit it is right only 58% of the time, and from 4 digits on it is within 5 points of a blind guess (25%). The difference is learning across long gaps. To learn to keep the letter, a network needs a signal that travels back from the “?” to the first step; in a plain RNN that signal shrinks at every step, and over a long test almost nothing of it is left (the vanishing gradient). Its gaps were drawn evenly from 1 to 200, so 80% of its training tests had more than 40 digits: nearly all it saw was tests it could not learn from, which is why it never got good even at short ones. Plain RNNs trained this way also differ a lot from one random start to the next; this is one run.
The LSTM carries its second memory, the cell state c, from step to step by multiplying it by the forget gate and adding what the input gate lets in: ct = f ⊙ ct−1 + i ⊙ g. With f near 1 and i near 0, c passes along almost untouched, like a parcel on a conveyor belt (over hundreds of digits it slowly grows, as the input gate is never quite shut), and the output gate decides when it shows in h. The same path lets the training signal travel back without shrinking much. Ours got every one of the 295,000 random tests (and every possible test up to 5 digits) right, at every length up to 300 digits, though it trained only up to 200.
What its units learnt. Nobody told the units what to do; we found out by running the trained network on 2,000 tests of 50 digits. All 8 units hold part of the letter. During the digits their forget gates average 0.999, their input gates 0.020 and their output gates 0.021; at the letter the input gates open to 0.69, and at the “?” the output gates open to 0.86.
How they were trained. Both main networks: 4,000 rounds of 64 random tests with 1 to 200 digits: the same stream of random tests, the same optimiser and the same seed (their starting weights differ, as the networks differ in size). Two things differ, both for the LSTM. Its forget gates started open (bias 3, so f was about 0.95 before training), as is usual for LSTMs. And its training added a small cost, 0.05 times the average input gate after the letter plus the average output gate before the “?”, so it learnt to open those only when it must; the forget gate was not charged. Without that cost it still learnt the test, but kept its input gates half open: right, and much harder to read.
Before transformers arrived in 2017, LSTMs were the usual network for language, in machine translation for example. A transformer keeps no running memory at all: each word looks straight back at every earlier word through attention.
The plain RNNs. A plain RNN can hold the letter: one trained on short tests (1 to 40 digits) gets every test we ran right up to 74 digits, at least 95% of them up to 122, and still 76% at 300 (the dashed orange line). But trained like the LSTM, on 1 to 200 digits, it hardly learnt the test: even with 1 digit it is right only 58% of the time, and from 4 digits on it is within 5 points of a blind guess (25%). The difference is learning across long gaps. To learn to keep the letter, a network needs a signal that travels back from the “?” to the first step; in a plain RNN that signal shrinks at every step, and over a long test almost nothing of it is left (the vanishing gradient). Its gaps were drawn evenly from 1 to 200, so 80% of its training tests had more than 40 digits: nearly all it saw was tests it could not learn from, which is why it never got good even at short ones. Plain RNNs trained this way also differ a lot from one random start to the next; this is one run.
The LSTM carries its second memory, the cell state c, from step to step by multiplying it by the forget gate and adding what the input gate lets in: ct = f ⊙ ct−1 + i ⊙ g. With f near 1 and i near 0, c passes along almost untouched, like a parcel on a conveyor belt (over hundreds of digits it slowly grows, as the input gate is never quite shut), and the output gate decides when it shows in h. The same path lets the training signal travel back without shrinking much. Ours got every one of the 295,000 random tests (and every possible test up to 5 digits) right, at every length up to 300 digits, though it trained only up to 200.
What its units learnt. Nobody told the units what to do; we found out by running the trained network on 2,000 tests of 50 digits. All 8 units hold part of the letter. During the digits their forget gates average 0.999, their input gates 0.020 and their output gates 0.021; at the letter the input gates open to 0.69, and at the “?” the output gates open to 0.86.
How they were trained. Both main networks: 4,000 rounds of 64 random tests with 1 to 200 digits: the same stream of random tests, the same optimiser and the same seed (their starting weights differ, as the networks differ in size). Two things differ, both for the LSTM. Its forget gates started open (bias 3, so f was about 0.95 before training), as is usual for LSTMs. And its training added a small cost, 0.05 times the average input gate after the letter plus the average output gate before the “?”, so it learnt to open those only when it must; the forget gate was not charged. Without that cost it still learnt the test, but kept its input gates half open: right, and much harder to read.
Before transformers arrived in 2017, LSTMs were the usual network for language, in machine translation for example. A transformer keeps no running memory at all: each word looks straight back at every earlier word through attention.
Things to try