Inside an RNN
Type a word letter by letter. Watch the network’s memory update and guess what comes next.
Type a word letter by letter. Watch the network’s memory update and guess what comes next.
A small network reads a word one letter at a time. After each letter it updates its memory, a list of 16 numbers called the hidden state, and guesses the next letter (or that the word has ended). It is a real trained model: 1,147 numbers it learnt (weights and biases) from 431 words, mostly common English words with some Indian foods and cities. Every number it works out is on screen.
The question: how does a network that sees only one letter at a time keep track of the letters before it, and how long does that memory last?
Each box is the same cell with the same weights, used once per letter. The columns are its memory after each letter (16 numbers, top to bottom); the arrows pass it on. Under each column: its top guess for the next letter.
Letter 6: “l”
In: x6, one-hot
Memory before, h5
Memory after, h6 (tap a unit)
Its guess for letter 7
The top five of its 27 outputs (a–z and “end”), plus “end” wherever it ranks. They come from h6 alone: everything it knows about the letters so far is in those 16 numbers.
Recurrent means the network feeds its own result back in: the memory h it writes after one letter is read again with the next letter. Each step is the same sum, ht = tanh(Wxhxt + Whhht−1 + bh), and the memory starts as 16 zeros.
The same weights at every step. Drawn out through time (“unrolled”) it looks like a chain of cells, but there is only one cell, used again and again. So it reads a word of any length with the same 1,147 numbers. Real language models are far bigger: in GPT-2, every token’s vector alone has 768 numbers.
How good is it? On the 431 words it trained on, its first guess was right 37% of the time. On 92 words it never saw, 34%, and the right letter was in its top three 58% of the time; always guessing “end of word” would be right 21%. It has learnt spelling patterns (q is followed by u), not a dictionary.
Why it forgets. Its memory never grows, and every step mixes the new letter in, so older letters fade (chapter 3). That is also what its training asked for: to guess the next letter of a word, the last few letters matter most, so little pushed it to keep the first one for long. To hold something over a long stretch, a network must be trained on a task that needs it, and a plain RNN finds long stretches hard to learn: see Inside an LSTM. Transformers drop the loop altogether: each word looks straight back at every earlier word through attention, and the words can be worked on all at once.
Letters a–z, up to 12. It trained on this word.
Recurrent means the network feeds its own result back in: the memory h it writes after one letter is read again with the next letter. Each step is the same sum, ht = tanh(Wxhxt + Whhht−1 + bh), and the memory starts as 16 zeros.
The same weights at every step. Drawn out through time (“unrolled”) it looks like a chain of cells, but there is only one cell, used again and again. So it reads a word of any length with the same 1,147 numbers. Real language models are far bigger: in GPT-2, every token’s vector alone has 768 numbers.
How good is it? On the 431 words it trained on, its first guess was right 37% of the time. On 92 words it never saw, 34%, and the right letter was in its top three 58% of the time; always guessing “end of word” would be right 21%. It has learnt spelling patterns (q is followed by u), not a dictionary.
Why it forgets. Its memory never grows, and every step mixes the new letter in, so older letters fade (chapter 3). That is also what its training asked for: to guess the next letter of a word, the last few letters matter most, so little pushed it to keep the first one for long. To hold something over a long stretch, a network must be trained on a task that needs it, and a plain RNN finds long stretches hard to learn: see Inside an LSTM. Transformers drop the loop altogether: each word looks straight back at every earlier word through attention, and the words can be worked on all at once.
Things to try