gpt4 book ai didi

python - Numpy sum() 出现 'keepdims' 错误

转载 作者:太空宇宙 更新时间:2023-11-04 08:38:51 25 4
gpt4 key购买 nike

这是一段神经网络代码示例:

def forward_step(X, W, b, W2, b2):
hidden_layer = np.maximum(0, np.dot(X, W) + b)
scores = np.dot(hidden_layer, W2) + b2
exp_scores = np.exp(scores)
probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True)
...

上面代码的最后一行抛出了一个错误:

<ipython-input-49-d97cff51c360> in forward_step(X, W, b, W2, b2)
14 scores = np.dot(hidden_layer, W2) + b2
15 exp_scores = np.exp(scores)
---> 16 probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True)
17 corect_logprobs = -np.log(probs[range(X.shape[0]), y])

/Users/###/anaconda/lib/python3.6/site-packages/numpy/core/fromnumeric.py in sum(a, axis, dtype, out, keepdims)
1810 pass
1811 else:
-> 1812 return sum(axis=axis, dtype=dtype, out=out, **kwargs)
1813 return _methods._sum(a, axis=axis, dtype=dtype,
1814 out=out, **kwargs)

TypeError: sum() got an unexpected keyword argument 'keepdims'

Numpy sum keepdims error有个类似的问题说numpy的版本应该大于1.7。我检查了我的 numpy 版本:

import numpy
numpy.version.version
>> 1.12.1

现在我很困惑这个错误是怎么发生的。

最佳答案

注意在keepdimsdocs for numpy.sum() 中的参数它指出:

keepdims : bool, optional
If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
If the default value is passed, then keepdims will not be passed through to the sum method of sub-classes of ndarray, however any non-default value will be. If the sub-classes sum method does not implement keepdims any exceptions will be raised.

所以它在这里声明,如果您使用的是 numpy.ndarray 的子类, 那么如果相应的 sum 就会出现这个错误子类的函数尚未用它定义。

请注意,在您的错误中,它引用了行 1812numpy/core/fromnumeric.py .在实际的上下文中查看 numpy 1.12.x source :

kwargs = {}
if keepdims is not np._NoValue:
kwargs['keepdims'] = keepdims
if isinstance(a, _gentype):
res = _sum_(a)
if out is not None:
out[...] = res
return out
return res
if type(a) is not mu.ndarray:
try:
sum = a.sum
except AttributeError:
pass
else:
return sum(axis=axis, dtype=dtype, out=out, **kwargs)
return _methods._sum(a, axis=axis, dtype=dtype,
out=out, **kwargs)

这里有两件事需要注意:sum函数 did 解析了你的 keepdims变量,因为它把它拉到 1812 线以上并试图将它放在另一个函数中,所以你知道错误不是你使用变量的方式。另一件重要的事情是 1812 行你错误的是只执行 if type(a) is not mu.ndarray ,即,如果您使用的类不同于 ndarray .这正是文档所引用的内容。如果你有不同的类,那么他们需要实现这个 sum 使用 keepdims 参数 函数,如果不这样做,将引发错误。

其他类,如 np.matrix例如将有一个不同的求和函数,似乎,即使在numpy 1.13.x , sum对于 np.matrix类型不支持 keepdim参数(因为在 numpy 中,矩阵总是 是二维的)。例如,它适用于 np.array :

>>> import numpy as np
>>> A = np.eye(4)
>>> A
array([[ 1., 0., 0., 0.],
[ 0., 1., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]])
>>> np.sum(A, axis=1, keepdims=True)
array([[ 1.],
[ 1.],
[ 1.],
[ 1.]])

但是有了 np.matrix ,它不会:

>>> import numpy.matlib
>>> B = np.matlib.eye(4)
>>> np.sum(B, axis=1, keepdims=True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../numpy/core/fromnumeric.py", line 1832, in sum
return sum(axis=axis, dtype=dtype, out=out, **kwargs)
TypeError: sum() got an unexpected keyword argument 'keepdims'

但是,大多数数组/矩阵类型的对象都可以很容易地转换为数组 numpynp.array(<object>) ,这应该可以解决 numpy 中大多数子类对象的问题。可能是你的问题。您也可以简单地将结果包装回 np.matrix如果需要的话。

>>> B = np.matlib.eye(4)
>>> B = np.array(B)
>>> np.sum(B, axis=1, keepdims=True)
array([[ 1.],
[ 1.],
[ 1.],
[ 1.]])

但是,如果您的对象类 np.matrix输入,然后是 keepdims争论是没有意义的。矩阵总是二维的,所以sum函数不会减少维度,因此参数不会做任何事情。这就是它未针对矩阵实现的原因。

关于python - Numpy sum() 出现 'keepdims' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46420007/

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