Recurrent Neural Networks (RNNs)
Prerequisites
- Backpropagation — Chain rule and gradient flow
- Activation Functions — tanh, softmax
🎯 Learning Objectives
- Understand why feedforward networks fail on sequential data
- Write the RNN hidden state equation
- Unfold an RNN in time and count parameters
- Explain Backpropagation Through Time (BPTT)
- Describe the vanishing / exploding gradient problem in RNNs
1. Why Sequences Need Recurrence
Feedforward networks take a fixed-size input and produce a fixed-size output. Many real-world tasks involve variable-length sequences:
- Language modelling: predict the next word given all previous words.
- Speech recognition: audio frames → text.
- Time-series forecasting: stock prices, weather data.
An RNN processes one element at a time while maintaining a hidden state that summarises the history of all previous elements.
2. RNN Architecture
Basic RNN Equations
ht = tanh(Whh · ht−1 + Wxh · xt + bh)
yt = Why · ht + by
Where:
xt ∈ ℝd — input at time step t
ht ∈ ℝn — hidden state at time t
yt ∈ ℝm — output at time t
Wxh ∈ ℝn×d — input-to-hidden weights
Whh ∈ ℝn×n — hidden-to-hidden weights
Why ∈ ℝm×n — hidden-to-output weights
Key Property: Parameter Sharing
The same weights Wxh, Whh, Why are used at every time step. This is analogous to how CNN filters are shared across spatial locations.
3. Unfolding in Time
The compact RNN diagram (with a self-loop on h) can be unfolded into a deep feedforward network where each “layer” corresponds to one time step:
h0 → h1 → h2 → ... → hT
↑ ↑ ↑ ↑
x0 x1 x2 xT
↓ ↓ ↓
y1 y2 yT
Despite being “deep” (T layers), the unfolded network shares weights across all layers.
4. Parameter Counting
Total RNN Parameters
Params = n(d + n + 1) + m(n + 1)
= |Wxh| + |Whh| + |bh| + |Why| + |by|
= nd + n² + n + mn + m
Example: Language Model
Vocabulary size d = 10,000, hidden size n = 256, output size m = 10,000.
Wxh: 256×10000 = 2,560,000
Whh: 256×256 = 65,536
bh: 256 = 256
Why: 10000×256 = 2,560,000
by: 10000 = 10,000
Total = 5,195,792
Note: The parameter count is independent of sequence length T.
5. Backpropagation Through Time (BPTT)
BPTT is simply backpropagation applied to the unfolded RNN graph.
BPTT Algorithm
- Forward pass: Compute h1, h2, …, hT and y1, …, yT. Store all intermediate values.
- Compute loss: L = ∑t=1T Lt(yt, targett)
- Backward pass: For t = T, T−1, …, 1:
δt = ∂L/∂ht ∂L/∂Whh += δt · ht−1&topsuperscript; ∂L/∂Wxh += δt · xt&topsuperscript; - Update weights: θ ← θ − η · ∇L
Truncated BPTT
For very long sequences, full BPTT is expensive. Truncated BPTT limits backpropagation to the last k time steps, trading accuracy for speed.
6. Vanishing & Exploding Gradients in RNNs
The gradient with respect to h0 involves a product of Jacobians through all T time steps:
∂hT/∂h0 = ∏t=1T ∂ht/∂ht−1 = ∏t=1T diag(tanh'(zt)) · Whh
The Fundamental Problem
- If λmax(Whh) < 1: gradients shrink exponentially → vanishing (cannot learn long-range dependencies).
- If λmax(Whh) > 1: gradients grow exponentially → exploding (training diverges, NaN losses).
Solutions
| Solution | Addresses | Mechanism |
|---|---|---|
| Gradient clipping | Exploding | Clip ‖∇‖ to max threshold |
| LSTM / GRU | Vanishing | Gated memory preserves gradients |
| Skip connections | Vanishing | Direct gradient paths |
| Orthogonal init | Both | Keeps eigenvalues near 1 |
7. Sequence Architectures
| Type | Input → Output | Example |
|---|---|---|
| One-to-Many | 1 → T | Image captioning |
| Many-to-One | T → 1 | Sentiment analysis |
| Many-to-Many (synced) | T → T | POS tagging |
| Many-to-Many (encoder–decoder) | T1 → T2 | Machine translation |
Bidirectional RNN
A bidirectional RNN runs two separate hidden states: one forward (left-to-right) and one backward (right-to-left). The outputs are concatenated:
ht = [ht→ ; ht←] ∈ ℝ2n
This allows each position to have context from both the past and the future.
Summary
- RNN hidden state: ht = tanh(Whh ht−1 + Wxh xt + b).
- Parameters are shared across time; count is independent of T.
- BPTT unfolds the RNN and applies backprop; gradient is a product of T Jacobians.
- Vanishing gradients prevent learning long-range dependencies → solved by LSTM/GRU.
- For GATE: know the RNN equations, parameter counting, and why vanilla RNNs struggle with long sequences.