Neural NetworksUnit 810 min read
Dynamic Driven Recurrent Networks: RNNs, LSTMs, GRUs
Unit 8 of Neural Networks explores Dynamic Driven Recurrent Networks (RNNs), covering their architecture, variants (LSTM, GRU), training challenges, and real-world applications in sequential data processing like time-series forecasting, natural language, and speech recognition.
TAKEAWAYS:
- Recurrent Neural Networks (RNNs) process sequential data by maintaining hidden states, but suffer from vanishing gradients.
- LSTMs and GRUs solve this with gating mechanisms (forget, input, output gates) to selectively retain or discard information.
- Backpropagation Through Time (BPTT) trains RNNs by unfolding them into layered networks over time steps.
- Applications include Nepali speech-to-text (eSewa’s IVR), stock price prediction (NEPSE), and chatbots (Pathao’s customer service).
- Bidirectional RNNs process sequences forward and backward for tasks like sentiment analysis.
- Attention mechanisms (e.g., in Google Translate) improve performance by focusing on relevant parts of input sequences.
1. Sequential Data and the Need for Recurrent Networks
Sequential data—where order matters—includes:
- Time-series: Stock prices (NEPSE), weather data, or NTC’s electricity demand.
- Text: Sentences (e.g., "कति दिनमा डिलिभर हुन्छ?" in Daraz orders).
- Speech: Audio waveforms (e.g., Khalti’s voice authentication).
Traditional feedforward networks fail because they cannot remember past inputs. Recurrent Networks solve this by looping information through a hidden state.
graph LR
A["Input (t-1)"] --> B["Hidden State (hₜ₋₁)"]
B --> C["RNN Cell"]
C --> D["Output (t)"]
C --> E["Hidden State (hₜ)"]
E --> CReal-world example:
- Pathao’s ride-hailing app uses RNNs to predict driver availability based on past demand patterns (e.g., rush hours in Kathmandu).
2. Vanishing Gradient Problem in Basic RNNs
Basic RNNs update their hidden state as: where:
- : Hidden state at time .
- : Weight matrix for recurrent connections.
Problem: Gradients during backpropagation (BPTT) become exponentially small for long sequences, making early layers "forget" long-term dependencies.
Visualization of vanishing gradients:
graph TD
A["Gradient at t=100"] -->|"→0.5"| B["Gradient at t=99"]
B -->|"→0.5"| C["Gradient at t=98"]
C -->|"→0.5"| D["..."]
D -->|"→0.5"| E["Gradient at t=1"]Example: Predicting tomorrow’s temperature in Kathmandu based on data from 30 days ago. A basic RNN would fail because gradients "vanish" over time.
3. Long Short-Term Memory (LSTM) Networks
LSTMs introduce gates to regulate information flow:
- Forget Gate: Decides what to discard from the cell state.
- Input Gate: Updates the cell state with new information.
- Output Gate: Controls what the hidden state outputs.
LSTM Cell Equation:
Visualization of an LSTM cell:
graph LR
A["Input (xₜ)"] --> B["Forget Gate (σ)"]
C["Previous Hidden (hₜ₋₁)"] --> B
B --> D["Cell State (Cₜ₋₁)"]
A --> E["Input Gate (σ)"]
C --> E
E --> F["Candidate (tanh)"]
D --> G["New Cell State (Cₜ)"]
F --> G
G --> H["Output Gate (σ)"]
H --> I["Hidden State (hₜ)"]Worked Example: Predicting NEPSE stock prices for the next day using 7 days of historical data.
- Input: Daily closing prices .
- LSTM Layers: 2 layers with 64 units each.
- Output: Predicted .
- Loss: Mean Squared Error (MSE).
- Result: The LSTM retains long-term trends (e.g., seasonal fluctuations) better than a basic RNN.
4. Gated Recurrent Units (GRUs)
GRUs simplify LSTMs by merging the cell state and hidden state into a single update gate and reset gate:
Comparison Table: LSTM vs. GRU
| Feature | LSTM | GRU |
|---|---|---|
| Gates | 3 (Forget, Input, Output) | 2 (Update, Reset) |
| Parameters | More (better for long sequences) | Fewer (faster training) |
| Use Case | Complex patterns (e.g., speech) | Simpler sequences (e.g., text) |
Real-world example:
- WhatsApp’s voice messages use GRUs for real-time transcription because they are computationally efficient yet effective for short-term dependencies.
5. Backpropagation Through Time (BPTT)
BPTT unfolds the RNN into a deep feedforward network over time steps, then applies backpropagation.
Steps:
- Forward Pass: Compute outputs and hidden states for all time steps.
- Backward Pass: Propagate gradients backward through time.
- Update Weights: Adjust weights using gradient descent.
Visualization of BPTT:
graph LR
A["Input (t=1)"] --> B["RNN Cell (t=1)"]
B --> C["Hidden (h₁)"]
C --> D["Output (t=1)"]
C --> E["RNN Cell (t=2)"]
E --> F["Hidden (h₂)"]
F --> G["Output (t=2)"]
F --> H["RNN Cell (t=3)"]
H --> I["Hidden (h₃)"]
I --> J["Output (t=3)"]
J -->|"Loss"| K["Backpropagate"]
K -->|"Gradients"| H
K -->|"Gradients"| E
K -->|"Gradients"| BExample: Training an RNN to predict traffic congestion in Kathmandu using 1-hour intervals of GPS data from Pathao drivers.
- Input: Speed data for the last 5 hours.
- Output: Congestion level (0–100) for the next hour.
- Challenge: Gradients must propagate back 5 steps without vanishing.
6. Bidirectional RNNs
Bidirectional RNNs process sequences forward and backward, doubling the hidden state:
Use Case: Sentiment analysis of Nepali reviews on Daraz.
- Forward Pass: Processes text left-to-right.
- Backward Pass: Processes text right-to-left.
- Output: Combined hidden state captures context from both directions.
Visualization:
graph LR
A["Input (x₁)"] --> B["Forward RNN"]
B --> C["Hidden (h₁)"]
C --> D["Input (x₂)"]
D --> E["Forward RNN"]
E --> F["Hidden (h₂)"]
F --> G["Input (x₃)"]
G --> H["Forward RNN"]
H --> I["Hidden (h₃)"]
I --> J["Backward RNN"]
J --> K["Hidden (h₃)"]
K --> L["Backward RNN"]
L --> M["Hidden (h₂)"]
M --> N["Backward RNN"]
N --> O["Hidden (h₁)"]7. Attention Mechanisms
Attention allows the model to focus on relevant parts of the input sequence (e.g., specific words in a sentence).
Example: Google Translate uses attention to align Nepali words with English words during translation.
- Query: English word to translate.
- Key/Value Pairs: All words in the Nepali sentence.
- Attention Scores: Weights indicating importance.
Equation:
Real-world tie-in:
- eSewa’s chatbot uses attention to prioritize recent transactions when answering user queries like "कति पटक मेरो बिजुली बिल भएको छ?"
8. Applications in Nepal
| Company/App | Use Case | Network Type |
|---|---|---|
| eSewa | Voice-based bill payments | LSTM (speech recognition) |
| NEPSE | Stock price forecasting | Bidirectional LSTM |
| Pathao | Driver demand prediction | GRU (time-series) |
| Khalti | Fraud detection in transactions | LSTM (sequential patterns) |
| NTC | Electricity load forecasting | RNN + Attention |
9. Challenges and Solutions
| Challenge | Solution |
|---|---|
| Vanishing gradients | LSTM/GRU gating mechanisms |
| Long training times | Truncated BPTT (limit unrolling steps) |
| Overfitting | Dropout, regularization |
| Hardware limitations | Use GPUs/TPUs (e.g., Google Colab) |
## In the real world
eSewa’s IVR System:
- Uses LSTMs to convert Nepali voice commands (e.g., "बिजुली बिल तिर्नुहोस्") into text.
- Why LSTM? Handles variable speech speeds and accents by retaining phonetic context across syllables.
Pathao’s Ride Demand Prediction:
- GRUs analyze historical demand patterns (e.g., spikes during Dashain) to optimize driver dispatch.
- Real-time example: During a sudden rain in Kathmandu, GRUs detect increased demand in sheltered areas.
NEPSE Stock Forecasting:
- Bidirectional LSTMs process both past prices and future trends (e.g., upcoming holidays) to predict closing values.
- Worked trace: Given 2017–2019 data, the model predicts a 5% drop in 2020 (aligned with COVID-19 market crash).
## Exam Tip
- Diagrams are key: Draw LSTM/GRU cells and BPTT unrolling in exams. Label gates and hidden states.
- Math shortcuts:
- Memorize the 3 LSTM gates and 2 GRU gates equations.
- For BPTT, explain it as "unfolding time into layers."
- Application focus:
- Link RNNs to sequential data (time-series, text, speech).
- Mention Nepali examples (eSewa, NEPSE, Pathao) to show practical relevance.
- Common pitfalls:
- Don’t confuse vanishing gradients with exploding gradients (use gradient clipping).
- Bidirectional RNNs cannot be used for online processing (need full sequence).
- Expected questions:
- Derive the LSTM update equations.
- Compare LSTM vs. GRU in a table.
- Explain how attention improves translation accuracy.
Labeled LSTM cell with forget, input, and output gates (Image: fdeloche, CC BY-SA 4.0, via Wikimedia Commons)
GRU cell with update and reset gates (Image: Chankahou0968, CC BY-SA 4.0, via Wikimedia Commons)
Based on the TU BSc CSIT syllabus for Neural Networks, unit 8.
Discussion
Loading…