4.3 RNNs and their limits
1. Learn
Section titled “1. Learn”- Stanford CS224n: NLP with Deep Learning (Winter 2025)lecture 5 "Basic Sequence Models to RNNs" and lecture 6 "Advanced Variants of RNNs, Attention", with their slides and the shared notes
2. Bridge
Section titled “2. Bridge”One cell, reused. The MLP of 3.8 saw a fixed window of three characters. A recurrent network keeps a hidden state and updates it with the same weights at every step, so in principle it can remember anything it has read, with a parameter count independent of the length. Each step’s state also produces a prediction of the next token (CS224n RNN lecture notes).
Backpropagation through time. Unroll the network over the sequence and it is an ordinary deep network whose layers share weights, so backprop works as usual. The gradient reaching has two parts: one from step ‘s own prediction and one from every later step, passed back through . Because the weights are shared, their gradient is the sum over steps. In practice the sequence is cut into windows of a few dozen steps, carrying the hidden state across windows but not the gradient: truncated BPTT (Karpathy, The Unreasonable Effectiveness of RNNs).
Vanishing and exploding. That product of Jacobians is why plain RNNs forget: when its factors are below 1 in norm, distant steps contribute almost nothing to the gradient, and when they are above 1, it can blow up (Pascanu et al. 2013). Explosions are easy to treat: rescale the whole gradient whenever its global norm exceeds a threshold, which keeps its direction. Vanishing has no such fix in a plain RNN.
What came next. LSTMs and GRUs add gates and an additive memory cell whose Jacobian can stay near the identity, much like a residual connection (Hochreiter & Schmidhuber 1997). They still process tokens one at a time, which cannot be parallelized across the sequence during training. Attention (4.4) lets every position look directly at every earlier one: a path of length one instead of .
3. Explore
Section titled “3. Explore”stubs/m4_3_rnn.pyLoading starter file…
Prefer your own editor? Edit exercises/stubs/m4_3_rnn.py and run mlc check 4.3. The tests are identical; import the results on the progress page.
Then train on Tiny Shakespeare (locally):
cd exercises && python projects/char_rnn_shakespeare.pyWith the reference solution, 6,000 steps take under a minute. The loss falls from 3.35 (unigram) to about 2.0 on a slice of the training text and 2.5 on the held-out play, which brings new character names. Samples look like Shakespeare from a distance. The script then measures the trained network: the Jacobian of the last state with respect to the state 40 characters earlier has a norm of about 10⁻², and about 10⁻³ at 60 characters.
5. Check
Section titled “5. Check”- not yet: All tests pass (0/?)
- not yet: Checkpoint quiz ≥ 80%
Learner log: time spent and points of confusion
Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.