欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

numpy矩阵求和 TypeError: sum() received an invalid combination of arguments - got (axis=int, )

程序员文章站 2022-03-03 14:36:42
...

代码:

target.sum(axis=0)

报错:

Traceback (most recent call last):
  File "/home/user1/test.py", line 101, in <module>
    t = target.sum(axis=0).reshape((1, 40))
TypeError: sum() received an invalid combination of arguments - got (axis=int, ), but expected one of:
 * ()
      didn't match because some of the keywords were incorrect: axis
 * (torch.dtype dtype)
      didn't match because some of the keywords were incorrect: axis
 * (tuple of ints dim, torch.dtype dtype)
 * (tuple of ints dim, bool keepdim, torch.dtype dtype)
 * (tuple of ints dim, bool keepdim)

原因:你操作的是一个torch.tensor,需要先转成numpy array才能进一步求和。

解决:

target = target.numpy() # convert torch.tensor to numpy array

感谢:Shai @ https://*.com/questions/54999926/pytorch-typeerror-eq-received-an-invalid-combination-of-arguments