#407BPTT — the gradient-norm trajectoryMedium

BPTT — the gradient-norm trajectory

Background

BPTT pushes the gradient backward through the chain of hidden states. The gradient w.r.t. an earlier hidden state is the later gradient times the step's Jacobian:

Lht1=JtLht,Jt=htht1\frac{\partial \mathcal{L}}{\partial \mathbf{h}_{t-1}} = J_t^\top \frac{\partial \mathcal{L}}{\partial \mathbf{h}_t}, \qquad J_t = \frac{\partial \mathbf{h}_t}{\partial \mathbf{h}_{t-1}}

Tracking the norm of this gradient as it flows back shows whether it vanishes or explodes.

Problem statement

Implement bptt_norm_trajectory(g_last, jacobians) returning the list of gradient norms, starting from g_last and propagating back through each Jacobian.

Input

  • g_last — gradient at the final hidden state, length H.
  • jacobians — list of (H, H) matrices [J_T, J_{T-1}, …], ordered from the last step backward.

Output

Returns a list of float norms: [||g_last||, ||J_T^T g_last||, ||J_{T-1}^T J_T^T g_last||, …], length len(jacobians) + 1.

Examples

Example 1 — contracting Jacobians shrink the gradient

Input:  g_last = [1, 0], jacobians = [[[0.5, 0], [0, 0.5]], [[0.5, 0], [0, 0.5]]]
Output: [1.0, 0.5, 0.25]

Explanation: each step halves the norm — vanishing.

Example 2 — expanding Jacobians grow it

Input:  g_last = [1, 0], jacobians = [[[2, 0], [0, 2]]]
Output: [1.0, 2.0]

Constraints

  • Start with g = g_last; record its norm.
  • For each J in order: g = J.T @ g; record ||g||.
  • Return a list of floats of length len(jacobians) + 1.

Notes

  • A norm trajectory trending to 0 is the vanishing-gradient signature; one trending to ∞ is exploding. Stable training keeps it roughly flat.
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 1 — contracting Jacobians shrink the norm
  • Reference — expanding Jacobian grows the norm
  • Sample — identity Jacobians keep the norm flat