Exploding gradients: growth, overshoot and transients
梯度爆炸
Derive shared-weight gradients, execute a destructive finite update and prove transient amplification despite stable eigenvalues.
exploding-gradientsA derivative can grow before any number overflows
Suppose a recurrent computation multiplies its state by the same weight at every step. A modest weight above one can produce a large final state, and changing that shared weight affects every step. The resulting parameter derivative can grow much faster than a single-layer inspection suggests.
Exploding gradients describe harmful growth of gradient magnitudes, often with depth or temporal distance. Infinity and NaN are possible numerical consequences, but a large finite gradient can already make an update destructive. A high loss alone is not enough to diagnose this mechanism.
Pascanu, Mikolov and Bengio,2013, Sections 2.1–2.3 discusses derivative products and sharp changes in the objective. D2L, Section 5.4.1.2 introduces a matrix-product example. Here we use exact arithmetic to separate persistent growth, update overshoot and temporary amplification.
A shared weight contributes at every step
Let h₀=1, hₜ=w hₜ₋₁ and use a zero-target half squared loss at step T:
The factor T comes from the shared weight appearing T times. It would be wrong to differentiate only the last multiplication. For |w| greater than 1, the gradient magnitude grows exponentially with T, apart from the polynomial factor T. At w=1 the gradient equals T, which grows only linearly. The kind of growth and the variable tending to infinity must be stated.
One can compute the derivative without symbolic powers. Differentiate the recurrence while carrying the old state:
Our code uses this recurrence and tests it against the closed form. With w=3/2 and T=8, the loss is about 328.42 and the weight gradient about 3503.15. A learning rate of 0.01 changes w from 1.5 to about−33.53, producing a vastly larger finite loss. The derivative points downhill locally, but that does not justify an arbitrarily large finite step.
Capping the scalar gradient magnitude at 1 before this same 0.01 step instead moves w to 1.49 and lowers this particular loss. This illustrates one intervention, not a guarantee that every clipped step improves every objective. The gradient clipping chapter derives and compares clipping rules.
Stable eigenvalues do not rule out large finite amplification
Consider a different linear map:
Both eigenvalues are 1/2, so Aᵗ tends to zero as t grows. Yet with K=16, the second component of the transported gradient is 16 at t=1 and t=2. At t=8 it is 1, and by t=32 it is 1/4194304. An eigenvalue statement about eventual decay does not bound all finite-time amplification by one.
The power formula follows by writing A=rI+N with N²=0 and expanding the binomial: only the zero- and first-order N terms remain. For K nonzero, A is not normal, meaning A Aᵀ differs from Aᵀ A. Its eigenvectors do not form the orthonormal basis that would make eigenvalue magnitudes directly control the 2-norm at each step. This is transient amplification followed by decay, distinct from the persistent exponential growth of the scalar recurrence above.
Compute the derivative and the resulting update
The complete Python 3.12 program evaluates both recurrences with exact fractions. Decimal formatting is only for display; the internal losses and gradients remain rational.
from fractions import Fraction as F
def recurrent_objective(weight, depth):
if type(weight) not in (int,F) or abs(weight)>64:
raise ValueError('rational weight bounded by 64 required')
if type(depth) is not int or not 1<=depth<=32:
raise ValueError('depth from 1 to 32 required')
w=F(weight);state=F(1);sensitivity=F(0)
for _ in range(depth):
state,sensitivity=w*state,state+w*sensitivity
return state,state*state/2,state*sensitivity
def transient_gradient(depth, coupling=16):
if type(depth) is not int or not 0<=depth<=32 or type(coupling) not in (int,F) or not 0<=coupling<=32:
raise ValueError('bounded depth and rational coupling required')
p,q=F(1),F(0)
for _ in range(depth):p,q=p/2,F(coupling)*p+q/2
return p,q
if __name__=='__main__':
for depth in (2,4,8):
state,loss,gradient=recurrent_objective(F(3,2),depth)
print('depth:',depth,'state/loss/weight gradient:',*[f'{float(v):.9f}' for v in (state,loss,gradient)])
w=F(3,2);_,loss,g=recurrent_objective(w,8)
for name,gradient in [('raw',g),('capped at1',min(F(1),max(F(-1),g)))]:
updated=w-F(1,100)*gradient
new_loss=recurrent_objective(updated,8)[1]
print(name,'updated weight:',f'{float(updated):.9f}','new loss:',f'{float(new_loss):.9g}')
for depth in (1,2,8,32):
print('stable-eigenvalue matrix depth:',depth,'transported gradient:',[str(v) for v in transient_gradient(depth)])
Actual output:
depth: 2 state/loss/weight gradient: 2.250000000 2.531250000 6.750000000
depth: 4 state/loss/weight gradient: 5.062500000 12.814453125 68.343750000
depth: 8 state/loss/weight gradient: 25.628906250 328.420417786 3503.151123047
raw updated weight: -33.531511230 new loss: 1.27708569e+24
capped at1 updated weight: 1.490000000 new loss: 295.087151
stable-eigenvalue matrix depth: 1 transported gradient: ['1/2', '16']
stable-eigenvalue matrix depth: 2 transported gradient: ['1/4', '16']
stable-eigenvalue matrix depth: 8 transported gradient: ['1/256', '1']
stable-eigenvalue matrix depth: 32 transported gradient: ['1/4294967296', '1/4194304']
The raw update’s enormous loss is still a finite rational number. No floating-point overflow caused that failure. The matrix example uses the actual reverse-transport direction, not only a spectral-radius calculation. Tests check the derivative against its analytic form for positive, negative and zero weights, use finite differences, and verify the transient power formula.
Each scalar or 2×2 recurrence costs O(T) arithmetic and constant evolving state. Exact numerator and denominator sizes grow, so bit cost is not constant per operation. The depth cap 32 and parameter bounds keep this teaching program small; exact fractions are not a practical deep-network training format.
A large gradient has more than one cause
Summing a loss instead of averaging it can multiply a gradient by the batch size without changing the model’s derivative paths. Large input units, poor initialization, an incorrect derivative or a corrupted update can also create large values. Check loss scaling and data units before attributing every spike to recurrent dynamics.
The scalar example has a shared parameter and a terminal squared loss. A different output loss, bounded state dynamics, parameter sharing pattern or truncation policy changes its derivative. A norm upper bound greater than one alone does not prove expansion in the direction actually carrying the error; see vanishing gradients.
Clipping, smaller learning rates and architectural changes address different parts of the problem. Once a computation has already produced nonfinite gradients, scaling them cannot reconstruct the original finite direction. Inspect the first nonfinite operation and its inputs, and distinguish mathematical amplification from number-format overflow.
Does the diagnosis match the calculation?
- Why does dL_T/dw contain a factor T?
Answer 1: The same weight participates in every one of the T recurrent multiplications, so all contributions must be differentiated.
- At w=1, is the displayed gradient growth exponential in T?
Answer 2: No. The gradient is T, so it grows linearly in this case.
- Can both eigenvalues be 1/2 while a finite-step gradient component grows from 0 to 16?
Answer 3: Yes. The triangular example has such transient amplification even though its powers eventually vanish.
- Did numerical overflow cause the raw update’s large loss in the program?
Answer 4: No. Fractions compute that finite loss exactly; the chosen step is too large for this objective at this point.
Separate a mechanism from a universal explanation
The recurrent-gradient paper derives temporal products and discusses a sharp-wall hypothesis for some objectives. Its hypothesis is not a proof that every large gradient is aligned with a Hessian eigenvector or that clipping always lowers loss. We use its distinction between derivative growth and update behavior, then supply our own exact examples.
The recurrence, triangular power calculation and tests are original, with no source figures or experimental numbers copied. Continue with gradient clipping for an explicit bounded-gradient operation and minibatches for a separate source of gradient variation.
Make a prediction, then check
Can eigenvalues inside the unit circle coexist with large finite-time gradient amplification?
Learning objectives
- Derive a shared-parameter gradient through all steps.
- Separate persistent growth, transient amplification and numerical overflow.
Prerequisites & connections
Explore related knowledge →References
- On the difficulty of training recurrent neural networks
Razvan Pascanu; Tomas Mikolov; Yoshua Bengio · 2013 - Numerical Stability and Initialization — Dive into Deep Learning 1.0.3
D2L authors · 2023
Version1 · Editorial status: Recorded technical checks completed
View technical check record (AI)
Codex · 2026-09-06 · Codex single-agent authored technical review: cited primary-source scope, explicit assumptions, original calculations and counterexamples, runnable tests and bilingual review; not independent human or second-agent review.
- Single-agent authored technical review: bounded primary-source locations, assumptions, original derivations, exact/literal references and matching bilingual equations/examples/exercises. Not independent human or second-agent review.
- Four focused CPython3.12.4 tests passed per chapter. Shared recurrent gradients, transient powers, stable clipping, exact minibatch distributions and accumulated-update traces are checked against literal references, analytic identities and finite differences. All four examples use the standard library.
- Promoted the full prose, equations, code and exercises. Tests read formal MDX only; removed temporary fallbacks/draft skips and updated review assertion to checked. Algorithm and independent references are unchanged; final record binds both locale hashes.
- Publication navigation points published topics to complete articles and removes corresponding pending wording, retaining directory entries for unwritten targets. Principles, equations, programs and exercises are unchanged; final article SHA values rebound.
Source locations checked:
- On the difficulty of training recurrent neural networks · Sections2.1–2.3 and scalar recurrence discussion physicalpages2–4; sharp-wall hypothesis retained as conditional.
- Numerical Stability and Initialization — Dive into Deep Learning 1.0.3 · Section5.4.1.2 matrix-product amplification, interpreted without a universal inference from one random example.
Related topics