Lecture 4: Sequence Modeling - Recurrent and Recursive Nets (RNNs)
Overview of Sequence Modeling and RNNs
Context and Progression:
Previously, the course covered Multi-Layer Perceptrons (MLP), which are standard feedforward neural networks consisting of an input layer, one or more hidden layers, and an output layer.
CNNs (Convolutional Neural Networks) were discussed as tools to learn from grid-structured data, specifically images.
The current focus shifts to sequential data, where the order of arrivals is critical.
Examples of Sequential Data:
Time Series: Sales figures, temperature readings, and sensor streams.
Text: Characters, words, and sentences.
Audio: Phonemes distributed over time.
Video: Frames presented in a specific temporal order.
The Core Question: How can a neural network remember useful information from the past while processing new, incoming inputs?
Understanding Sequential Data
The Importance of Order: In sequential problems, the interpretation of data depends on the sequence.
Fixed Input Setting: Denoted as . The model observes all features simultaneously.
Sequential Setting: Denoted as . The model receives data as a stream over discrete steps.
Practical Implications of Order:
Text: In the phrase "the movie was not good," the final word "good" combined with "not" changes the entire sentiment interpretation.
Finance: Today's stock price is dependent on the recent history of prices.
Video: Identifying an action depends on observing motion across multiple consecutive frames.
Requirements for a Good Sequence Model:
Memory: The ability to store past information.
Shared Logic: Using consistent logic across different time steps.
Flexibility: The ability to handle variable-length inputs.
Limitations of Plain Feedforward Networks
Variable Input Length: Standard ANNs struggle because sentence lengths vary. Forcing a fixed length requires padding or truncation.
Position-Specific Weights: ANNs learn separate rules for "word 1," "word 2," etc. This leads to poor generalization for longer sequences than those seen in training.
The RNN Solution: Recurrent Neural Networks use parameter sharing across time, reusing the same transition rule at every step.
Core Architecture of Recurrent Neural Networks (RNNs)
The Hidden State: An RNN maintains a hidden state () that acts as the model's memory.
Typical Mathematical Form:
The state transition function:
The internal activation:
The hidden state calculation:
The output calculation:
Parameter Definitions:
: Current input at time step .
: A summary of the past up to time step .
: The output generated at time step .
: Shared weight matrices reused for all time steps.
: Bias vectors.
Visualizing and Unfolding RNNs
Circuit View: A visualization where the hidden unit feeds back into itself, delayed by one time step. This creates a loop representing memory, allowing the model to process sequences of any length.
Unfolding Through Time: The recurrent graph can be unfolded into a deep chain. Once unfolded, the RNN resembles a deep network where the depth corresponds directly to the sequence length.
The Power of Parameter Sharing
Mechanism: The same matrices () are applied at every time step.
Benefits:
Fewer Parameters: Significant reduction compared to having unique weights for every position.
Generalization: The model can process sequences of lengths it did not encounter during training.
Position Invariance: The model can detect a pattern (e.g., the year "2009") regardless of where it appears in a sentence (e.g., "I went to Japan in 2009" vs. "In 2009, I went to Japan").
Input–Output Patterns in RNNs
Pattern | Example Task | Description |
|---|---|---|
One-to-Many | Image Captioning | One image input generates a sequence of words (caption). |
Many-to-One | Sentiment Analysis | A sequence of words (sentence) results in a single sentiment label. |
Many-to-Many (Same Length) | Speech Tagging | A sequence of audio frames maps to a sequence of phoneme labels. |
Many-to-Many (Different Length) | Machine Translation | A source sentence in one language maps to a translated sentence in another. |
Numerical Examples of RNN Forward Passes
Example 1: Scalar Time Series
Parameters: , .
Initial Conditions: , .
Step 1:
Step 2:
Step 3:
Observation: The hidden state acts as a smoother version of the raw input because it accumulates history.
Example 2: Text Data (Words)
Input Sequence: "I love AI"
Embeddings: , , .
Weights: , , .
Formula: .
Step 1 ("I"): .
Step 2 ("love"): .
Step 3 ("AI"): .
Final Utility: serves as the semantic summary of the entire sentence.
Example 3: Video Frames
Process: CNN extracts features from frames; RNN processes the sequence of features.
Input: .
Parameters: , .
Step 1:
Step 2:
Step 3:
Interpretation: Evidence of motion accumulates across the frames to recognize an action (e.g., waving).
RNN as a Probabilistic Sequence Model
Modeling Sequences: RNNs model the joint probability .
Chain Rule of Probability: .
Language Modeling: At each step, the model predicts the next word based on all previous words.
Output Mechanism: , and , providing a probability distribution over the possible next tokens.
Training RNNs: Loss and Backpropagation
Total Loss: Generally calculated as the sum of losses over all time steps: .
Specific Loss: For classification tasks (like language modeling), the loss is typically the negative log-likelihood: .
Backpropagation Through Time (BPTT):
The RNN is unfolded into a computational graph.
Standard backpropagation is run on the unfolded graph.
Cost: Forward pass is sequential; memory requirements grow linearly with the sequence length.
Vanishing and Exploding Gradients
The Problem: During training, gradients involve products of multiple Jacobians: .
Vanishing Gradients: Occur if the product factors are mostly less than 1. This prevents the model from learning long-term dependencies.
Exploding Gradients: Occur if factors are mostly greater than 1, leading to numerical instability.
Simple Intuition:
If , then after 5 steps, influence scales by (only 3% remains).
If , then after 5 steps, influence scales by (blows up).
Advanced Training Techniques and Variants
Teacher Forcing: Used when previous outputs are fed as future inputs.
Training: The true "ground truth" token is fed as the previous input at each step to stabilize training.
Testing: True future tokens are unavailable; the model must use its own predicted output.
Issue: This leads to a train/test mismatch where errors can accumulate during testing.
Bidirectional RNNs: Address the limitation that standard RNNs only see past context. Uses two RNNs—one left-to-right and one right-to-left. Their hidden states are combined to provide both past and future context.
Encoder–Decoder (Sequence-to-Sequence): Handles cases where input and output lengths differ (e.g., Machine Translation).
Encoder: Reads the input sequence and compresses it into a fixed-size context vector ().
Decoder: Generates the output sequence starting from the context vector ().
Recursive Nets vs. Recurrent Nets
Recurrent Nets: Process sequences. The hidden state evolves over linear time. Best for chains (text, speech, time series).
Recursive Nets: Process tree structures. They combine child nodes into parent representations. Best for hierarchical structures like parse trees.
Domain Applications Summary
Time Series: Sensor values, price forecasting, anomaly detection.
Text: Next-word prediction, tagging, sentiment analysis.
Speech: Transcription of acoustic frames.
Video: Action recognition and captioning.
Biology: DNA/Protein sequence classification and motif detection.
Limitations of Vanilla RNNs
Difficulty learning long-term dependencies.
Sequential computation makes it hard to parallelize processing.
Unstable training on very long sequences.
The context vector in encoder-decoder structures acts as a memory bottleneck.
Note: These limitations directly motivated the development of LSTM, GRU, and eventually Transformers.