RuntimeError: mean(): input dtype should be either floating point or complex dtypes. Got Long instead

RuntimeError: mean(): input dtype should be either floating point or complex dtypes. Got Long instead

我使用 pytorch 编写了以下代码,运行 出现运行时错误:

tns = torch.tensor([1,0,1])
tns.mean()
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-666-194e5ab56931> in <module>
----> 1 tns.mean()

RuntimeError: mean(): input dtype should be either floating point or complex dtypes. Got Long instead.

但是,如果我将张量更改为浮动,错误就会消失:

tns = torch.tensor([1.,0,1])
tns.mean()
---------------------------------------------------------------------------
tensor(0.6667)

我的问题是为什么会发生错误。第一个tenor的数据类型是int64而不是Long,为什么PyTorch会把它当作Long?

这是因为 torch.int64torch.long 都引用相同的数据类型,即 64 位有符号整数。有关所有数据类型的概述,请参阅 here