All problems
Finite-Difference Gradient Check
Problem
Gradient checking compares an analytic gradient against central finite differences with step h = 1e-6.
For f(x) = sum(coeffs[j] * x_j^2), verify that the supplied gradient matches numerically: return True only if every component differs by at most tol (default 1e-6).
Hint
Perturb one coordinate at a time and compare the symmetric difference quotient.
Test cases
case 1
in: [[1,2],[3,4],[6,16]]
expected: true
case 2
in: [[1,2],[3,4],[6,15]]
expected: false
case 3
in: [[0,0.5],[2,-1],[0,-1]]
expected: true
case 4
in: [[1],[0],[0]]
expected: true
Starter codePython
def finite_difference_check(coeffs, x, grad, tol=1e-6):
# Your code here
pass4 test cases · run your code against them in the editor