AI Notes
Master RNN architecture for sequence modeling. Learn LSTM, GRU, vanishing gradients, sequence-to-sequence, language modeling. BTech interview prep 2024.
Introduction
Recurrent Neural Networks (RNNs) are designed to handle sequential data where the order of elements matters. Unlike feedforward networks that process each input independently, RNNs maintain a hidden state that acts as memory, allowing information from previous time steps to influence the processing of current inputs. This makes them natural candidates for tasks like language modeling, speech recognition, time series prediction, and machine translation.
Why Standard Networks Fail on Sequences
Consider predicting the next word in "The clouds are dark, it will probably ___". A standard feedforward network would need to process all previous words simultaneously as a fixed-size input. But sentences vary in length, and the relationship between words depends on their position. An RNN processes one word at a time while maintaining a running summary of everything it has seen so far.
RNN Architecture
Basic RNN Cell
h_t = tanh(W_hh × h_{t-1} + W_xh × x_t + b_h)
y_t = W_hy × h_t + b_y
Where
x_t = input at time step t
h_t = hidden state at time t (the "memory")
h_{t-1} = hidden state from previous step
y_t = output at time t
W_hh = hidden-to-hidden weight matrix
W_xh = input-to-hidden weight matrix
W_hy = hidden-to-output weight matrix
Unrolled through time
x_1 → [RNN] → h_1 → y_1
↓
x_2 → [RNN] → h_2 → y_2
↓
x_3 → [RNN] → h_3 → y_3
↓
...
The same weight matrices are shared across all time steps. This parameter sharing is what allows RNNs to generalize to sequences of any length.
Worked Example: Character-Level Language Model
Let us trace an RNN processing the word "hello" character by character:
| Vocabulary | {h, e, l, o} encoded as one-hot vectors |
| Hidden size | 3, Learning to predict next character |
| Step 1 | Input 'h' = [1,0,0,0] |
| Predict next: softmax(W_hy × h_1) | probability over {h,e,l,o} |
| Step 2 | Input 'e' = [0,1,0,0] |
| Step 3 | Input 'l' = [0,0,1,0] |
| Should predict 'l' again (since "hell" | next is 'l') |
The Vanishing Gradient Problem in RNNs
When training RNNs with backpropagation through time (BPTT), gradients must flow backward through many time steps. The gradient at step t with respect to parameters at step 1 involves multiplying many Jacobian matrices:
| If largest eigenvalue of W_hh < 1 | gradients shrink exponentially |
| If largest eigenvalue of W_hh > 1 | gradients explode exponentially |
| Gradient factor: 0.9^50 = 0.0052 | vanished! |
| Gradient factor: 1.1^50 = 117.4 | exploded! |
This means standard RNNs struggle to learn long-range dependencies. If understanding word 50 requires remembering word 1, the gradient signal is too weak to establish that connection.
LSTM: Long Short-Term Memory
LSTMs solve the vanishing gradient problem by introducing a cell state with gated access:
| Forget gate | f_t = σ(W_f × [h_{t-1}, x_t] + b_f) |
| Input gate | i_t = σ(W_i × [h_{t-1}, x_t] + b_i) |
| Candidate | C̃_t = tanh(W_c × [h_{t-1}, x_t] + b_c) |
| Cell update | C_t = f_t ⊙ C_{t-1} + i_t ⊙ C̃_t |
| Output gate | o_t = σ(W_o × [h_{t-1}, x_t] + b_o) |
| Hidden state | h_t = o_t ⊙ tanh(C_t) |
| Key insight | Cell state C flows through time with only |
| multiplication | no vanishing! |
Think of the cell state as a conveyor belt. Information flows along it unchanged unless a gate explicitly decides to remove old information (forget gate) or add new information (input gate).
GRU: Gated Recurrent Unit
GRUs simplify LSTMs by combining the forget and input gates into a single update gate:
| Update gate | z_t = σ(W_z × [h_{t-1}, x_t]) |
| Reset gate | r_t = σ(W_r × [h_{t-1}, x_t]) |
| Candidate | h̃_t = tanh(W × [r_t ⊙ h_{t-1}, x_t]) |
| Output | h_t = (1-z_t) ⊙ h_{t-1} + z_t ⊙ h̃_t |
Types of RNN Architectures
Applications and Real-World Examples
Language Modeling: Given "The cat sat on the ___", predict "mat". RNNs model P(next_word | previous_words) enabling text generation, autocomplete, and speech recognition.
Machine Translation: Encoder RNN reads source sentence into a fixed vector, decoder RNN generates translation word by word. Attention mechanism (leading to Transformers) was invented to improve this.
Speech Recognition: Process audio frames sequentially, outputting phonemes or characters. The temporal nature of speech makes RNNs a natural fit.
Time Series Forecasting: Stock prices, weather data, sensor readings. The hidden state captures trends and patterns across time.
RNNs vs Transformers
While Transformers have largely replaced RNNs for NLP tasks (due to parallelization and better long-range modeling), RNNs remain relevant for streaming data (process one element at a time with constant memory), embedded systems (smaller model size), and tasks where sequential processing is naturally required. Understanding RNNs is essential because they introduce fundamental concepts (hidden states, gating, temporal backpropagation) that underpin modern sequence models.
Summary
RNNs introduced the revolutionary idea of neural networks with memory, enabling AI to process sequential data. While vanilla RNNs suffer from vanishing gradients, LSTM and GRU architectures solve this through gating mechanisms that control information flow. Though largely superseded by Transformers for many NLP tasks, the principles of recurrent computation remain foundational to understanding modern sequence processing in AI.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Recurrent Neural Networks (RNN) - Sequence AI Models.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Artificial Intelligence topic.
Search Terms
artificial-intelligence, artificial intelligence, artificial, intelligence, deep, learning, recurrent, neural
Related Artificial Intelligence Topics