Gated Recurrent Units (GRU)
3.5.5· AI-ML › Sequence Models
GRU Kis Problem Ko Solve Karta Hai?
Standard RNN ka issue: Gradient bahut zyada timesteps par exponentially vanish ho jaata hai → long-range dependencies seekh nahi sakta.
LSTM ka solution: Separate cell state gates ke saath information flow control karne ke liye.
GRU ki insight: Kya hume sach mein ek separate cell state chahiye? Kya hum hidden state aur cell state ko ek mein combine kar sakte hain, aur forget aur input gates ko ek single update gate mein merge kar sakte hain? Isse parameters ~25% kam ho jaate hain aur LSTM ki zyaadatar power bani rehti hai.
GRU Architecture

GRU mein 2 gates hain jo hidden state par operate karte hain:
First Principles Se Derivation
Goal: Hidden state ko aur se update karo, information flow control karte hue.
Step 1: Reset Gate Compute Karo
Yeh step kyun? Hume decide karna hai: kya past un naye candidate ke liye relevant hai jo hum compute karne wale hain? Agar sequence ka context change hota hai (jaise topic shift), toh hum chahte hain taaki "reset" ho sake aur irrelevant history se bias na aaye.
Step 2: Candidate Hidden State Compute Karo
Yeh step kyun?
- purani state ko reset gate se element-wise multiply karta hai. Agar chhota hai, toh hum ignore karte hain.
- ke saath concatenate karo, tanh se pass karo taaki candidate update mila sake mein.
- Yeh "proposed new memory" hai agar hum ko poori tarah replace karte.
Step 3: Update Gate Compute Karo
Yeh step kyun? Hume decide karna hai: purani kitni rakhni hai vs. naye candidate kitna accept karna hai. Yahi interpolation weight hai.
Step 4: Final Hidden State (Linear Interpolation)
Yeh step kyun?
- Agar : → purani memory preserve karo (jaise LSTM ka high forget gate).
- Agar : → naya candidate accept karo (jaise LSTM ka high input gate).
- Yeh ek convex combination hai: ek learned interpolation weight ki tarah kaam karta hai har dimension ke liye.
Key insight: Update gate LSTM ke forget aur input gates ko merge karta hai. LSTM mein, alag (forget) aur (input) hote hain. GRU mein, jab high hota hai (purana rakho), toh naye ke liye low hota hai (naya reject karo), aur vice versa—yeh coupled hain aur ki tarah.
GRU vs LSTM: Formula Comparison
| LSTM | GRU |
|---|---|
| 3 gates: forget , input , output | 2 gates: update , reset |
| Alag cell state aur hidden state | Single hidden state (koi alag cell nahi) |
| (koi output gate nahi, directly exposed hai) |
Parameter count: GRU mein LSTM se ~25% kam parameters hain (2 gates vs 3, koi separate cell state weights nahi).
Example 1: Sentiment Analysis Sentence
Sequence: "The movie started well but the ending was terrible."
Chalte hain ek GRU cell trace karte hain word "but" par (timestep ):
- Input: = "but" ka embedding, = "well" ke baad hidden state (likely positive sentiment accumulated).
Step 1: compute karo: "But" ek contrast signal deta hai. Agar network ne seekha hai ki "but" aksar sentiment reverse karta hai, toh low ho sakta hai (say 0.2), matlab "positive par zyaada rely mat karo—hum direction change karne wale hain."
Yeh step kyun? Reset gate contrast word detect karta hai aur decide karta hai positive history ko partially ignore karna hai.
Step 2: compute karo: ke saath, hum ko 0.2 se multiply karte hain, purane positive sentiment ko heavily damp karte hue. Candidate ab zyaadatar ("but") se driven hai aur neutral ki taraf shift hone lagega ya negativity ke liye prepare karega.
Yeh step kyun? Hum ek fresh candidate compute karte hain jo past positive context se zyaada biased nahi hai.
Step 3: compute karo: "But" ek pivot word hai. Network seekh sakta hai ki low hona chahiye (say 0.3), matlab "purani positive zyaada mat rakho—naye candidate ka zyaada accept karo."
Yeh step kyun? Update gate decide karta hai ki purane sentiment se hat ke naye candidate ki taraf shift karna hai.
Step 4: Final : Nayi hidden state 70% candidate hai (jisme reset gate ki wajah se past ka kam influence hai) aur 30% purani state. Net effect: sentiment representation shift karta hai positive se neutral/negative ki taraf.
Result: Jaise sequence aage badhti hai ("ending was terrible"), reset gate phir low hoga, update gate naye negative information ko favor karega, aur final negative sentiment represent karega.
Example 2: Long-Term Dependency (Name Recall)
Sequence: "Alice went to the park. She played with her dog. Later, she went home. __ was tired."
Hum blank mein "Alice" (ya "She") fill karna chahte hain, jo "Alice" ki memory require karta hai sequence ke start se.
Blank par (timestep ):
- "Alice" mention hone ke baad bahut saare timesteps guzar chuke hain.
- Intermediate words ("park", "dog", "home") kaun tired hai, iske liye less relevant hain.
GRU ka behavior:
Intermediate words ke liye (park, dog, home):
- high hai (say 0.8–0.9): Update gate seekhta hai ki yeh words subject identity change nahi karte, isliye . Identity "Alice" preserved rahti hai kyunki term chhoti hai—hum zyaadatar purani rakhte hain.
Yeh kyun kaam karta hai: High LSTM ke high forget gate + low input gate ki tarah kaam karta hai: "purani memory rakho, naya irrelevant content ignore karo." Gradient aur terms ke through backward flow karta hai, jo gated hain—gradient highway.
Blank par:
- abhi bhi "Alice" encode karta hai kyunki intermediate high the.
- Decoder "Alice" ko se extract kar sakta hai "She" ya "Alice was tired" generate karne ke liye.
Key: GRU ka update gate gradient flow control karta hai. Jab , gradient (chain rule), vanishing gradient se bachta hai.
Common Mistakes
GRU vs LSTM Kab Use Karein?
| GRU Use Karo | LSTM Use Karo |
|---|---|
| Naye projects ke liye default choice (simpler) | Bahut lambi sequences (100+ steps) |
| Chhote datasets (kam params → kam overfit) | Complex hierarchical dependencies |
| Faster training/inference chahiye | Jab compute budget ho aur max expressiveness chahiye |
| Performance empirically LSTM ke close hai | Speech, complex NLP (kuch benchmarks mein slight edge) |
Rule of thumb: GRU se shuru karo. Agar performance ceiling aa jaaye aur resources hon, LSTM try karo. Difference aksar marginal hota hai (1-2% accuracy) practice mein.
Recall Ek 12-Saal-Ke Bachche Ko Samjhao
Socho tum ek notebook mein ek story likh rahe ho, ek baar mein ek sentence. Tumhare paas do sticky notes hain jo decide karne mein help karti hain ki aage kya likhna hai: Sticky Note 1 (Reset Gate): "Kya mujhe ideas ke liye pehle jo likha tha wo dekhna chahiye, ya fresh start karna chahiye?" Agar story topic change karti hai (jaise pirates se princesses), toh shayad tum purane sentences ignore karo. Low reset = "naye idea ke liye purani cheez bhool jao."
Sticky Note 2 (Update Gate): "Kya mujhe apna current page as-is rakhna chahiye, ya kuch erase karke naya sentence likhna chahiye?" Agar naya sentence bahut important hai, toh tum zyaada erase karte ho (low update gate). Agar purani cheez abhi bhi important hai, toh zyaadatar rakho (high update gate).
GRU yeh automatically karta hai har word ke liye, decide karta hai "kitni purani memory rakhni hai" aur "naye ideas ke liye kitna past context use karna hai." Yeh ek smart notebook ki tarah hai jo jaanti hai kab yaad rakhna hai aur kab aage badhna hai—lambi stories ya conversations samajhne ke liye perfect!
Connections
- 3.5.03-Long-Short-Term-Memory-(LSTM) — GRU, LSTM ko gates merge karke simplify karta hai
- 3.5.02-Vanishing-and-Exploding-Gradients — GRU, gating ke zariye vanishing gradient solve karta hai
- 3.5.01-Basic-RNN-Architecture — GRU, basic RNN ko long-term memory ke liye gates ke saath extend karta hai
- 3.5.06-Bidirectional-RNNs — GRU ko bidirectionally use kiya ja sakta hai dono directions se context ke liye
- 3.6.01-Attention-Mechanism — Attention, bahut lambi sequences ke liye GRU ko replace/augment kar sakta hai
#flashcards/ai-ml
GRU mein do gates kaun se hain aur har ek kya control karta hai? :: Reset gate control karta hai ki past hidden state naye candidate ko kitna influence kare. Update gate purani hidden state aur naye candidate ke beech interpolation control karta hai.
GRU final hidden state update equation likho :: , jahan update gate hai, candidate hidden state hai.