- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是一段神经网络代码示例:
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
现在我很困惑这个错误是怎么发生的。
最佳答案
注意在keepdims
下docs 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 thesum
method of sub-classes ofndarray
, however any non-default value will be. If the sub-classessum
method does not implement keepdims any exceptions will be raised.
所以它在这里声明,如果您使用的是 numpy.ndarray
的子类, 那么如果相应的 sum
就会出现这个错误子类的函数尚未用它定义。
请注意,在您的错误中,它引用了行 1812
在numpy/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'
但是,大多数数组/矩阵类型的对象都可以很容易地转换为数组 numpy
与 np.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/
我的应用程序从一个有 5 个选项卡的选项卡栏 Controller 开始。一开始,第一个出现了它的名字,但其他四个没有名字,直到我点击它们。然后根据用户使用的语言显示名称。如何在选项卡栏出现之前设置选
我有嵌套数组 json 对象(第 1 层、第 2 层和第 3 层)。我的问题是数据表没有出现。任何相关的 CDN 均已导入。该表仅显示部分。我引用了很多网站,但都没有解决我的问题。 之前我使用标准表来
我正在尝试设置要显示的 Parse PFLoginViewController。这是我的一个 View Controller 的类。 import UIKit import Parse import
我遇到了这个问题,我绘制的对象没有出现在 GUI 中。我知道它正在被处理,因为数据被推送到日志文件。但是,图形没有出现。 这是我的一些代码: public static void main(Strin
我有一个树状图,其中包含出现这样的词...... TreeMap occurrence = new TreeMap (); 字符串 = 单词 整数 = 出现次数。 我如何获得最大出现次数 - 整数,
因此,我提示用户输入变量。如果变量小于 0 且大于 10。如果用户输入 10,我想要求用户再次输入数字。我问时间的时候输入4,它说你输入错误。但在第二次尝试时效果很好。例如:如果我输入 25,它会打印
我已经用 css overflow 属性做了一个例子。在这个例子中我遇到了一个溢出滚动的问题。滚动条出现了,但没有工作意味着每当将光标移动到滚动条时,在这个滚动条不活动的时间。我对此一无所知,所以请帮
我现在正在做一个元素。当您单击一个元素时,会出现以下信息,我想知道如何在您单击下一个元素而不重新单击同一元素时使其消失....例如,我的元素中有披萨,我想单击肉披萨看到浇头然后点击奶酪披萨看到浇头和肉
我有一个路由器模块,它将主题与正则表达式进行比较,并将出现的事件与一致的键掩码链接起来。 (它是一个简单的 url 路由过滤,如 symfony http://symfony.com/doc/curr
这个问题在这里已经有了答案: 9年前关闭。 Possible Duplicate: mysql_fetch_array() expects parameter 1 to be resource, bo
我在底部有一个带有工具栏的 View ,我正在使用 NavigationLink 导航到该 View 。但是当 View 出现时,工具栏显示得有点太低了。大约半秒钟后,它突然跳到位。它只会在应用程序启
我试图在我的应用程序上为背景音乐添加一个 AVAudioPlayer,我正在主屏幕上启动播放器,尝试在应用程序打开时开始播放但出现意外行为... 它播放并立即不断创建新玩家并播放这些玩家,因此同时播放
这是获取一个数字,获取其阶乘并将其加倍,但是由于基本情况,如果您输入 0,它会给出 2 作为答案,因此为了绕过它,我使用了 if 语句,但收到错误输入“if”时解析错误。如果你们能提供帮助,我真的很感
暂停期间抛出异常 android.os.DeadObjectException 在 android.os.BinderProxy.transactNative( native 方法) 在 androi
我已经为猜词游戏编写了一些代码。它从用户输入中读取字符并在单词中搜索该字符;根据字符是否在单词中,程序返回并控制一些变量。 代码如下: import java.util.Random; import
我是自动化领域的新手。这是我的简单 TestNG 登录代码,当我以 TestNG 身份运行该代码时,它会出现 java.lang.NullPointerException,双击它会突出显示我导航到 U
我是c#程序员,我习惯了c#的封装语法和其他东西。但是现在,由于某些原因,我应该用java写一些东西,我现在正在练习java一天!我要创建一个为我自己创建一个虚拟项目,以便让自己更熟悉 Java 的
我正在使用 Intellij,我的源类是 main.com.coding,我的资源文件是 main.com.testing。我将 spring.xml 文件放入资源文件中。 我的测试类位于 test.
我想要我的tests folder separate到我的应用程序代码。我的项目结构是这样的 myproject/ myproject/ myproject.py moduleon
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 6 年前。 因此,我尝试比较 2 个值,一个
我是一名优秀的程序员,十分优秀!