当前位置: 代码迷 >> 综合 >> RuntimeError: expected device cpu and dtype Float but got device cpu and dtype Long
  详细解决方案

RuntimeError: expected device cpu and dtype Float but got device cpu and dtype Long

热度:25   发布时间:2024-01-29 00:26:42.0

prob * SCORES 报错:
RuntimeError: expected device cpu and dtype Float but got device cpu and dtype Long
其中prob和SCORES的数据及类型是:
SCORES = torch.tensor([[1, 2, 3, 4, 5]])
In [71]: prob
Out[71]: 
tensor([[0.2000, 0.2000, 0.2000, 0.2000, 0.2000],
[0.2001, 0.2002, 0.1998, 0.2001, 0.1998],
[0.2000, 0.2000, 0.2000, 0.2000, 0.2000],
...,
[0.2000, 0.2000, 0.2000, 0.2000, 0.2000],
[0.2000, 0.2000, 0.2000, 0.2000, 0.2000],
[0.1999, 0.2000, 0.2000, 0.2000, 0.2000]], grad_fn=<SoftmaxBackward>)

解决方法:
将SCORES由整数类型改为float类型:
SCORES = torch.tensor([[1., 2., 3., 4., 5.]])
或者:
SCORES = SCORES.float()

  相关解决方案