Backpropagation through time
3.5.2· AI-ML › Sequence Models
Problem: Recurrent Networks Mein Gradients
Ek standard RNN ka form hota hai:
Hum ek sequence par loss minimize karna chahte hain:
jahan time step par loss hai (jaise classification ke liye cross-entropy).
Challenge: update karne ke liye, hume chahiye. Lekin har time step par use hoti hai, toh usmein changes sabhi future hidden states ko affect karte hain. Gradient ko time ke through sabhi paths account karne chahiye.
First Principles Se Derivation
Step 1: Computation Graph Ko Unroll Karo
Forward pass ko time steps ke liye explicitly likho:
Har depend karta hai par, jo depend karta hai par, aur aise aage. Isse ek chain of dependencies banti hai.
Step 2: Chain Rule Ko Time Ke Across Apply Karo
find karne ke liye, sabhi time steps ke contributions sum karo:
Yeh step kyun? ke computation mein appear hoti hai, toh total gradient har time par partial effects ka sum hai.
Ab, depend karta hai par, jo depend karta hai par. Lekin depend karta hai par, jinmein sab mein involved hai:
Yahan key yeh hai: ko direct effect (time par) aur indirect effects (pehle ke hidden states ke through) account karne chahiye:
Yeh step kyun? Multivariate chain rule se, gradient ke through (indirect path) flow karta hai aur seedha ko bhi affect karta hai (direct path).
Step 3: Recursive Gradient Flow
define karo (post-activation hidden state ke w.r.t. gradient). Toh:
Yeh step kyun? par gradient ke do sources hain: immediate loss aur future loss jo se back propagate hoti hai.
RNN update rule ke liye, ko pre-activation maano, toh :
Yeh step kyun? ka derivative hai, aur compute hoti hai se. Jacobian diagonal hai (elementwise nonlinearity) times .
Isliye:
Step 4: Weights Ke W.R.T. Gradient ( Factor Mat Bhoolna!)
Weight pre-activation mein enter hoti hai, aur . Toh step par ke w.r.t. local gradient mein derivative zaroor include hona chahiye:
Yeh step kyun? post-activation hai. tak pahunchne ke liye hum se guzarte hain: , aur . term skip karna ek common bug hai.
Pre-activation gradient define karo, accumulated gradient yeh hai:
The BPTT Algorithm:
- Forward pass: aur losses compute karo.
- Backward pass: initialize karo, phir se tak recursively compute karo.
- Gradients accumulate karo:
Unrolled Network Ko Visualize Karo

Diagram dikhata hai:
- Forward pass (left se right): hidden states sequentially compute hoti hain.
- Backward pass (right se left): gradients time ke through backward flow karte hain.
- Shared weights : sabhi time steps ke gradient contributions sum kiye jaate hain.
Worked Example 1: Simple 2-Step RNN
Setup: 1-dimensional RNN, , , , activation, .
Input: . Target outputs: . Loss: with (simplicity ke liye identity output).
Forward pass:
Yeh steps kyun? Hum RNN equations seedha apply karte hain. Har hidden state pichle aur current input par depend karti hai.
Backward pass (post-activation compute karo):
par:
Yeh step kyun? Squared error ka ke w.r.t. derivative.
par:
Yeh step kyun? Pre-activation par ka derivative hai, aur linear part contribute karta hai.
Gradient — factor include karo!
Pre-activation gradients:
Yeh step kyun? Har time step contribute karta hai (pre-activation gradient times incoming hidden state). ka term hai — not . factor bhoolne se answer yahan double ho jaata hai; yahi classic error hai.
Worked Example 2: Long Sequences Mein Gradient Vanishing
Setup: , , sabhi ke liye, .
Forward pass (carefully computed):
Yeh steps kyun? Hidden states ke paas ek fixed point ki taraf saturate hoti hain kyunki bade values ko squash kar deta hai.
Backward pass: Maano . Recurrence factor hai:
Saturated region mein use karke, , toh har factor hai.
par: par:
Yeh step kyun? Har backward step se multiply karta hai. Kyunki ke paas saturate hoti hai, bahut chhota hota hai, jo exponential decay cause karta hai.
9 steps baad: . Vanishing gradient: time 10 ki information time 1 tak practically pahuncht hi nahi.
Yeh kyun matter karta hai: Long-range dependencies seekhe nahi ja sakte. Yahi LSTMs aur GRUs ko motivate karta hai.
Truncated BPTT: Practical Compromise
Long sequences ke liye, sabhi hidden states store karna aur exact gradients compute karna memory-intensive hai. Truncated BPTT sequence ko length ke chunks mein split karta hai:
- steps ke liye forward pass.
- Sirf un steps ke liye backward pass.
- Gradient detach karo aur agle steps ke liye forward continue karo.
Trade-off: Gradients steps se aage propagate nahi ho sakte, jo model ki long-range dependencies seekhne ki ability limit karta hai. Lekin memory se ghatakar ho jaati hai.
Formula:
jahan current chunk ki starting hai.
Recall Ek 12-Saal-Ke Bachche Ko Explain Karo
Imagine karo tum piano par ek song seekh rahe ho. Song mein 100 notes hain, aur tum better hona chahte ho.
Normal learning (feedforward): Tum ek note bajate ho, tumhara teacher correct karta hai, tum agle note par jaate ho. Har note independent hai. Recurrent learning (RNN): Tumhara har note bajana pichle notes par depend karta hai. Agar tum note 50 par galti karo, toh notes 51, 52, ..., 100 affect hoti hain. Seekhne ke liye, tumhe figure out karna hoga ki har note ne final mistake mein kya contribute kiya.
Backpropagation through time: Sabhi 100 notes bajane ke baad, tum song "rewind" karte ho aur poochte ho: "Note 1 ne final sound ko kaise affect kiya?" Tum backward trace karte ho: note 100 depend kiya 99 par, jo depend kiya 98 par, .., note 1 tak. Tum adjust karte ho ki tumne note 1 kaise bajaya, effects ki poori chain ke basis par.
Problem yeh hai: jitna peeche jaao, utna hi dhundha hota hai ki kya hua tha (vanishing gradients). Isliye RNNs lambe songs (sequences) ke saath struggle karte hain. LSTMs better notes lene jaisi hain taaki tum pehle ke parts clearly yaad raho.
Connections
- 3.5.01-Recurrent-Neural-Networks-RNNs: BPTT, RNNs ka training algorithm hai.
- 3.5.03-Vanishing-and-Exploding-Gradients: BPTT deep sequences mein vanishing/exploding gradient problem expose karta hai.
- 3.5.04-Long-Short-Term-Memory-LSTM: LSTMs gradient flow ko gate karke vanishing gradients mitigate karte hain.
- 3.5.05-Gated-Recurrent-Unit-GRU: GRUs LSTMs ko simplify karte hain, long-term gradient flow preserve karte hue.
- 2.2.03-Backpropagation-Algorithm: BPTT, unrolled computational graph par apply kiya gaya backprop hai.
- 3.5.07-Bidirectional-RNNs: BPTT bidirectional RNNs tak extend hota hai, dono directions mein backprop karke.
Summary
Backpropagation through time