Consider a deep network as a composition of layers. Backprop multiplies Jacobians together:
∂h0∂L=∂hn∂L∏k=1n∂hk−1∂hk
If the typical singular value of each factor ∂hk−1∂hk is >1, this product grows geometrically with depth n (like 2n). This is the exploding gradient problem.
The key insight: near a "cliff" in the loss landscape (common in recurrent models), the gradient's magnitude becomes unreliable, but its direction is still roughly correct. So we keep the direction and just rescale the length.
Q:g=(0,5,12), clip-by-norm with c=6.5. What is g^?
Forecast, then check:
∥g∥=0+25+144=169=13. α=min(1,6.5/13)=0.5. g^=(0,2.5,6), norm =0+6.25+36=42.25=6.5. ✓
Imagine you're walking down a hill blindfolded, taking steps in the steepest downhill direction. Usually your steps are normal-sized. But sometimes the ground is super steep and you take a GIANT leap — so big you fly right past the bottom and land higher up on the other side! Gradient clipping is like putting a rule: "no matter how steep it looks, never take a step longer than 2 meters." You still walk in the correct downhill direction — you just refuse to make crazy-big jumps. That keeps you from launching off the mountain.
Dekho, deep networks — khaas kar ke RNN aur Transformers — train karte waqt ek dikkat aati hai jise exploding gradients kehte hain. Backprop mein har layer ke gradients ek dusre se multiply hote jaate hain, aur agar values 1 se badi hui to depth ke saath ye geometrically badh jaati hain (jaise 2n). Result: gradient ki norm kabhi kabhi 106 tak pahunch jaati hai, aur jab tum w←w−ηg karte ho to step itna bada ho jata hai ki tum valley ko cross kar ke aur upar chale jaate ho. Loss NaN ho jaata hai. Bas yahi rokne ke liye gradient clipping aata hai.
Idea bahut simple hai: gradient ki direction sahi hoti hai, sirf uski length (magnitude) pagal ho jaati hai. To hum ek threshold c set karte hain. Agar ∥g∥≤c hai, kuch mat karo. Agar ∥g∥>c hai, to poore vector ko ek hi scalar c/∥g∥ se multiply kar do — isse direction same rehti hai par norm exactly c ho jaati hai. Ek line ka formula: g^=g⋅min(1,c/∥g∥). Yeh hai clip-by-norm, aur yahi standard hai.
Ek doosra tareeka hai clip-by-value, jisme har component ko alag se [−v,v] mein clamp karte ho. Lekin iska ek chhupa hua nuksaan hai — yeh gradient ki direction badal deta hai. Jaise (100,1) ko v=10 pe clip karo to (10,1) mil jata hai, ratio 100:1 se 10:1 ho gaya. Isliye jab direction preserve karni ho, clip-by-norm use karo.
Yaad rakhna do baatein: (1) clipping sirf exploding gradients theek karti hai, vanishing nahi — vanishing ke liye LSTM gates ya residual connections chahiye. (2) Threshold c ko bahut chhota mat rakho, warna har step clip hoga aur learning slow pad jaayegi. Typical value 1 se 5 ke beech, aur backprop ke baad, optimizer step se pehle apply karo. Bas — "same arrow, shorter arrow" yaad rakho!