Recurrent Neural Network
Read a sequence one step at a time, carrying a memory forward.
Type a sentence, or press Write to let the network add a letter. It reads one character at a time and guesses the next.
- memory above 0
- memory below 0
- Ttemperature
Step 23: read “.”. The memory h23 holds 20 numbers, from −1.00 to 1.00.
Best guess “␣”: score z = 20.96, and with T = 1.0 its chance is 100%.
| 1 | t | h | 96% | h | 0.1 |
| 2 | h | e | 100% | e | 0.0 |
| 3 | e | ␣ | 100% | ␣ | 0.0 |
| 4 | ␣ | l | 35% | c | 9.4 |
| 5 | c | a | 100% | a | 0.0 |
| 6 | a | t | 100% | t | 0.0 |
| 7 | t | ␣ | 99% | ␣ | 0.0 |
| 8 | ␣ | l | 40% | s | 2.3 |
| 9 | s | a | 58% | a | 0.8 |
| 10 | a | n | 73% | t | 1.9 |
| 11 | t | ␣ | 100% | ␣ | 0.0 |
| 12 | ␣ | o | 98% | o | 0.0 |
| 13 | o | n | 100% | n | 0.0 |
| 14 | n | ␣ | 100% | ␣ | 0.0 |
| 15 | ␣ | t | 80% | t | 0.3 |
| 16 | t | h | 100% | h | 0.0 |
| 17 | h | e | 100% | e | 0.0 |
| 18 | e | ␣ | 100% | ␣ | 0.0 |
| 19 | ␣ | h | 25% | m | 2.3 |
| 20 | m | a | 100% | a | 0.0 |
| 21 | a | t | 100% | t | 0.0 |
| 22 | t | . | 100% | . | 0.0 |
| 23 | . | ␣ | 100% | – | — |
Try it in Python
The Lab's trained network, its text and temperature, in Python: the same weights, so the same chances. NumPy does each step as two matrix products; Plain Python writes the sums out.
What you are looking at
A small neural network has read many short, made-up sentences, one character at a time, and learned to guess the next character. Each sentence is someone, doing something, somewhere: “the cat sat on the mat.”
Each column is one step: the character the network has just read, and under it the network's memory after reading it, 20 numbers between −1 and 1. The bars show its guesses for the next character after the selected step.
What each symbol means
- xₜ
- The character read at step t.
- hₜ
- The memory (hidden state) after step t.
- Wₓ, Wₕ, W_y
- The weights the network learned: how a character, the old memory and the memory feed into the next step.
- zₜ
- One score for every possible next character.
- T
- The temperature: how bold or cautious the guesses are.
How the network reads
At every step, the network takes the new character and its memory of everything before, mixes them with its weights, and squeezes the result between −1 and 1. That is its new memory. From the memory, it scores every possible next character.
The same weights are used at every step: the network is one small machine run over and over, passing its memory along. That loop is what makes it recurrent.
Memory
After “the c”, the network guesses “a”, because “cat” is the only word here starting with c after “the”. To know that, it must remember the “the” it read a few steps ago. Watch the memory strips: some numbers flip when a word ends, others hold steady across a word. Nobody told the network what to remember; it found out while learning.
Its memory is short, though. A plain network like this one forgets quickly, so it cannot keep track of a whole page. Bigger models add gates that decide what to keep and what to forget.
Temperature, and surprise
The scores become chances with softmax. Dividing the scores by the temperature T first changes how bold the network is: a low T makes the top guess even more likely, a high T spreads the chances out. Press Write at different temperatures: low ones repeat safe words, high ones invent strange ones.
Surprise measures how well the network predicted what really came next. Averaged over the text, it is lower the better the network knows this kind of sentence. Type something unusual and watch it rise.
How it learned
The weights started as small random numbers. The network read the training sentences, and after every 40 characters it measured its total surprise and nudged every weight a little to lower it, working back through the steps (backpropagation through time). After a few thousand rounds, it guesses most characters well. The sentences used these words: the cat, the dog, a bird, my friend, the fox, a duck, the frog, my cat; sat on, ran to, looked at, jumped on, slept on, hid in, sang to, went to; the mat, the log, the tree, the park, a box, the pond, the hill, a bed.
The model in short
- Read one character at a time.
- Remember: mix the character into the memory, using the same weights at every step.
- Guess the next character from the memory, as chances.
- Learn by nudging the weights to make the real next character less surprising.