expected device cpu and dtype Float but got device cpu and dtype Long
再练习torch时遇到的bug
将两个tensor相加,报错,不能相加
a = torch.arange(16).reshape((4, 4))
a
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
b = torch.zeros((4, 4))
b
tensor([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
print(torch.add(a, b))
报错如下
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-28-fbc2cda58a12> in <module>
----> 1 print(torch.add(a, b))
RuntimeError: expected device cpu and dtype Float but got device cpu and dtype Long
这里可以根据报错看出,它两的数据类型不一样。
解决方法
a = torch.arange(16).reshape((4, 4)).float()
将两个的数据类型改为一致的