TypeError: mean() received an invalid combination of arguments - got (axis=int, )
运行程序
import torch
A = torch.arange(20,dtype=torch.float32).reshape(5,4)
#dtype=torch.float32防止numel()报错RuntimeError: Can only calculate the mean of floatingtypes. Got Long instead.
A.mean(axis=0),A.sum(axis=0) / A.shape[0]
出现如下错误:
TypeError Traceback (most recent call last) <ipython-input-18-a6b2437fb377> in <module> ----> 1 A.mean(axis=0),A.sum(axis=0) / A.shape[0] TypeError: mean() 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)
解决方法:
修改把axis替换成dim代码如下:
A.mean(dim=0),A.sum(dim=0) / A.shape[0]#此处为torch的mean()而不是numpy中的mean()
#dim
#指定为1时,求得是行的平均值;
#指定为0时,求得是列的平均值。
输出如下:
(tensor([ 8., 9., 10., 11.]), tensor([ 8., 9., 10., 11.]))
原因分析:此处的mean()函数使用的torch里面的,而不是numpy的mean()函数