Why do RNNs suffer from vanishing gradients across long sequences whereas Transformers do not?

Last updated 22 November 2026

  • In an RNN, the gradient across t−k steps is a product of Jacobians, so it decays (or explodes) exponentially with distance. LSTMs/GRUs only mitigate this.

  • In a Transformer, attention links any two positions directly, so gradient path length depends on layer count, not sequence length.

  • Residual connections (x + f(x)) carry gradients through depth undiminished. Caveats remain: Post-LN needs warmup, and saturated softmax shrinks gradients (hence √d_k scaling).

  • https://www.techmazone.com/pages/interview-rnn-lstm.html
  • https://aicassindra.com/blogs/transformers/the_vanishing_gradient_problem.html
  • https://pub.towardsai.net/attention-vs-memory-why-transformers-killed-the-rnn-58f3f705ede8
All Transformers interview questions