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

运行代码:

import torch
from torch.nn import L1Loss

inputs = torch.tensor([1,2,3])
targets = torch.tensor([1,2,5])

inputs = torch.reshape(inputs,(1,1,1,3))  # 1batch_size,1个channel,1行,3列
targets = torch.reshape(targets,(1,1,1,3))

loss = L1Loss()
result = loss(inputs,targets)

print(result)

报错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_第1张图片
出问题的代码:

inputs = torch.tensor([1,2,3])
targets = torch.tensor([1,2,5])

修改数据类型

inputs = torch.tensor([1,2,3],dtype=torch.float32)
targets = torch.tensor([1,2,5],dtype=torch.float32)

再次运行:

RuntimeError: mean(): input dtype should be either floating point or complex dtypes.Got Long instead_第2张图片

你可能感兴趣的:(pytorch,pytorch,深度学习,python)