I want to make a function that –
returns a vector of the gradient of a function at a specific point
what I tried –
import numpy as np
def function_2(x):
return x[0]**2 + x[1]**2
def numerical_gradient(f, x):
h = 1e-4
grad = np.zeros_like(x)
for idx in range(x.size):
tmp_val = x[idx]
# f(x + h)
x[idx] = tmp_val + h
fxh1 = f(x)
# f(x - h)
x[idx] = tmp_val - h
fxh2 = f(x)
grad[idx] = (fxh1 - fxh2) / (2 * h)
# x[idx] original value
x[idx] = tmp_val
return grad
grd1 = numerical_gradient(function_2, np.array([3,4]))
print(grd1)
and it showed [25000 35000]
Why it is not [6.000xx 7.999x] as expected??your text
New contributor
열두시 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.