【问题标题】:RuntimeError: mean(): input dtype should be either floating point or complex dtypes. Got Long insteadRuntimeError: mean(): input dtype 应该是浮点数或复数 dtypes。反而长了
【发布时间】:2021-11-29 17:57:48
【问题描述】:

我使用 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?

【问题讨论】:

    标签: pytorch tensor


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2019-11-18
      • 2013-05-07
      • 1970-01-01
      • 1970-01-01
      • 2021-01-16
      • 2020-04-13
      • 1970-01-01
      • 2020-02-21
      • 1970-01-01
      相关资源
      最近更新 更多