For a vanilla RNN with loss , how does backpropagation through time give for , and why do gradients over long time spans vanish or explode?
Sequence models and transformers, Recurrent networks
Level L3 Intermediate. Length Long. Nature Mat Mathematical.
What's being asked
Whether you can unroll the recurrence, write the gradient as a product of per-step Jacobians, and bound it using the singular values of .
Key idea
Here is the hidden state at step , the input, and are shared across time. The one-step Jacobian is . Chaining these, latest step on the left, gives , so . If bounds and is the largest singular value of , then , and so .
Steps
- Write the pre-activation , so . Use , applied element-wise.
- One-step Jacobian: , where puts a vector on the diagonal of a matrix.
- Chain over time, with the latest step on the left: . So . The order matters, because matrix products do not commute.
- Bound each factor: , with . Hence , and the gradient norm is at most this times . If , the gradient vanishes geometrically (a sufficient condition). Exploding requires (a necessary condition, not a sufficient one).
- Weight gradient: is shared, so , where means the immediate dependence at step (holding fixed). Long-range terms ( large) are exponentially small, so the RNN learns mainly short-range dependencies.
- Remedies: gradient clipping, when (rescale the gradient to norm ), for exploding gradients; LSTM or GRU gating and orthogonal initialisation of ; and truncated BPTT (backpropagating through only the last few steps) to cut cost.
Common mistakes
- Writing and forgetting the diagonal.
- Forgetting that is shared across time, so its gradient is a sum over all time steps.
- Saying gradient clipping fixes vanishing gradients. It only caps large ones.
- Stating as sufficient for explosion. It is only necessary.
How long your answer should be
About a page; 6–8 minutes.
Last checked on 4 December 2026.
Read this
Previous in Sequence models and transformers: In scaled dot-product attention, why divide by , where and are the query and key matrices and is the key dimension?
Next in Sequence models and transformers: What are the LSTM cell equations, and why does the cell state ease the vanishing-gradient problem of a vanilla RNN? How does a GRU differ, and how many parameters does each have for input size 100 and hidden size 128?