Recurrent Neural Networks (RNs) are neural network architectures designed to process sequential data by maintaining an internal hidden state that acts as memory. Unlike feedforward networks that treat each input independently, RNNs have connections that loop back on themselves, allowing information to persist across time steps.
Forward pass: O(T⋅dh2) (matrix multiplications at each step)
Backward pass (BPTT): O(T⋅dh2) (same, but gradients flow backward)
Space: O(T⋅dh) (store hidden states for backprop)
Comparison to feedforward:
Feedforward: O(1) for one input, no sequential dependency
RNN: O(T) sequential operations, cannot parallelize across time
This sequential nature is RNN's key limitation compared to Transformers.
Recall Explain to a 12-year-old
Imagine you're reading a detective story. Each page gives you a clue. A normal robot brain (feedforward network) would look at each page separately and forget what it read before—useless for solving the mystery!
An RNN is like a detective's notebook. When you read page1, you write down important stuff in your notebook. On page 2, you read the NEW clue AND check your notebook to remember what you learned before. You update your notebook with this combined information. Page 3? Same thing—read new clue, check notebook, update.
The notebook is the "hidden state" (h). The detective's brain uses the same thinking rules (weights W) for every page, but the notebook contents change as you learn more. By the last page, your notebook has clues from the ENTIRE book, helping you say "The butler did it!"
Problem: If the book is 1000 pages long, the notebook might forget stuff from page 1 by page 1000—that's the vanishing gradient problem. Better detectives (LSTM, GRU) have special notebooks that can remember longer!
What is the purpose of the hidden state in RNNs? :: The hidden state h(t) acts as memory, encoding information about all previous time steps, allowing the network to maintain context across the sequence.
Why do RNNs share weights across time steps?
Weight sharing allows RNNs to handle variable-length sequences with a fixed number of parameters, and provides an inductive bias that the sequence rules don't change over time (analogous to spatial weight sharing in CNNs).
Write the RNN hidden state update equation
h(t)=tanh(Whhh(t−1)+Wxhx(t)+bh) where Whh are recurrent weights, Wxh are input weights, and tanh is the activation function.
What causes vanishing gradients in RNNs?
During backpropagation through time, gradients involve products of Jacobians ∏iWhhT⋅diag(tanh′). If ∣∣Whh∣∣<1, repeated multiplication shrinks gradients exponentially, making it impossible to learn long-term dependencies.
What is the difference between many-to-one and many-to-many RNN architectures?
Many-to-one processes an input sequence and produces a single output (e.g., sentiment classification using final hidden state). Many-to-many processes an input sequence and produces an output sequence (e.g., machine translation), with potentially different input/output lengths.
Why initialize h(0)=0 at the start of each new sequence?
Training examples are independent. Carrying over hidden state from a previous sequence creates spurious correlations, causing the network to learn patterns that don't actually exist across different sequences.
What is truncated BPTT and why use it?
Truncated Backpropagation Through Time limits gradient computation to k time steps instead of the full sequence. This reduces memory usage and computation for long sequences, while still allowing forward propagation through the entire sequence.
How do you produce output from an RNN hidden state?
Apply a linear projection: y(t)=Whyh(t)+by, then apply task-specific activation (softmax for classification, none for regression). This allows hidden dimension to differ from output dimension.
What is the computational complexity of RNN forward pass for sequence length T?
O(T⋅dh2) where dh is hidden dimension. This is due to matrix multiplications at each time step. Unlike feedforward networks, RNN operations are sequential and cannot be parallelized across time.
Why use tanh activation instead of sigmoid RNN hidden state?
tanh outputs in range [−1,1] (zero-centered) instead of sigmoid's [0,1], which helps maintain healthier gradients during backpropagation and prevents bias shift issues in the hidden state.
Dekho, RNN ka core idea bahut simple hai — jab hum ek sentence padhte hain word by word, to "mat" tak pahuchne se pehle humein "cat" aur "sat" yaad rehta hai. Normal feedforward networks mein yeh memory nahi hoti, woh har input ko alag-alag treat karte hain. Isliye RNN ek hidden state (basically ek memory vector) rakhta hai jo time ke saath aage travel karta hai. Har time step par network current input ke saath purani memory ko combine karta hai, ek formula se: naya hidden state = tanh(purani memory + current input). Yahi loop-back connection RNN ko "sequential" data ke liye perfect banata hai.
Ab sabse important baat jo yaad rakhni hai woh hai weight sharing. Matlab jo weight matrices hain (Wxh, Whh, Why), woh har time step par same rehte hain — badalte nahi. Yeh isliye zaroori hai kyunki isse humein sirf ek fixed set of parameters chahiye, chahe sequence kitna bhi lamba ho. Aur jo pattern network position 1 par seekhta hai, woh position 5 par bhi apply ho jaata hai. Yeh CNN ke spatial weight sharing jaisa hi hai, bas yahan hum time ke across share kar rahe hain. Iska matlab hai ek strong assumption — ki "sequence ke rules time ke saath change nahi hote".
Yeh concept kyun matter karta hai? Kyunki real-world mein bahut saara data sequential hota hai — text, speech, stock prices, weather, sab kuch. In sab mein order aur context important hota hai, aur RNN humein woh "memory" deta hai jisse machine bhi context samajh sake. Language models, translation, speech recognition — yeh sab RNN (aur uske advanced versions jaise LSTM, GRU) ki foundation par khade hain. Toh agar aap sequence models ko samajhna chahte ho, to yeh hidden state aur weight sharing ka intuition aapki base hai — ise achhe se pakad lo, aage sab kuch isi par build hoga.