Gradient Clipping: One Scale for All Tensors

OptimizationMedium+20 XP+15

Gradient Clipping: One Scale for All Tensors

One bad batch can produce an enormous gradient, and a single step along it wrecks weights that took hours to train — the loss goes to NaN and the run is over. Clipping caps the step size without changing where it points.

1global_norm = sqrt(sum of every gradient element squared, across all tensors)
2
3if global_norm > max_norm:
4 every element *= max_norm / global_norm

Task: write clip_by_global_norm(gradients, max_norm), where gradients is a list of tensors (each a flat list). Return the same structure, rounded to 4 decimal places.

  • The norm is computed once, over everything at once — not per tensor.
  • If the norm is already within max_norm, return the gradients unchanged.
  • A norm of exactly zero is left alone.

Doing it globally rather than per-tensor is the important part: one shared scale factor preserves the relative sizes between layers, so the update still points in the direction backprop computed. Clipping each tensor separately would quietly rotate it.

Examples

gradients = [[3.0, 4.0]]
max_norm = 1.0
returns [[0.6, 0.8]]
gradients = [[0.1, 0.2], [0.3]]
max_norm = 10.0
returns [[0.1, 0.2], [0.3]]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run