2.1 Sequential Models and RNNs
A shared cell, then an RNN-LM: next-token softmax from the hidden state.
These notes match the lecture slides. Use (slides) on the course hub for the deck.
An MLP sees a vector and forgets. Language is a sequence: the meaning of not depends on what came before, so a recurrent net keeps a state and updates it at every token with the same weights. This week is the historical path to transformers: LSTMs still show up in speech and time series, and the failure mode (a chain of Jacobians) is why Week 3 exists.
1. What an RNN is
An RNN is a net that shares one cell across time. At step the cell takes the new token embedding and the previous hidden state , and emits , which is supposed to hold both. That vector is the memory.

The vanilla (Elman) recurrence is
The same are used at every step. That is weight sharing along time, the sequence analogue of a CNN’s shared filter. An RNN language model then predicts the next token from with a linear map and a softmax.
Between two independent sequences (two reviews in a batch) you reset . The loop lives inside one sequence. If you do not reset, review 2 starts with leftover sentiment from review 1. That is leakage, not transfer learning.
2. Why we use it
A language model predicts the next token. Week 1 already wrote
An n-gram approximates the history by the last words and counts. Raise and the table is sparse; most strings never occurred. A fixed-window MLP concatenates the last embeddings and is dense, but is still a hard cutoff and each position in the window has its own weights. A bag of words cannot count “the verb agrees with the noun 40 tokens back” without growing width.
You need a map that (i) accepts any length, (ii) shares weights across time, and (iii) can, in principle, use the beginning of the sentence. That map is a recurrent cell.
Do not build an n-gram this week. The counting story is only here so the cell has a job.
3. Architecture
Write the loop as a chain. Four tokens, one set of weights:

A CNN looks at a fixed neighborhood, in parallel. An RNN looks at everything so far, in order, and cannot fully parallelize across time. That is the accuracy / speed tradeoff.
Unrolling is how you backprop (note 2.2). The picture is one graph with copies of . There are not four different matrices named .
If the first word is the subject of a verb twenty tokens later, must still contain that subject. The cell is a lossy compressor: every step overwrites. Week 2.2 is what happens to the gradient on that long path. Week 2.3 adds gates so the cell can choose what to keep.
The same cell, different wiring:

- Many-to-one (sentiment): read , emit from the last . Lab 2 is this shape: copy the first bit at the end.
- Many-to-many, aligned (tagging, language modeling): emit at every .
- Encoder then decode: compress the source into one vector, then generate. That bottleneck is why Week 3 exists. Do not train seq2seq this week.
An RNN language model looks up the token id, runs the cell, then a linear map plus softmax over the vocabulary.

4. How it works, step by step
Read left to right. The same at every step.
- Reset at the start of the sequence.
- Lookup , mix it with through the shared cell, apply , get .
- For language modeling, map to vocabulary logits and take a softmax. The loss at step is the same next-token NLL as Week 1:
- Average over tokens in the sentence (or a small batch of sentences). That is teacher forcing: the true previous token is the next input, not the model’s sample.
Perplexity is in nats, or if you used log base 2. Lower is better. You will not chase a Wikipedia perplexity number this week. You will chase whether Lab 2’s first bit is still in .
Generation (optional picture): sample from the softmax, feed it back as . Training does not do that.
A numeric two-step unroll is in the worked example. The teaser you should already see: token 1’s footprint in shrinks. That shrinkage is vanishing gradients, next note.
5. Mathematical formulas
The cell:
The joint factorization of a sequence, which the RNN-LM approximates by conditioning on instead of the raw history:
Next-token loss at step :
Perplexity is in nats (or in bits). Lower is better.
6. Positive points and negative points
Positive.
- Variable length: one cell, any , without growing a new table of n-grams.
- Parameter sharing across time is the sequence analogue of a CNN’s shared filter.
- In principle the state can carry the beginning of the sentence, which a fixed window cannot.
- The same cell wires as many-to-one (Lab 2) or many-to-many (language modeling).
Negative.
- Sequential: you cannot fully parallelize across time, unlike a transformer.
- The cell is a lossy compressor. Every step overwrites, so early tokens shrink in later states.
- Vanishing and exploding gradients on the unrolled chain (next note) kill long-range learning.
- Forgetting to reset between batch items leaks one document into the next.
- Training uses teacher forcing; sampling during training is not the RNN-LM loss.
When not to. If you need every token to see every other token in one hop, this is the wrong mixing layer. That is Week 3. This week you need the recurrent picture so the Jacobian product has somewhere to live.
7. Teaching this note
~18 minutes. Why a window is not enough, then the cell, then unroll three tokens with shared , then the 2-D numeric table, then the RNN-LM diagram so next-token NLL has a hidden state. Play 0:00–12:00 of StatQuest RNNs (the loop and the unroll). Save vanishing gradients for note 2.2 even if the video teases them.
8. Worked example
Let , , ,
Step 1: , so because .
Step 2: , then .

The same was used twice. Token 1 still has a shrunk footprint in (). That shrinkage is the teaser for vanishing gradients.
9. Where students get stuck
- Drawing a new at each time step (no sharing).
- Feeding the whole sentence as one concatenated vector “to make it an MLP.”
- Forgetting to reset between batch items.
- Thinking an RNN-LM samples during training. It uses the true previous token.
10. Video
Watch StatQuest: Recurrent Neural Networks, Clearly Explained.
Pause on the first unroll (same weights copied) and on the moment the hidden state is described as memory. Skip any long software demo; the board math is the point.
11. Practice
-
Unroll three tokens on paper with , . Which matrices are reused?
-
Why reset the state between two unrelated documents in a batch?
-
Name one thing an RNN can mix that a kernel of size 5 cannot, in a single layer.
-
Using the worked example’s and , write the pre-activation with . Do not bother with if you are short on time; the linear mix is the lesson.
-
If every were replaced by , and after , approximately how does scale with ? (Geometric sequence.)
-
In one sentence: what is the loss at step of an RNN language model? What is teacher forcing?
-
Many-to-one or many-to-many: which wiring is Lab 2, and which wiring is next-token language modeling?