Recurrent Neural Networks (RNNs) were the dominant architecture for sequence modeling from the 1990s through 2016, but they suffer from fundamental architectural constraints that transformers were explicitly designed to overcome. Understanding why RNNs fail is critical to appreciating why the transformer architecture works.
Let's try to compute h3 directly. We need:
h3=tanh(Whhh2+Wxhx3+bh)
But h2 requires:
h2=tanh(Whhh1+Wxhx2+bh)
And h1 requires:
h1=tanh(Whhh0+Wxhx1+bh)
Conclusion: To compute ht, we MUST sequentially compute h1,h2,…,ht−1 first. There is no way to "skip ahead" because each hidden state is a non-linear function of the previous one.
Imagine you're trying to understand a long story by listening to it one word at a time, but you have a really bad memory. By the time you hear word 100, you've forgotten what word 1 was about. That's how RNNs work - they have to process words in order and they forget stuff from a long time ago.
Now imagine instead you could see the ENTIRE story written down, and you could look at any part of it whenever you want. You could see word 1 and word 100 at the same time! You could even have multiple people reading different parts simultaneously. That's how transformers work - they can "see" all the words at once and pay attention to any word they need, no matter how far apart the words are.
The result? Transformers are:
Way faster - like having 100 people read different parts vs. 1 person reading word-by-word
Better memory - they can remember connections between words even if they're really far apart
Smarter understanding - they can see how ALL the words relate to each other, not just nearby words
What is the sequential processing bottleneck in RNNs? :: RNNs compute hidden states iteratively as ht=f(ht−1,xt), creating a causal chain where ht cannot be computed until ht−1 is available. This makes computation O(T) sequential steps that cannot be parallelized.
Why't RNNs parallelize computation across time?
Because each hidden state ht is a non-linear function of the previous state ht−1, there's no way to compute ht without first computing all h1,h2,…,ht−1 in sequence.
Derive the vanishing gradient formula for RNNs over T timesteps :: Starting from ∂ht−1∂ht=diag(tanh′(⋅))⋅Whh, we have ∂ht−1∂ht≈σmax(Whh)⋅γ where γ<1. Over T steps, gradients decay as (σmax(Whh)⋅γ)T−1. If this product is less than 1, gradients vanish exponentially.
What is the compression bottleneck in encoder-decoder RNNs?
The encoder must compress the entire input sequence x1,…,xT into a single fixed-size hidden state hT∈Rd. This creates an information bottleneck: a d-dimensional vector has limited capacity while the sequence may contain Tlog2V bits of information.
How much faster can transformers be than RNNs on long sequences?
Transformers have O(1) parallel time complexity compared to RNNs' O(T) sequential time. On sequences of length T=1000 with sufficient parallel hardware, transformers can be ~1000× faster in wall-clock time.
Why do LSTMs NOT fully solve the vanishing gradient problem? :: LSTMs mitigate but don't eliminate the problem. The forget gate ft still multiplies the gradient: ∂ct−1∂ct=ft. If ft<1 at most steps, gradients still decay exponentially, just more slowly. LSTMs extend effective range from ~10 to ~100 steps, but problems return for1000+ token sequences.
What is the effective context window formula for RNNs?
Define effective context as distance k where gradient decays to 1%: γk=0.01, so k=log(γ)log(0.01). For vanilla RNN with γ=0.95: k≈90 steps. For LSTM with γ=0.99: k≈459 steps.
How does self-attention solve the long-range dependency problem?
Self-attention creates direct connections between all positions: position i attends to position j with a single matrix operation, regardless of distance ∣i−j∣. The gradient path has length 1 (not length ∣i−j∣), eliminating exponential decay with distance.
Chalो is concept ko simple tarike se samajhte hain. RNN ka sabse bada problem ye hai ki wo sequence ko ek-ek karke, order mein process karta hai - jaise tum ek book ko word-by-word padho aur word 1000 tak pahunchne ke liye pehle 1 se 999 tak saare words padhne padein. Iska matlab hai ki har step ka answer previous step par depend karta hai (yaad rakho: ht=f(ht−1,xt)). Isse do badi problems aati hain - ek to parallelization possible nahi hai, kyunki har step ko previous step ka wait karna padta hai; aur doosra, jab sequence lambi ho jaati hai to shuruaat ki information dheere-dheere "fuzzy" ho jaati hai, matlab long-range dependencies kamzor pad jaati hain.
Ab ye matter kyun karta hai? Aajkal humaare paas powerful GPUs hain jinme hazaaron CUDA cores hote hain jo ek saath kaam kar sakte hain. Lekin RNN mein, kyunki processing sequential hai, ye saare cores idle baithe rehte hain - sirf ek timestep hi ek time par compute ho sakta hai. Socho, 5000 cores mein se sirf 32 use ho rahe hain aur baaki 4968 khaali! Yahi wajah hai ki RNN training bahut slow ho jaati hai. Transformer isi problem ko solve karta hai - self-attention ki madad se saari positions ek saath, simultaneously compute ho jaati hain, jisse position 512 direct position 1 ko "dekh" sakti hai bina beech ke saare steps ka wait kiye. Isse training kai guna tez ho jaati hai.
Doosra important point hai vanishing gradient problem. Jab RNN train hota hai, gradients ko backward flow karna padta hai poore sequence ke through - matlab agar 1000 timesteps hain to gradient ko 1000 multiplications se guzarna padta hai. Har multiplication mein value thodi chhoti hoti jaati hai, aur end tak gradient itna chhota ho jaata hai ki network shuruaat ke words se effectively kuch seekh hi nahi paata. Yahi teen limitations - no parallelization, information loss, aur vanishing gradients - transformer architecture ke banne ki asli motivation hain. Jab tum transformer ka design samajhoge, to har feature isi problem ka solution nazar aayega, isliye ye foundation samajhna zaroori hai.