#410Vanishing or exploding? Classify the regimeEasyRNNNeural NetworksCalculus
Vanishing or exploding? Classify the regime
Background
BPTT multiplies a recurrent gradient factor once per timestep, so after steps the gradient scales like :
- factor < 1 → the product collapses toward 0 (vanishing),
- factor > 1 → it blows up (exploding),
- factor ≈ 1 → it stays controlled (stable).
Problem statement
Implement gradient_regime(factor, T) returning which regime the gradient is in after T timesteps, using the product factor ** T.
Input
factor—float, the per-step gradient magnitude.T—int, the number of timesteps.
Output
Returns one of "vanishing", "exploding", "stable":
"vanishing"iffactor**T < 1e-3,"exploding"iffactor**T > 1e3,"stable"otherwise.
Examples
Example 1 — vanishing
Input: factor = 0.5, T = 20
Output: "vanishing"
Explanation: .
Example 2 — exploding
Input: factor = 1.5, T = 20
Output: "exploding"
Explanation: .
Constraints
- Compute
p = factor ** T; classify with the thresholds above. - Return the exact regime string.
Notes
- This exponential-in-
Tbehaviour is why long sequences are hard for vanilla RNNs — and why gradient clipping (for exploding) and gated cells (for vanishing) exist.
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Example — vanishing after many steps
- •Reference — exploding after many steps
- •Sample — deep contraction vanishes