#410Vanishing or exploding? Classify the regimeEasy

Vanishing or exploding? Classify the regime

Background

BPTT multiplies a recurrent gradient factor once per timestep, so after TT steps the gradient scales like factorT\text{factor}^T:

  • 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

  • factorfloat 0\ge 0, the per-step gradient magnitude.
  • Tint, the number of timesteps.

Output

Returns one of "vanishing", "exploding", "stable":

  • "vanishing" if factor**T < 1e-3,
  • "exploding" if factor**T > 1e3,
  • "stable" otherwise.

Examples

Example 1 — vanishing

Input:  factor = 0.5, T = 20
Output: "vanishing"

Explanation: 0.5209.5×107<1030.5^{20} \approx 9.5\times10^{-7} < 10^{-3}.

Example 2 — exploding

Input:  factor = 1.5, T = 20
Output: "exploding"

Explanation: 1.5203325>1031.5^{20} \approx 3325 > 10^{3}.

Constraints

  • Compute p = factor ** T; classify with the thresholds above.
  • Return the exact regime string.

Notes

  • This exponential-in-T behaviour 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