接上节:
3、梯度(gradient):
def numerical_gradient(f,x):
h=1e-5
grad=np.zeros_like(x)
for index_x in range(x.size):
tmp=x[index_x]
x[index_x]=tmp+h
fxh1=f(x)
x[index_x]=tmp-h
fxh2=f(x)
grad[idx]=(fxh1-fxh2)/(2*h)
x[index_x]=tmp
return grad
接上节:
3、梯度(gradient):
def numerical_gradient(f,x):
h=1e-5
grad=np.zeros_like(x)
for index_x in range(x.size):
tmp=x[index_x]
x[index_x]=tmp+h
fxh1=f(x)
x[index_x]=tmp-h
fxh2=f(x)
grad[idx]=(fxh1-fxh2)/(2*h)
x[index_x]=tmp
return grad
转载于:https://www.cnblogs.com/Dai-py/p/10636782.html