Lesson 6.4core3 h

Recurrent neural networks and BPTT

Sharing one set of weights across time — and paying for it in gradients

Recurrent networks were the first neural architecture to model sequences of arbitrary length, and they ruled NLP from 2014 to 2017. Their equations are simple, but deriving backpropagation through time exposes the product of Jacobians that makes gradients vanish or explode — the single most important training pathology in deep learning, which also explains why residual connections (4.6 Initialization and normalization), gating (6.5 LSTM and GRU) and ultimately attention (7.1 Attention Is All You Need: the big picture) were invented.

You'll be able to

  • Classify sequence tasks (one-to-many, many-to-one, many-to-many) and write the RNN recurrence
  • Derive backpropagation through time and the Jacobian product ∏ Wᵀ diag(σ′) that controls gradient flow
  • Explain vanishing and exploding gradients with a bound and a numeric example, and apply gradient clipping and truncated BPTT
  • Train and sample from a character-level RNN language model

Key ideas

  1. An RNN carries a hidden state hₜ = tanh(W_hh hₜ₋₁ + W_xh xₜ + b) and reuses the same weights at every step; parameters do not grow with sequence length.
  2. Unrolling over T steps gives a T-layer network with tied weights; BPTT is ordinary reverse-mode autograd on that graph.
  3. ∂hₜ/∂hₖ = ∏ W_hhᵀ diag(tanh′): a product of t−k Jacobians controls how far gradients travel.
Show 4 more key ideas
  1. ‖∂hₜ/∂hₖ‖ ≤ (λγ)^{t−k}: gradients vanish when λγ < 1 and may explode when λγ > 1 (0.9⁵⁰ ≈ 0.005, 1.1⁵⁰ ≈ 117).
  2. Clip gradients for explosions; use gating (LSTM/GRU), careful initialization or residual paths for vanishing; truncate BPTT for cost.
  3. Train language models with teacher forcing and masked padding, evaluate with perplexity, generate by sampling with temperature.
  4. RNNs cannot parallelize across time steps — the computational weakness attention and transformers fixed.

Suggested path through this lesson