Skip to content
DeepForgeby svx
All problems
EasyCalculusca-238

Gradient of the Frobenius Norm

Problem

For a nonzero matrix X, the gradient of the Frobenius norm ||X||_F = sqrt(sum of squared entries) is X / ||X||_F.

Given a matrix as a nested list, return the gradient with the same shape. Return the zero matrix when all entries are zero.

Hint

The gradient points along the matrix itself with length one.

Test cases

case 1
in: [[[3,4],[0,0]]]
expected: [[0.6,0.8],[0,0]]
case 2
in: [[[1,0],[0,1]]]
expected: [[0.7071067811865475,0],[0,0.7071067811865475]]
case 3
in: [[[0,0],[0,0]]]
expected: [[0,0],[0,0]]
case 4
in: [[[2]]]
expected: [[1]]
Starter codePython
def frobenius_norm_gradient(matrix):
    # Your code here
    pass

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