Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.A network with a memory
Reading a sentence, you keep a running sense of what it is about. A recurrent network does the same: it reads one word, sound or sensor reading at a time and updates a small memory, its hidden state. It uses the same rule at every step, so it can read sequences of any length. The catch is that old memories fade quickly. The demo shows how the learning signal from the end of a sequence shrinks, or blows up, as it travels back.
Sensor streams, telemetry, speech and control signals are sequences. RNNs were the first neural networks to model them, and their recurrence is the basis of LSTMs, GRUs and modern state-space models.
- 1. An RNN's memory is its…
- 2. An RNN uses at every step…
- 3. Old information in a vanilla RNN tends to…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Recurrence: hₜ = tanh(xₜWₓ + hₜ₋₁Wₕ + b), and the output is yₜ = hₜV + c.
- For a scalar RNN with Wₓ = 0.5, Wₕ = 2, b = 0, h₀ = 0 and x₁ = 1: h₁ = tanh(0.5) = 0.462.
- Parameters: input d, hidden n and output m give dn + n² + n + nm + m. For d = 10, n = 32 and m = 5 that is 1541. The count does not depend on the sequence length.
- Unrolling: a T-step RNN is a T-layer feed-forward network whose layers share weights. Backpropagation through it is BPTT (E11-2).
- Gradient through time:
- ∂hₜ/∂hₜ₋₁ = diag(1 − hₜ²) Wₕ;
- in the scalar case each step multiplies the gradient by w(1 − hₜ²) ≤ |w|;
- with |w| = 0.9 and slope 1, after 20 steps the factor is 0.12, and after 66 steps it is below 10⁻³;
- with w = 1.1 it grows to 117 after 50 steps.
- Output patterns:
- many-to-one (classify a sequence);
- many-to-many aligned (tag each step);
- one-to-many (generate);
- encoder–decoder (E11-8).
- 1. Parameters for d = 1, n = 16, m = 1:
- 2. Gradient factor 0.9²⁰ is about…
- 3. An unrolled RNN is…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.RNNs in code
Exercise 1.
- A 3-input, 4-unit RNN over 6 steps gave BPTT gradients matching central differences.
- The parameter count matched the formula.
Practice.
- PyTorch’s
nn.RNN(input_size, hidden_size, batch_first=True)returns all hidden states and the last one. Its parameter count has two bias vectors (b_ih and b_hh). - Initialise Wₕ orthogonally or scaled so its spectral radius is near 1.
- Clip gradients by norm (
torch.nn.utils.clip_grad_norm_) against explosions. - For variable-length batches, pad and use
pack_padded_sequenceso padding does not update the state. - Vanilla RNNs are rarely the best choice now. LSTMs, GRUs (E11-3, E11-5), temporal convolutions and transformers usually train better on long sequences.
rnn = nn.RNN(input_size=1, hidden_size=16, batch_first=True)
out, h_T = rnn(x) # out: (batch, T, 16), h_T: (1, batch, 16)
logit = head(h_T[-1]) # many-to-one
loss.backward(); torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)- 1. Against exploding gradients use…
- 2. nn.RNN has how many bias vectors per layer?
- 3. Padded batches are handled with…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Why gradients vanish or explode.
- ∂h_T/∂h_t = Π diag(1 − h_k²) Wₕ over the T − t steps in between.
- If the largest singular value of Wₕ times the largest tanh slope is below 1, the product shrinks exponentially in T − t. Bengio, Simard and Frasconi (1994) showed this happens whenever the network stores information robustly.
- If Wₕ has an eigenvalue above 1 and the units are not saturated, it can grow exponentially.
- The loss gradient for a dependency of lag k is then dominated by short-range terms.
Expressiveness. With enough units, an RNN can approximate any dynamical system and is Turing complete with unbounded precision (Siegelmann and Sontag, 1995). The limit is learning, not representation.
Remedies.
- Gradient clipping (for explosions).
- Careful initialisation, such as orthogonal or identity matrices with ReLU (IRNN).
- Gated architectures that add an additive memory path (LSTM, GRU).
- Skip connections in time, or attention, which shortens paths.
- 1. ∂h_T/∂h_t is a product of…
- 2. Who showed long-term dependencies are hard for gradient descent?
- 3. An RNN's main limit is…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.How far back can a vanilla RNN remember?
History.
- M. Jordan (1986) and J. Elman (1990, “Finding structure in time”) introduced the recurrent networks now called vanilla RNNs.
- D. Rumelhart, G. Hinton and R. Williams (1986) described backpropagation through time.
- S. Hochreiter (1991) and Y. Bengio, P. Simard and P. Frasconi (1994) analysed vanishing gradients.
- R. Pascanu, T. Mikolov and Y. Bengio (2013) proposed gradient clipping.
Exercise 2.
- Task: a 16-unit tanh RNN sees a ±1 bit at the first step, then
lagsteps of unit Gaussian noise, and must output the bit at the end. - Training: 300 Adam steps of batch 64, 3 seeds.
- Measurements: test accuracy, and at initialisation the size of ∂L/∂h at the first step relative to the last.
| lag | accuracy (3 seeds) | gradient at step 1 / at last step (initialisation) |
|---|---|---|
| 5 | 1.0, 1.0, 1.0 | 1.9 × 10⁻¹ |
| 10 | 1.0, 1.0, 1.0 | 3.1 × 10⁻² |
| 20 | 0.52, 1.0, 0.85 | 1.1 × 10⁻³ |
| 50 | 0.52, 0.53, 0.51 | 3.7 × 10⁻⁸ |
- The gradient reaching the first step shrinks exponentially with the lag, by roughly 10× every 7–8 steps here.
- Learning follows. Lags up to 10 are solved. Over 12 seeds, lag 20 was solved on 7 and lag 50 on only 1; the rest stayed at chance, because the signal about the bit is buried under gradients from recent noise.
- Success at long lags is a matter of luck. Your browser’s arithmetic differs slightly from the native run, so individual seeds can come out differently; one browser run solved lag 50 on seed 0. The overall pattern is stable.
- The network could represent the solution: saturated units can hold a bit indefinitely. The failure is in learning, as theory predicts.
RNNs in AI, robotics and aerospace
- Control: recurrent networks model and control dynamical systems, from robot arms to engines, since their state mirrors a physical state.
- Telemetry: small RNNs detect anomalies in spacecraft and aircraft sensor streams on board.
- Legacy and revival: gated RNNs dominated speech and translation until about 2017. Linear recurrent models (E17) revive recurrence for long sequences.
References
- J. L. Elman (1990). “Finding structure in time.” Cognitive Science 14(2).
- D. E. Rumelhart, G. E. Hinton, R. J. Williams (1986). “Learning representations by back-propagating errors.” Nature 323.
- Y. Bengio, P. Simard, P. Frasconi (1994). “Learning long-term dependencies with gradient descent is difficult.” IEEE Trans. Neural Networks 5(2).
- R. Pascanu, T. Mikolov, Y. Bengio (2013). “On the difficulty of training recurrent neural networks.” Proc. ICML.
- H. T. Siegelmann, E. D. Sontag (1995). “On the computational power of neural nets.” J. Computer and System Sciences 50(1).