运行时错误:mean():输入数据类型应该是浮点或复杂数据类型。反而长了

Chr*_*ris 5 pytorch tensor

我使用 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.
Run Code Online (Sandbox Code Playgroud)

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

tns = torch.tensor([1.,0,1])
tns.mean()
---------------------------------------------------------------------------
tensor(0.6667)
Run Code Online (Sandbox Code Playgroud)

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

Goo*_*eds 4

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