Google-inspired optimization team is debugging a custom differentiable component whose analytical gradient may be wrong. You need to compute a numerical finite-difference gradient for one parameter so the team can compare it with the implementation.
Estimate the derivative of the function f with respect to the first coordinate of x by a centred finite difference, 2hf(x+he1)−f(x−he1), and compare it with the claimed analytic derivative analytic[0]. f takes an array and returns a scalar; h defaults to 10−5.
Implement solve(f, x, analytic, h=1e-5).
Returns. Return a tuple (numeric, analytic, abs_error) of Python floats.
Example 1
Input:
solve(lambda v: float(np.sum(v ** 2)), [1.5], [3.0])
Output:
(3.0, 3.0, 1.96532e-11)
Example 2
Input:
solve(lambda v: float(np.sum(v ** 2)), [1.5], [2.0])
Output:
(3.0, 2.0, 1.0)
No results yet
Run your code to check it against the examples, or submit to run every test.