gpt4 book ai didi

python - python计算梯度错误?

转载 作者:行者123 更新时间:2023-12-01 02:14:27 25 4
gpt4 key购买 nike

我正在尝试遵循此处的类(class) http://cs231n.github.io/optimization-1/ ,在用有限差分数值计算梯度部分中,他们提供了一个代码片段,用于计算给定函数和数组的梯度。我尝试使用我自己的函数和 numpy 数组作为输入来运行它,但出现以下错误:

ValueError                                Traceback (most recent call last)
<ipython-input-18-31c1f1d6169c> in <module>()
2 return a
3
----> 4 eval_numerical_gradient(f,np.array([1,2,3,4,5]))

<ipython-input-12-d6bea4220895> in eval_numerical_gradient(f, x)
28 print(x[ix])
29 # compute the partial derivative
---> 30 grad[ix] = (fxh - fx) / h # the slope
31 it.iternext() # step to next dimension
32

ValueError: setting an array element with a sequence.

我知道错误是因为它无法为 grad[ix] 分配一个序列,我也尝试使用列数组并得到相同的错误。

这是代码:

def eval_numerical_gradient(f, x):
"""
a naive implementation of numerical gradient of f at x
- f should be a function that takes a single argument
- x is the point (numpy array) to evaluate the gradient at
"""

fx = f(x) # evaluate function value at original point
print(x)
print(fx)
grad = np.zeros(x.shape)
h = 0.00001

# iterate over all indexes in x
it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite'])
while not it.finished:
print(it)
# evaluate function at x+h
ix = it.multi_index
print(ix)
old_value = x[ix]
print(old_value)
x[ix] = old_value + h # increment by h
print(x)
fxh = f(x) # evalute f(x + h)
print(fxh)
x[ix] = old_value # restore to previous value (very important!)
print(x[ix])
# compute the partial derivative
grad[ix] = (fxh - fx) / h # the slope
it.iternext() # step to next dimension

return grad

我的问题是:我输入的 numpy 数组(行和列)是否错误?有人可以解释为什么会发生这种情况吗?

示例输入:

def f(a):
return a

eval_numerical_gradient(f,np.array([[1],[2],[3]]))

def f(a):
return a

eval_numerical_gradient(f,np.array([1,2,3]))

最佳答案

我建议对 eval_numerical_gradient(f, x) 进行以下修复:

  • 第 25 行:将 fxh = f(x) 替换为 fxh = f(x[ix])
  • 第 30 行:将 grad[ix] = (fxh - fx)/h 替换为 grad[ix] = (fxh - fx[ix])/h

并使用 float 条目创建输入矩阵,例如,

eval_numerical_gradient(f,np.array([[1],[2],[3]], dtype=np.float))

关于python - python计算梯度错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48460978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com