0:00Intro: MOFUSA Tech Academy
Welcome to MOFUSA Tech Academy. Beyond learning, towards mastery.
0:09Why simple RNNs forget the start of a review
Long reviews have a structure that defeats simple RNNs. The customer sets up a contrast at the start, "I was not expecting much", writes a hundred and eighty words about delivery and packaging, and delivers the verdict at the very end: "but honestly, it's brilliant." A simple RNN's memory is rewritten at every word, so by the end, the opening has faded away. What we need is a memory the network can protect: deliberately keep important information for as long as it's needed, and deliberately throw away what isn't. That's exactly what the long short-term memory network, the LSTM, does, using little learned valves called gates.
Today, we take one apart, step by step, with real numbers.
1:01Where we are on the map
Season Two, Module Nine, stop two: LSTMs and GRUs.
1:08Gates: learned valves
The key ingredient is the gate. A gate is a tiny neural layer with a sigmoid, so its output is between zero and one, like a valve. Multiply a signal by a gate, and zero blocks it completely, one lets it through untouched, and a half lets half through. And the gates aren't fixed. They look at the current word and the current memory, and decide how open to be, using weights learned by backpropagation like any other.
1:39The LSTM: a conveyor belt with three gates
Here's the LSTM. Along the top runs the cell state, a conveyor belt that carries long-term memory from step to step. Below it runs the hidden state, the network's working output at each moment. Three gates control the belt. The forget gate decides how much of the old memory to keep. The input gate, together with a candidate value, decides what new information to write onto the belt, and how much of it. And the output gate decides how much of the memory to reveal as the hidden state right now. Look at the heart of it: the new cell state is the forget gate times the old cell state, plus the input gate times the new candidate.
Keep some of the old, add some of the new. That plus sign is the most important symbol in this episode, and we'll see why in a moment.
2:32One LSTM step, by hand
Let's run one step with real numbers. Suppose the conveyor belt is carrying a strong memory, a value of two, from that "not expecting much" at the start of the review. The gates look at the current word. The forget gate comes out at zero point nine five: keep ninety-five percent of the memory. The input gate is a half, and the candidate new information is zero point seven six. New cell state: zero point nine five three times two, plus a half of zero point seven six two. That's one point nine one plus zero point three eight: two point two nine.
The old memory survived almost intact, and a little new information was added. The output gate is zero point seven three, so the hidden output is zero point seven three times the tanh of two point two nine, about zero point seven two. The network reveals part of what it's remembering.
3:30Remembering across 50 steps
How long can an LSTM remember? It depends on the forget gate, and the network learns to set it. If the forget gate sits at zero point nine five three for fifty steps, the memory fades to about nine percent. But if the network learns to hold it at zero point nine nine nine, the memory keeps ninety-five percent of its strength across all fifty steps. So the LSTM can learn to keep a detail like "not" safely on the belt, through a hundred and eighty words about packaging, then use it at the end. And it can learn to drop the forget gate towards zero to wipe the memory clean, say, at the start of a new review.
4:14Why LSTM gradients survive
Now, why does this fix the vanishing gradient? Look at the cell update again: forget gate times old cell, plus something new. When we backpropagate along the conveyor belt, the derivative of the new cell with respect to the old one is simply the forget gate. In a simple RNN, the gradient was squashed by a weight and a tanh slope at every step. Along the LSTM's belt, it's only multiplied by the forget gate. When the network keeps the forget gate near one, the gradient flows back across many steps almost undiminished.
If that sounds familiar, it should. It's the same idea as the residual connections from episode twenty-eight: an additive path gives the gradient a highway. The LSTM was published by Sepp Hochreiter and Jürgen Schmidhuber in nineteen ninety-seven, and the forget gate was added a few years later.
5:14The GRU: a lighter design
Much later, in two thousand fourteen, came a simpler cousin: the gated recurrent unit, or G-R-U. It merges the cell state and hidden state into one, and uses just two gates. The update gate blends the old memory with a new candidate, keeping a share of the old and taking a share of the new, like the LSTM's forget and input gates combined. The reset gate controls how much of the past to use when forming that candidate. With fewer gates, it has fewer parameters. For an input of a hundred numbers and a memory of a hundred and twenty-eight, a simple RNN has about twenty-nine thousand weights, a GRU about eighty-eight thousand, and an LSTM about a hundred and seventeen thousand.
In practice, GRUs and LSTMs often perform similarly, so the choice is usually made by experiment.
6:14Where LSTMs and GRUs shine, and their limits
LSTMs and GRUs powered a wave of progress in the twenty-tens, in speech recognition, machine translation and handwriting recognition, and they're still useful today, for example in many time-series forecasting systems, like predicting Brightcart's daily orders. But they have two lasting limitations. First, they're sequential: step five can't start until step four is done, so training can't take full advantage of a GPU's ability to do thousands of things at once. Second, everything the network knows about the past still has to fit into one fixed-size memory vector.
What if, instead of squeezing the past into one vector, the network could look back at any earlier word directly, whenever it needed to? That idea is called attention, and it's where we're heading next.
7:09Try it yourself: an LSTM step with different gates
Let's run another LSTM step, with different gate values. The forget gate is a half, the input gate is about zero point eight eight, the candidate is about zero point four six, and the output gate is a half. The old memory is two. What's the new cell value? Pause and compute.
A half times two is one. Zero point eight eight times zero point four six adds about zero point four one. The new memory is about one point four one, and the output is a half times the tanh of that: about zero point four four. Compare that with our earlier step, where the forget gate was zero point nine five and the memory grew to two point two nine. This time the network chose to forget half of its old memory and write in a good amount of new information. The gates let the same cell behave very differently, word by word.
8:15What do the gates actually learn?
What do the gates learn in practice? When researchers visualise trained LSTMs, they sometimes find cells with surprisingly clear jobs: a forget gate that closes sharply at the end of a sentence or when the topic changes, an input gate that opens at important words like "not" or "brilliant", even cells that track whether the text is inside quotation marks. Not every cell is so interpretable, but these glimpses show the gates doing exactly what they were designed for: deciding what to keep and what to let go.
8:52Stacked and bidirectional LSTMs
Like other networks, LSTMs can be stacked: the outputs of one layer become the inputs of the next, so lower layers can handle local patterns and higher layers longer-range meaning. They can also be bidirectional, reading the text forwards and backwards and combining both views, which works well for classifying or tagging a review when the whole text is available. The training tricks from episode twenty-eight, like residual connections and dropout between layers, help deeper stacks train.
9:26LSTM or GRU? A practical guide
LSTM or GRU? The GRU has fewer parameters, about eighty-eight thousand against a hundred and seventeen thousand in our example, so it's a little faster and sometimes better with limited data. The LSTM keeps a separate memory cell, which can help with very long dependencies, and it has a longer track record. In practice, accuracy is often similar, so try both and decide based on validation results, speed and memory.
10:00Where they're still used
Even in the age of transformers, recurrent networks still have niches. They process streams with a fixed-size memory, one step at a time, which suits on-device tasks like keyboard suggestions or detecting a wake word, and many time-series forecasting systems. A transformer's memory grows with the length of its input; an RNN's doesn't. That advantage is part of why newer recurrent-style designs are being explored again, as we'll see in Season Three.
10:35Training tips for LSTMs and GRUs
A few practical tips. A common trick is to start the forget gate's bias at a positive value, so the cell begins by remembering rather than forgetting, giving the gradient a clear path early in training. Clip gradients. Mask the padding in batches of different-length texts. Use moderate memory sizes with dropout between layers, and Adam with a modest learning rate. These small choices make recurrent models much easier to train.
11:06Try it yourself: one GRU step
Let's run a GRU step by hand. The old memory is zero point eight. A negative word arrives, and the candidate memory is minus zero point four. The update gate decides the blend. If it's zero point two five, what's the new memory? Pause.
Three quarters of zero point eight is zero point six. A quarter of minus zero point four is minus zero point one. The new memory is zero point five: mostly the old memory, nudged down. Now set the gate to zero point nine. A tenth of the old memory, zero point zero eight, plus nine tenths of the candidate, minus zero point three six, gives minus zero point two eight: the memory has been almost completely replaced. One small warning if you read code: some libraries write this formula with the roles of z and one minus z swapped.
The idea is identical: a learned blend between keeping and replacing.
12:13Try it yourself: how much does 0.9 forget?
Quick prediction. In Scene seven, a forget gate of zero point nine five three kept about nine percent of the memory over fifty steps. What if the gate is zero point nine instead, only a little lower? Just half a percent survives. Forgetting compounds, step after step, so small differences in the gate become enormous over long spans. That's why the network must learn to hold the forget gate very close to one for anything it needs to remember for a long time.
12:53Myth vs Fact
Myth: LSTMs solved long-term memory. Fact: they extended it dramatically compared with simple RNNs, but they still compress the past into a fixed-size state and process one step at a time. For very long texts, that remains a real limit, which is why attention and transformers took over for language.
13:15Recap
Let's recap. Gates are learned valves built from sigmoids. The LSTM uses a forget gate, an input gate with a candidate, and an output gate to manage a protected cell state. Its memory is updated by addition, keeping some of the old and adding some of the new, which gives gradients a highway back through time. The GRU does a similar job with two gates and fewer parameters. One sentence to remember: forget on purpose, and remember on purpose.
13:47Next: sequence-to-sequence and attention
Mission complete. Brightcart's review reader now carries the opening "not expecting much" across nearly two hundred words, and reads the verdict at the end correctly. Next, a harder task: read a customer's message, and write a whole reply. That needs two networks, one to read and one to write, and it led to one of the most important ideas in modern AI: attention. See you on the map.