Deep Learning · TEE · formulas
BPTT recurrent-weight gradient
The same recurrent weight is reused at several times. Each use contributes to its gradient, so sum all required time-step contributions before updating it. For vectors, delta is a column and the previous state is transposed into a row: their outer product has the same shape as the weight matrix.
g_{\mathrm{clipped}}=g\min\left(1,\frac{\tau}{\lVert g\rVert_2}\right)\frac{\partial L}{\partial W_{hh}}=\sum_t\delta_t h_{t-1}^T\frac{\partial L}{\partial W_{xh}}=\sum_t\delta_t x_t^TVanishing and exploding gradients
Definition. Vanishing and exploding gradients describe the attenuation or amplification of backward derivatives through repeated Jacobian products. In recurrent networks, these products extend across time and affect long-range credit assignment.
Explanation and working
In BPTT, the gradient repeatedly multiplies recurrent weights and activation derivatives. Products of many small factors can approach zero; early information then receives very little learning signal. Large repeated factors can make the gradient explode. These are about training signals, not an instruction that every hidden-state value must become zero or infinity.
Numerical intuition: 0.5¹⁰ = 0.0009765625, whereas 2¹⁰ = 1,024. Sigmoid's derivative is at most 0.25 and becomes even smaller in saturation. Tanh also saturates for large magnitude inputs.
Useful remedies include LSTM or GRU gates and cell paths, suitable initialization and learning rate, and gradient clipping for explosion. Clipping does not by itself solve vanishing gradients. LSTM makes long-range learning easier; it does not guarantee perfect unlimited memory.
Why the effect grows with sequence length
For a vanilla tanh RNN, each hidden-state transition contributes a Jacobian. The derivative through several transitions contains a product of these Jacobians:
\frac{\partial h_t}{\partial h_{t-1}}=\operatorname{diag}(1-h_t^2)W_{hh}\frac{\partial h_T}{\partial h_t}=\prod_{k=T}^{t+1}\frac{\partial h_k}{\partial h_{k-1}}The product is ordered from the latest transition toward the earliest. Small activation derivatives and recurrent transformations can attenuate the signal; repeated amplification can make it grow. A one-dimensional product illustrates the issue, but a vector RNN depends on the full matrix products, not one weight alone.
Gradient norm clipping: a complete numerical
Norm clipping rescales an oversized gradient while retaining its direction. It limits the update size caused by an exploding gradient.
g_{\mathrm{clipped}}=g\min\left(1,\frac{\tau}{\lVert g\rVert_2}\right)If g is the zero vector, leave it unchanged. The threshold τ is positive. This is global norm clipping for the vector being considered; elementwise clipping is a different operation.
Question. A gradient is g = [6, 8]ᵀ. Clip its Euclidean norm to at most τ = 5, then update w = [1, 1]ᵀ with learning rate η = 0.1.
Given
Gradient g = [6,8]ᵀ; norm threshold τ = 5.
Old weights w = [1,1]ᵀ; η = 0.1.
To find
Find the clipped gradient and updated weights.
Formula — which one and why
Gradient norm clipping. If the vector norm exceeds the allowed threshold, multiply every component by the same scale. This changes magnitude while preserving direction.
g = gradient vector; τ (tau) = maximum norm; ||g||₂ = square root of the sum of squared components; η = learning rate.
\lVert g\rVert_2=\sqrt{\sum_i g_i^2}g_{\mathrm{clip}}=g\min\left(1,\frac\tau{\lVert g\rVert_2}\right)w_{\mathrm{new}}=w-\eta g_{\mathrm{clip}}Yaad rakhne ka cue: Bada gradient? Sab components ko ek hi factor se chhota karo.
Solution — step by step
Step 1: calculate the original norm.
\lVert g\rVert_2=\sqrt{6^2+8^2}=\sqrt{36+64}=10Step 2: find the scale. Since 10 > 5, scale the vector by 5/10 = 0.5.
g_{\mathrm{clipped}}=0.5\begin{bmatrix}6\\8\end{bmatrix}=\begin{bmatrix}3\\4\end{bmatrix}Step 3: verify the clipped norm.
\sqrt{3^2+4^2}=\sqrt{9+16}=5Step 4: update each parameter with the clipped gradient.
w_{1,\mathrm{new}}=1-0.1(3)=0.7w_{2,\mathrm{new}}=1-0.1(4)=0.6Final answer: clipped gradient [3, 4]ᵀ and new weights [0.7, 0.6]ᵀ. Clipping does not increase a vanishing gradient and does not replace the need for an appropriate recurrent architecture.
Gradient norm clipping: [3, 4]
Question. Clip gradient g = [3, 4] to maximum L2 norm 10. Use global norm clipping.
Given
g = [3, 4]; threshold τ = 10.
To find
The norm, scaling factor and clipped gradient.
Formula — which one and why
Global norm clipping scales the entire list by one common factor only if its norm exceeds the threshold.
τ is the maximum permitted norm, not a per-entry bound.
\|g\|_2=\sqrt{\sum_i g_i^2}g_{\mathrm{clip}}=g\min(1,\tau/\|g\|_2)Yaad rakhne ka cue: Norm bada ho tab sab entries ko same ratio se chhota karo.
Solution — step by step
Step 1: calculate the squared norm.
3^2+4^2=25Step 2: take the square root.
\|g\|_2=\sqrt{25}=5Step 3: choose the shared scale.
c=\min(1,10/5)=1Step 4: scale every entry.
g_1^{\mathrm{clip}}=3(1)=3g_2^{\mathrm{clip}}=4(1)=4Final answer: Clipped gradient = [3, 4].
Gradient norm clipping: [0, 12]
Question. Clip gradient g = [0, 12] to maximum L2 norm 3. Use global norm clipping.
Given
g = [0, 12]; threshold τ = 3.
To find
The norm, scaling factor and clipped gradient.
Formula — which one and why
Global norm clipping scales the entire list by one common factor only if its norm exceeds the threshold.
τ is the maximum permitted norm, not a per-entry bound.
\|g\|_2=\sqrt{\sum_i g_i^2}g_{\mathrm{clip}}=g\min(1,\tau/\|g\|_2)Yaad rakhne ka cue: Norm bada ho tab sab entries ko same ratio se chhota karo.
Solution — step by step
Step 1: calculate the squared norm.
0^2+12^2=144Step 2: take the square root.
\|g\|_2=\sqrt{144}=12Step 3: choose the shared scale.
c=\min(1,3/12)=0.25Step 4: scale every entry.
g_1^{\mathrm{clip}}=0(0.25)=0g_2^{\mathrm{clip}}=12(0.25)=3Final answer: Clipped gradient = [0.0, 3.0].
Drill 45 BPTT shared weights
Question
Question. Linear recurrence hₜ = uxₜ+vhₜ₋₁+b, output h₂, target 1, half squared loss. u = 0.2, v = 0.5, b = 0, h₀ = 0, x₁ = 1, x₂ = 2, η = 0.1. Find both states and one update.
Answer — step by step
Given
Linear recurrence hₜ = uxₜ+vhₜ₋₁+b.
u = 0.2; v = 0.5; b = 0; h₀ = 0.
x₁ = 1; x₂ = 2; target for h₂ = 1.
Half squared loss; learning rate η = 0.1.
To find
Find both states, all three shared-parameter gradients and one simultaneous update.
Formula — which one and why
Backpropagation through time. Unroll the recurrence, send the loss derivative backward through its links and add contributions for each shared parameter before updating it once.
For h_t=ux_t+vh_(t−1)+b with loss only at the last state: δ_t = loss derivative at that state. The linear recurrence has activation slope 1.
\delta_T=h_T-y\delta_t=v\delta_{t+1}g_u=\sum_t\delta_tx_tg_v=\sum_t\delta_th_{t-1}g_b=\sum_t\delta_tYaad rakhne ka cue: Time ke har use ka gradient jodo; shared weight ko ek baar update karo.
Gradient descent. Evaluate the gradient at the current parameter, multiply by the learning rate and subtract. If the objective is supplied, differentiate it and recompute the gradient after each update.
θ (theta) = trainable parameter; g = derivative of the stated objective J; η = learning rate; t labels the update number.
g_t=J\prime(\theta_{t-1})\theta_t=\theta_{t-1}-\eta g_tYaad rakhne ka cue: Naya = purana − learning rate × current gradient.
Solution — step by step
Meaning and formulas
The same u, v and b appear at both times. u multiplies the input, v multiplies the previous state, and b is a shared bias. The loss is only at the final state h₂. Because the recurrence is linear, its local activation derivative is 1.
h_t=ux_t+vh_{t-1}+b,\qquad L=\frac12(h_2-y)^2\delta_2=h_2-y,\qquad \delta_1=v\delta_2Each shared parameter receives a contribution from both of its uses.
\frac{\partial L}{\partial u}=\delta_1x_1+\delta_2x_2\frac{\partial L}{\partial v}=\delta_1h_0+\delta_2h_1,\qquad \frac{\partial L}{\partial b}=\delta_1+\delta_2Step 1: forward through time 1
h_1=0.2(1)+0.5(0)+0=0.2Step 2: forward through time 2
h_2=0.2(2)+0.5(0.2)+0=0.4+0.1=0.5L=\frac12(0.5-1)^2=0.125Step 3: carry the gradient backwards
\delta_2=0.5-1=-0.5\delta_1=0.5(-0.5)=-0.25Step 4: add contributions for each shared parameter
\frac{\partial L}{\partial u}=(-0.25)(1)+(-0.5)(2)=-0.25-1=-1.25\frac{\partial L}{\partial v}=(-0.25)(0)+(-0.5)(0.2)=0-0.1=-0.1\frac{\partial L}{\partial b}=-0.25-0.5=-0.75Step 5: update once after summing
u_{\mathrm{new}}=0.2-0.1(-1.25)=0.325v_{\mathrm{new}}=0.5-0.1(-0.1)=0.51b_{\mathrm{new}}=0-0.1(-0.75)=0.075Final answer: states 0.2 and 0.5; new parameters u = 0.325, v = 0.51, b = 0.075.