AI Fundamentals
Chapter 12 · Sequences and Forecasting: RNNs and LSTMs

Watch a network remember

Two experiments with networks that have memory. First, draw a pattern and watch a SimpleRNN and an LSTM learn it from sliding windows, then continue it into the future. Then a memory test: the answer is hidden at the start of a long, noisy sequence. Watch which network still remembers it at the end. Everything trains here in your browser, in seconds; nothing is uploaded.

1 · Draw a pattern, the network continues it §5.3

Draw a wiggly line from left to right in the box (mouse or finger), or pick a pattern. The demo cuts it into overlapping windows: each window of timesteps numbers is one example, and the number right after it is the answer. Then both networks learn from the same windows, and each continues your pattern one step at a time, feeding its own guesses back in.

Your pattern

Cut into windows

Train both, then continue

your patternhow it really goes onSimpleRNNLSTM
SimpleRNN loss (mse)–
LSTM loss (mse)–
SimpleRNN lossLSTM loss
The Keras code for these settings

  

This part trains real TensorFlow.js simpleRNN and lstm layers, the browser version of the Keras layers above, on your computer's processor (for networks this small it is faster than the graphics card). Starting weights and the order of the windows are seeded, so the same settings give the same result each time; Colab's numbers will differ a little.

2 · The memory test §3–4

Each sequence starts with a secret: +1 or −1, flagged as the one to remember. Then come N loud random numbers between −3 and 3, which tell you nothing. At the very end the network must say what the secret was. Guessing gets 50%. The longer the gap N, the longer the secret must survive in the network's memory. Chapter 12 says a simple RNN's memory fades over many steps and an LSTM's cell state can carry it. Test it.

One sequence, gap N = 25

Each epoch is 256 fresh sequences. A run stops early once its loss is below 0.02. The sweep trains 16 networks: about 10 to 20 seconds.
SimpleRNN test accuracy–
LSTM test accuracy–
SimpleRNNLSTMguessing (50%)

Accuracy as the gap grows

SimpleRNNLSTMguessing (50%)

Inside the memory, from your last single run

Take one sequence and its twin: the same noise, the opposite secret. The line shows how different each network's hidden state (its memory, the numbers it passes to the next step) is for the two, on average per unit. If it falls to zero, the network can no longer tell the twins apart: the secret is gone.

SimpleRNN memory of the secretLSTM memory of the secret

And the LSTM's gates for its memory cell that holds the secret most strongly. A forget gate near 1 means “keep what you have”; an input gate near 0 means “ignore what comes in”. A trained network is rarely that tidy: see what this one actually learned.

forget gateinput gate
The same test in Keras

  

This part trains 16 networks for the sweep, so it uses a small SimpleRNN and LSTM written in plain JavaScript (the file seqnet.js beside this page): the same equations, starting weights and Adam optimizer as the Keras layers in the code above, without the overhead of a general library. Seeded, so a run repeats on the same computer; another computer or browser may give different numbers. LSTMs are not infinite memory: in this small training budget the LSTM usually holds on to about 30 to 40 steps, and past that it often fails too. With more training it can go further; the SimpleRNN generally does not.