Basic RNN (LSTM)
Train an LSTM model for time series prediction using DeepSpeed with ZeRO-2 optimization.
Overview
This example demonstrates:
- LSTM architecture with proper initialization
- ZeRO-2 memory optimization
- Gradient clipping for RNN stability
- Validation set and early stopping
- Optional W&B experiment tracking
Task: Multi-frequency sine wave prediction
Understanding Sequential Data
Why Sequential Data is Different
Traditional feedforward neural networks assume that all inputs are independent. However, many real-world data types have inherent temporal or sequential dependencies:
| Data Type | Sequential Nature |
|---|---|
| Text | Words depend on previous words for meaning |
| Speech | Phonemes flow continuously in time |
| Stock Prices | Today's price relates to yesterday's |
| Weather | Temperature patterns follow temporal cycles |
| Music | Notes form melodies through time |
| Video | Frames are temporally correlated |
The key insight is that the order matters. The sentence "dog bites man" has a completely different meaning from "man bites dog" even though they contain the same words.
The Time Dimension
Sequential data introduces a time index to our data:
Where:
- is the observation at time step
- is the total sequence length
- Each can be a scalar, vector, or even a matrix
For example, in a sentence:
- = "The"
- = "cat"
- = "sat"
- ...
Why Standard Neural Networks Fail
A feedforward network processes inputs independently:
Problems:
- No memory - Each input processed in isolation
- Fixed input size - Cannot handle variable-length sequences
- No parameter sharing - Learns separate patterns for each position
Markov Chains: The Foundation
What is a Markov Chain?
A Markov chain is a mathematical model for sequences where the probability of the next state depends only on the current state, not on the history of how we got there.
The Markov Property (Memoryless):
This is called the first-order Markov assumption - the future depends only on the present, not the past.
Markov Chain Diagram
Transition Matrix
The dynamics of a Markov chain are captured by a transition matrix :
Where is the probability of transitioning from state to state .
Properties:
- All entries are non-negative:
- Rows sum to 1:
Higher-Order Markov Models
Real sequences often have longer dependencies. An -th order Markov model considers the last states:
Example: In language modeling:
- 1st order: P("sat" | "cat")
- 2nd order: P("sat" | "the", "cat")
- 3rd order: P("sat" | "the", "cat", "happily")
From Markov Chains to RNNs
RNNs extend Markov chains by:
- Learning the transition function (not just storing probabilities)
- Maintaining a continuous hidden state instead of discrete states
- Theoretically capturing infinite-order dependencies through the hidden state
| Aspect | Markov Chain | RNN |
|---|---|---|
| State | Discrete | Continuous vector |
| Transitions | Fixed probabilities | Learned function |
| Memory | Limited to order | Theoretically unlimited |
| Parameters | $O( | S |
Recurrent Neural Network Architecture
The Core Idea
An RNN introduces a hidden state that acts as memory, carrying information from previous time steps:
The hidden state is updated at each time step based on:
- The previous hidden state