Recurrent Neural Networks
Source: CS1674 Ch.15 (Part B). RNNs process sequences with shared weights across time, maintaining a hidden state (memory). Vanilla RNNs suffer vanishing/exploding gradients → LSTM / GRU fix this. Key CV application: image captioning.
1. Why Sequences?
In CV, data associated with time = video. More generally, RNNs handle sequential prediction:
| Task | Example |
|---|---|
| Text classification | Sentiment ("The food was really good" → positive) |
| Text generation | Language modeling — sample next token |
| Image captioning | "A cat sitting on a suitcase" (neuraltalk2) |
| Machine translation | Sequence-to-sequence (many→many) |
Input–output scenarios
- One-to-one: feedforward network.
- One-to-many: sequence generation (captioning).
- Many-to-one: sequence classification (sentiment).
- Many-to-many: translation, captioning.
2. The Recurrent Unit (Vanilla RNN, Elman 1990)
Recurrence:
The hidden state
2.1 Cell equations
Weights
2.2 Forward pass
Unroll in time; at each step compute
3. Training: Backpropagation Through Time (BPTT)
- Treat the unfolded network as one big feed-forward net taking the whole sequence.
- Compute weight gradients at each copy, then sum (or average) and apply to the shared RNN weights.
- Problem: long sequences → huge memory.
Truncated BPTT
- Run forward over chunks of
steps; backprop within each chunk. - Carry hidden states forward in time, but only backpropagate for a smaller number of steps.
3.1 Backward pass (vanilla RNN)
3.2 Vanishing / exploding gradients
Computing the gradient for step
- If the largest singular value of
< 1 → gradients vanish. - If > 1 → gradients explode.
This is why vanilla RNNs struggle with long-range dependencies.
4. LSTM — Long Short-Term Memory (Hochreiter & Schmidhuber 1997)
Adds a memory cell
Gates and equations
= new candidate content, = input gate, = forget gate, = output gate. - Key: the gradient path from
to involves only addition and element-wise multiplication (no matrix multiply / activation), so error can flow many steps without vanishing.
5. GRU — Gated Recurrent Unit (Cho et al. 2014)
Simpler than LSTM: merges forget + output into an update gate, drops the separate cell state.
More efficient than LSTM, often comparable performance.
6. RNN Variants
- Multi-layer RNNs: stack hidden layers; skip connections across layers/time allowed.
- Bi-directional RNNs: process forward and backward (common in speech recognition) → each state sees past and future context.
7. Application: Image Captioning (Show and Tell, Vinyals et al. CVPR 2015)
- Encoder: CNN extracts image features
. - Decoder: RNN/LSTM generates words one at a time, conditioned on
and previous words. - Training: maximize likelihood of reference caption
:
- Test time: avoid always picking the max-likelihood word (greedy can be poor). Use beam search with beam width
: keep the top-scoring candidate sentences by sum of per-word log-likelihoods; expand successors and keep best each step.
Pipeline: one-hot word → word embedding → LSTM → softmax over vocabulary → (next word).
8. Summary
- RNNs share weights over time and keep a hidden state; trained by BPTT (truncated in practice).
- Vanilla RNNs vanish/explode because gradients multiply by
repeatedly. - LSTM (cell + gates) and GRU (update/reset gates) enable long-range memory.
- Use bi-directional and multi-layer RNNs; apply to captioning via encoder–decoder + beam search.
9. Seq2seq with Additive Attention (Recap)
For machine translation (Bahdanau), the decoder hidden state
The context
10. Beam Search & Teacher Forcing
- Beam search: keep the top-
partial sequences by cumulative log-likelihood ; expand and prune each step. Better than greedy ( ). - Length normalization: divide score by
to avoid favoring short sequences. - Teacher forcing: during training, feed the ground-truth previous word (not the model's own prediction) — stabilizes training but can cause exposure bias; mitigated by scheduled sampling.
11. Representations: Character vs. Word
- Word-level: embeddings per word (large vocab, needs unknown-word handling).
- Character-level: smaller vocab, handles misspellings/NEs, longer sequences (harder for RNNs).
- Subword (BPE): the common compromise (used in Transformers).
12. Beyond Captioning
- Video: extend RNN over frames; combine with CNN features per frame.
- Handwriting / speech recognition: bi-directional RNNs over time steps.
- Video captioning / VQA: encode frames + language, decode answer/caption.
13. LSTM/GRU Gradient Flow
The cell-state path in LSTM,
RNNs = weight-shared, time-unrolled networks; LSTMs/GRUs fix the gradient problem; attention + beam search unlock strong sequence generation (and paved the way for Transformers in Ch.16).