WHY tanh? It squashes to (−1,1), keeping the state bounded so it can't blow up as it recirculates, and it is centered at 0 (unlike sigmoid), which keeps gradients healthier.
WHY share weights? A sequence can be any length. If each step had its own weights you (a) couldn't handle variable length, and (b) couldn't generalise "the same physical rule applies at every instant." Sharing bakes in time-invariance.
We want ∂Whh∂L for a total loss L=∑t=1TLt, where Lt compares yt to the target.
Step 0 — pre-activation. Define at=Whhht−1+Wxhxt+bh, so ht=tanh(at).
Why? Naming the pre-activation lets us apply the chain rule cleanly.
Step 1 — gradient flowing back into a hidden state.ht affects the loss in two ways: directly through yt, and indirectly through the next step ht+1. So define δt≡∂ht∂L:
δt=via output yt∂ht∂Lt+via future∂ht∂ht+1⊤δt+1Why the sum? Multivariate chain rule: a variable that feeds several paths contributes the sum of the path gradients. This is the heart of BPTT — gradient is passed backward in time.
Step 2 — the recurrent Jacobian. Since ht+1=tanh(Whhht+…),
∂ht∂ht+1=diag(1−ht+12)Whh
using tanh′(a)=1−tanh2(a).
Why? Chain rule through tanh then through the linear map Whh.
Step 3 — accumulate over shared weights. Because Whh is reused at every step, its gradient is the sum of contributions from all steps:
∂Whh∂L=t=1∑T(1−ht2)⊙δtht−1⊤Why sum again? Same weight-sharing logic: one parameter, many uses ⇒ add up all its "jobs."
Let everything be scalar: whh=0.5,wxh=1,bh=0, inputs x1=1,x2=1, h0=0.
Forward.
a1=0.5⋅0+1⋅1=1⇒h1=tanh(1)=0.7616. Why? Plug into update rule.
a2=0.5⋅0.7616+1=1.3808⇒h2=tanh(1.3808)=0.8811.
Loss. Suppose L=21(h2−1)2 (target 1 at final step). ∂h2∂L=h2−1=−0.1189.
Backward (BPTT).
δ2=−0.1189.
∂h1∂h2=(1−h22)whh=(1−0.7763)⋅0.5=0.1118. Why?tanh′ times whh.
δ1=0+0.1118⋅(−0.1189)=−0.0133. Why 0 first term? No output loss at step 1.
Gradient w.r.t. whh (sum over steps, ht−1 as the "input"):
∂whh∂L=(1−h12)δ1h0+(1−h22)δ2h1=(0.4200)(−0.0133)(0)+(0.2237)(−0.1189)(0.7616)=−0.02025Why the two terms? Weight whh was used at step 1 (multiplying h0) and step 2 (multiplying h1). Sum both.
Task: detect "airspeed increasing for 3 consecutive samples." A feed-forward net over a fixed window of 3 must learn separate weights for slot-1, slot-2, slot-3. An RNN learns one comparison rule and applies it as it walks the stream — so it also works for a 5-sample window with no retraining.
Why this step matters: it shows weight sharing = a prior that "the physics is the same at every instant."
Recall Feynman: explain it to a 12-year-old
Imagine reading a story one word at a time and keeping a tiny sticky note in your head that says "what's happened so far." Each new word, you update the sticky note. That sticky note is the hidden state. When you get the ending wrong, you go backwards through the whole story figuring out which earlier words nudged you off — and you use the same reading rules the whole time, so you add up all the corrections. That backward-through-the-story blaming is BPTT. Trouble: after a very long story your "blame signal" fades to nothing (vanishing gradient), so the net forgets the beginning.
Dekho, ek normal neural network har input ko alag-alag dekhta hai — usko yaad nahi rehta ki pehle kya aaya tha. Lekin sequence data (jaise flight ka airspeed, attitude time ke saath badalta hai) mein history matter karti hai. Isiliye RNN ek chhota sa hidden stateht rakhta hai — samjho ek "sticky note" jo abhi tak ki poori memory ka summary hai. Har time step par woh sticky note update hota hai: naya input xt aur purana ht−1 milाके naya ht=tanh(Whhht−1+Wxhxt+b). Aur important baat — wahi weights har step par use hote hain (weight sharing), isliye sequence chhoti ho ya lambi, same rule chalega.
Ab training. Jab output galat aata hai, humein weights update karne ke liye gradient chahiye. Yahi hai BPTT (Backpropagation Through Time). Ismein kuch naya nahi hai — ye wahi purana backprop hai, bas RNN ko time ke along "unroll" karke ek lambe deep network ki tarah treat karte hain. Twist sirf itna: kyunki same weight bar-bar use hua, uska gradient sabhi steps ka sum hota hai. Mantra yaad rakho: "Same weight, sum the blame."
Ek dikkat: agar sequence bahut lambi ho, to gradient bar-bar Whh aur tanh′ se multiply hota hai, aur ye product λT ki tarah behave karta hai. Agar λ<1 to gradient vanish ho jaata hai (net shuruaat bhool jaata hai), agar λ>1 to explode ho jaata hai (training phat jaati hai). Iske solution: gradient clipping, aur LSTM/GRU jaise gated cells jo memory ka ek seedha (identity jaisa) rasta banate hain.
Aerospace mein ye kyun useful hai? Kyunki flight data pura temporal hota hai. RNN se aap next state predict kar sakte ho, ya anomaly detect kar sakte ho — sirf abhi ka snapshot nahi, balki poori history dekh kar. Yehi RNN ki asli taakat hai.