Skip to content
DeepForgeby svx
All problems
EasyOptimizationop-171

Per-Sample Grad Norm

Problem

Compute the Euclidean norm of a single per-sample gradient.

Return sqrt(sum(g ** 2)); an empty gradient returns 0.0. This value feeds per-sample clipping in differentially private training.

Hint

The norm is the Euclidean length of the gradient vector.

Test cases

case 1
in: [[3,4]]
expected: 5
case 2
in: [[0,0]]
expected: 0
case 3
in: [[1,-1,1]]
expected: 1.7320508075688772
case 4
in: [[]]
expected: 0
Starter codePython
def per_sample_grad_norm(grad):
    # Your code here
    pass

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