Skip to content
DeepForgeby svx
All problems
HardDeep Learningdl-049

Numeric Gradient Check (L2 Error)

Problem

Relative L2 error between an analytic and a numeric gradient: ||a - n||_2 / (||a||_2 + ||n||_2 + 1e-8).

The signature is gradient_check_error(analytic, numeric). Near 0 means the gradients agree.

Hint

Normalize the difference norm by the sum of the two gradient norms plus a small constant.

Test cases

case 1
in: [[1,2],[1,2]]
expected: 0
case 2
in: [[1,0],[0,1]]
expected: 0.7071067777
case 3
in: [[0.5,-0.5],[0.5,-0.5]]
expected: 0
case 4
in: [[3,4],[3.0001,4]]
expected: 0.0000099999
case 5
in: [[1e-8,0],[0,1e-8]]
expected: 0.4714045208
Starter codePython
import math
def gradient_check_error(analytic, numeric):
    # Your code here
    pass

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