TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

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
Adam OptimizerAdamw OptimizerNesterov MomentumGradient Descent QuadraticRmsprop Optimizer

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: number

You must run your code first.
PrevNext

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
Adam OptimizerAdamw OptimizerNesterov MomentumGradient Descent QuadraticRmsprop Optimizer

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: number

You must run your code first.