0:00Intro: MOFUSA Tech Academy
Welcome to MOFUSA Tech Academy. Beyond learning, towards mastery.
0:09"Not good" vs "good, not bad": why order matters
Here are two Brightcart reviews. "The mug is not good, it's great." And "The mug is good, it's not great." Exactly the same words, opposite meanings. One customer is delighted; the other is disappointed. The bag-of-words trick from episode eight counts words and throws away their order, so it sees these two reviews as identical. Order matters for so much of our data: words in a sentence, sales day after day, notes in music, sounds in speech. To understand sequences, a network needs memory: a way to carry what it has seen so far into what comes next.
Today: recurrent neural networks, the first networks with memory, and the reason they struggle to remember for long.
1:00Where we are on the map
Season Two, Module Nine: sequences. First stop: recurrent neural networks.
1:07Why sequences need something new
We could feed a dense network a fixed window of, say, the last three words. But reviews come in all lengths, and the word that changes everything, like "not", can sit ten words before the word it affects. A fixed window misses it. And a pattern learned in one position wouldn't automatically work in another. What we need is a network that can read sequences of any length, keeps track of order, and reuses the same weights at every step, just as a convolution reuses the same filter at every position in an image.
1:45The recurrent cell and hidden state, by hand
A recurrent neural network, or R-N-N, reads a sequence one step at a time and keeps a hidden state: a running memory of everything it has read so far. At each step, it combines two things: its previous memory, multiplied by a memory weight, and the new input, multiplied by an input weight. Add a bias, squash the result with tanh, and that's the new memory. The crucial detail: the same weights are used at every step. Let's run a tiny one by hand. We give each word a score: "okay" is zero point five, "late" is minus one, "lovely" is plus one.
The memory starts at zero. First word, "okay": tanh of zero point five, so the memory is zero point four six. Mildly positive. Second word, "late": zero point eight times the old memory, minus one, which is minus zero point six three; tanh gives minus zero point five six. The mood has turned negative. Third word, "lovely": zero point eight times minus zero point five six, plus one; tanh gives zero point five. Recovered, but notice it's not as high as if "late" had never appeared. The memory carries the history forward.
3:08Unrolling through time
To understand how an RNN learns, we unroll it. Instead of one cell with a loop, draw a copy of the cell for every step of the sequence, each passing its memory to the next. It's the same cell, with the same weights, repeated through time. In real networks, each input is a vector, like a word turned into numbers, which we'll study properly in Module Ten, and the hidden state is a vector of a few hundred numbers. The weights become matrices, but the idea is exactly what we just did by hand. Notice what the unrolled picture really is: a very deep network, with one layer per word, where every layer shares the same weights.
Hold on to that. It explains both how RNNs learn and why they struggle.
4:01Shapes of sequence problems
RNNs fit several shapes of problem. Many-to-one: read a whole review and output a single sentiment. Many-to-many: produce an output at every step, like tagging which words in a review are product names. One-to-many: generate a sequence from a single input. And sequence-to-sequence: read one sequence and produce a different one, possibly of a different length, like turning a customer's message into a reply, or translating a sentence. That's next episode. One pattern deserves special attention: at every step, predict the next word.
Train a network to do that on enough text, and it learns a surprising amount about language. That simple task is exactly how large language models are trained, and we'll come back to it in Season Three.
4:52Backpropagation through time (BPTT)
Training an RNN uses the backpropagation we already know, applied to the unrolled network. It's called backpropagation through time. The loss sits at the end, or at every step, and the error signal flows backwards along the chain of hidden states, one time step after another, multiplying by the memory weight and by the slope of tanh at each step. Because every step uses the same weights, each time step contributes its own piece of the gradient, and the pieces are added together into a single update for the shared weights.
5:31Why RNNs forget: vanishing and exploding gradients
Here's the problem. Remember from episode twenty-eight that gradients are products of many factors. In an RNN, the error signal is multiplied by roughly the same factor at every time step, the memory weight times the tanh slope. Suppose that factor is about zero point seven two. After ten steps, the signal has shrunk to about four hundredths. After fifty steps, it's less than one ten-millionth. So if the word that decides a review's meaning, like "not", is fifty words back, the network gets essentially no learning signal connecting them.
In practice, simple RNNs struggle to remember beyond a few tens of steps. And if the factor is above one, say one point five, the signal explodes instead: over six hundred million after fifty steps. Gradient clipping handles explosions. Practitioners also often backpropagate through only the most recent fifty or a hundred steps, called truncated backpropagation through time, to save memory. But the vanishing problem needs a smarter cell, one designed to protect its memory. That's the LSTM, next episode.
6:45Bidirectional RNNs
One useful variation: a bidirectional RNN runs two networks over the sequence, one reading forwards and one reading backwards, and combines their memories at every position. That way each word is understood in light of what comes before and after it, which helps with tasks like tagging. It only works when you have the whole sequence in advance, though, not when you're predicting the next word in real time.
7:14Try it yourself: change one word
Let's prove that an RNN cares about order. Same three words, reversed: "lovely", then "late", then "okay". Will the final memory be the same as before? Pause and work it through, with the memory weight zero point eight. After "lovely": tanh of one, about zero point seven six. After "late": zero point eight times zero point seven six, minus one, which is minus zero point three nine; tanh gives about minus zero point three seven. After "okay": zero point eight times minus zero point three seven, plus a half, about zero point two; tanh keeps it at zero point two.
The original order ended at zero point five; the reversed order ends at zero point two. Same words, different meaning, and unlike a bag of words, the RNN can tell the difference, because what it ends with depends on what came before.
8:23How many parameters? (it doesn't grow with length)
How many parameters does an RNN have? With inputs of a hundred numbers and a memory of a hundred and twenty-eight, the input weights, memory weights and biases come to twenty-nine thousand, three hundred and twelve. And here's the elegant part: that number doesn't change with the length of the text. The same weights read a five-word review or a five-hundred-word review; only the unrolled chain gets longer. A dense network looking at a fixed window would need separate weights for every position.
8:57Generating text, one character at a time
RNNs can also write. Train one to predict the next character of Brightcart's support messages. To generate, give it a starting letter, let it predict a probability for every possible next character, pick one, feed it back in, and repeat. Early in training, it produces random characters, then plausible letter pairs, then real words, then whole phrases. In Season Three, we'll train a tiny transformer to do exactly this and watch that progression with real numbers. The task, predicting the next token, is the same one that trains today's large language models.
9:37Teacher forcing and exposure bias
There's a subtle gap between how these models are trained and how they're used. During training, at every step, the model is given the true previous word, called teacher forcing. But when it generates on its own, it's fed its own previous words. If it makes one early mistake, it's now in a situation it never practised, and it can drift further off course. Researchers call this exposure bias. Mixing some of the model's own predictions into training, and smarter decoding methods, help, and we'll return to them in episode thirty-four and in Season Three.
10:16RNNs for time series
RNNs are also used for time series, like forecasting Brightcart's daily orders from the last thirty days, together with features such as the day of the week and planned promotions. But remember the baselines from episodes six and eight: last week's value, a rolling average, or gradient-boosted trees with lag features are often surprisingly hard to beat. Use an RNN when it clearly wins on rolling validation, always testing on days that come after the training data.
10:47Common RNN mistakes
Some mistakes are specific to sequence models. When sequences of different lengths are padded to the same length, the padding must be masked, or the model treats it as real input. The hidden state must be reset between unrelated sequences, or one customer's review bleeds into the next. Without gradient clipping, losses can explode. Shuffling time-series data across time leaks the future into training. And backpropagating through very long windows can exhaust memory, which is exactly why truncation exists.
11:22Try it yourself: twenty multiplications
Here's the vanishing and exploding problem in four numbers. Suppose each step back in time multiplies the gradient by the same factor. After twenty steps, what's left? Pause and guess for a factor of a half, and for one and a half.
A half, twenty times, leaves less than one millionth: the gradient has vanished. Zero point nine leaves about twelve percent. One point one grows to almost seven. And one and a half explodes to more than three thousand. Only a narrow band near one is safe, and a simple RNN has no reliable way to stay there. That's the problem the next episode's gated networks were designed to solve.
12:121D convolutions for sequences
RNNs aren't the only way to read a sequence. The convolutions from episode twenty-nine also work in one dimension: a filter slides along the words, three at a time. Stack layers with growing gaps, one, two, four, eight, and with just four layers each output can see thirty-one words; with six layers, a hundred and twenty-seven. Convolutions process every position at once, so they train quickly, but they only see a fixed window. RNNs carry an open-ended memory, but they process one step at a time. That trade-off, between parallel speed and long memory, is exactly what the transformer will resolve.
12:55Myth vs Fact
Myth: an RNN remembers everything it has read. Fact: its entire memory is one fixed-size vector, rewritten at every step. A thousand-word review has to be squeezed into, say, a hundred and twenty-eight numbers, and older details tend to fade or get overwritten. That bottleneck is exactly what later ideas, attention and then transformers, set out to remove.
13:23Recap
Let's recap. Sequences need models that respect order and handle any length. An RNN keeps a hidden state, updated at each step from the old state and the new input, using the same weights every time. Unrolled, it's a deep network through time, trained with backpropagation through time. And because the gradient is multiplied by similar factors at every step, it vanishes or explodes over long sequences, so simple RNNs have short memories. One sentence to remember: read one step at a time, and carry a memory forward.
14:03Next: LSTMs and GRUs
Mission complete. Brightcart's network now reads reviews word by word, with a memory that tracks the mood. "Not good, it's great" and "good, it's not great" finally look different. But long reviews still defeat it. Next episode, we meet the cells that fixed the memory problem: the LSTM and the GRU, with their ingenious gates that decide what to forget, what to store, and what to reveal. See you on the map.