TensorTonic
Problems
Gradient Clipping (Global Norm)
OptimizationNeural Networks
Medium

Clip an entire gradient array by its global L2 norm:

∥g∥2=∑igi2\lVert g \rVert_2 = \sqrt{\sum_i g_i^2}∥g∥2​=i∑​gi2​​ gclipped={g,∥g∥2≤mgm∥g∥2,∥g∥2>mg_{\mathrm{clipped}} = \begin{cases} g, & \lVert g \rVert_2 \le m \\ g\dfrac{m}{\lVert g \rVert_2}, & \lVert g \rVert_2 > m \end{cases}gclipped​=⎩⎨⎧​g,g∥g∥2​m​,​∥g∥2​≤m∥g∥2​>m​

Here, ggg is the gradient array and mmm is max_norm. Return a NumPy array with the same shape as ggg.

Loading visualization...

Examples

Input: g = [0.1, 0.2, 0.2], max_norm = 1.0

Output: [0.1, 0.2, 0.2]

Explanation: The global norm is 0.3, so the gradient remains unchanged.

Input: g = [6, 8], max_norm = 5.0

Output: [3.0, 4.0]

Input: g = [[2, 2], [2, 2]], max_norm = 2.0

Output: [[1.0, 1.0], [1.0, 1.0]]

Hint 1

Use np.linalg.norm(g) to compute the global L2 norm.

Hint 2

When clipping is needed, multiply by max_norm / norm.

Requirements

  • Compute one global norm across every array element
  • Scale only when the norm exceeds max_norm
  • Preserve shape and direction
  • Return a NumPy array

Constraints

  • g is a nonempty numeric array of any shape
  • max_norm is positive
  • g contains at most 10610^6106 values
  • Use NumPy only
Try Similar Problems
Implement Gradient Descent for a 1D QuadraticEasyImplement Adam Optimizer StepEasyRMSProp Optimizer (Single Update Step)EasyImplement AdamW (Decoupled Weight Decay)EasyImplement Nesterov Momentum (NAG)EasyTrain a Tiny Micrograd MLPHardApply One Policy-Value Head Training StepHardLogistic Regression Training LoopMediumGradient AccumulationMediumMini Training LoopMediumManual Weight UpdateMediumOptimizer SchedulerEasy
Scratchpad

Accepts: array

Accepts: number

You must run your code first.
Gradient Clipping (Global Norm)

Gradient Clipping (Global Norm)

OptimizationNeural Networks
Medium

Clip an entire gradient array by its global L2 norm:

∥g∥2=∑igi2\lVert g \rVert_2 = \sqrt{\sum_i g_i^2}∥g∥2​=i∑​gi2​​ gclipped={g,∥g∥2≤mgm∥g∥2,∥g∥2>mg_{\mathrm{clipped}} = \begin{cases} g, & \lVert g \rVert_2 \le m \\ g\dfrac{m}{\lVert g \rVert_2}, & \lVert g \rVert_2 > m \end{cases}gclipped​=⎩⎨⎧​g,g∥g∥2​m​,​∥g∥2​≤m∥g∥2​>m​

Here, ggg is the gradient array and mmm is max_norm. Return a NumPy array with the same shape as ggg.

Loading visualization...

Examples

Input: g = [0.1, 0.2, 0.2], max_norm = 1.0

Output: [0.1, 0.2, 0.2]

Explanation: The global norm is 0.3, so the gradient remains unchanged.

Input: g = [6, 8], max_norm = 5.0

Output: [3.0, 4.0]

Input: g = [[2, 2], [2, 2]], max_norm = 2.0

Output: [[1.0, 1.0], [1.0, 1.0]]

Hint 1

Use np.linalg.norm(g) to compute the global L2 norm.

Hint 2

When clipping is needed, multiply by max_norm / norm.

Requirements

  • Compute one global norm across every array element
  • Scale only when the norm exceeds max_norm
  • Preserve shape and direction
  • Return a NumPy array

Constraints

  • g is a nonempty numeric array of any shape
  • max_norm is positive
  • g contains at most 10610^6106 values
  • Use NumPy only
Try Similar Problems
Implement Gradient Descent for a 1D QuadraticEasyImplement Adam Optimizer StepEasyRMSProp Optimizer (Single Update Step)EasyImplement AdamW (Decoupled Weight Decay)EasyImplement Nesterov Momentum (NAG)EasyTrain a Tiny Micrograd MLPHardApply One Policy-Value Head Training StepHardLogistic Regression Training LoopMediumGradient AccumulationMediumMini Training LoopMediumManual Weight UpdateMediumOptimizer SchedulerEasy
Scratchpad

Accepts: array

Accepts: number

You must run your code first.
Problems
Loading...
1 / 1