Skip to content
DeepForgeby svx
All problems
MediumOptimizationop-304

Gradient Clip by Global Norm Scale

Problem

Global norm clipping rescales a gradient vector by threshold / ||g|| when its norm exceeds the threshold, and leaves it unchanged otherwise.

Given the gradient and the threshold, return the clipped gradient.

Hint

Only the overall scale changes, not the direction.

Test cases

case 1
in: [[3,4],2]
expected: [1.2000000000000002,1.6]
case 2
in: [[0.1,0.1],1]
expected: [0.1,0.1]
case 3
in: [[6,8],10]
expected: [6,8]
Starter codePython
def gradient_clip_by_global_norm_scale(grad, threshold):
    # Your code here
    pass

3 test cases · run your code against them in the editor