All problems
Gradient Clipping
Problem
Clip gradients by global L2 norm. If the norm of the gradient vector exceeds max_norm, scale it down to exactly max_norm; otherwise return it unchanged.
Empty input returns [].
Hint
Compute the vector norm once and scale every component by the same factor.
Test cases
case 1
in: [[3,4],5]
expected: [3,4]
case 2
in: [[3,4],2.5]
expected: [1.5,2]
case 3
in: [[0,0],1]
expected: [0,0]
case 4
in: [[6,8],5]
expected: [3,4]
case 5
in: [[],1]
expected: []
Starter codePython
import math
def clip_gradients(grads, max_norm):
# Your code here
pass5 test cases · run your code against them in the editor