Expected object of scalar type Float but got scalar type Long for argument #2 'target'
程序员文章站
2022-06-13 08:00:12
...
# 损失函数
# nn.MSELoss 来计算均方误差
# nn.CrossEntropyLoss 来计算交叉熵损失
output = net(input)
target = Variable(t.arange(0, 10))
# target = target.float()
criterion = nn.MSELoss()
loss = criterion(output, target)
loss
在上面注释代码里加上一行即可
target = target.float()
得到答案
tensor(28.3906, grad_fn=)
推荐阅读
-
报错:Expected object of scalar type Float but got scalar type Long for argument #2 'target'
-
RuntimeError: Expected tensor for argument #1 'indices' to have scalar type Long; but got CUDAType
-
RuntimeError: Expected object of scalar type Long but got scalar type Int for argument #2 'target'
-
RuntimeError: Expected tensor for argument #1 ‘indices‘ to have scalar type Long; but got CUDAFloatT
-
RuntimeError: Expected tensor for argument #1 ‘indices‘ to have scalar type Long; but got torch.IntT
-
Expected tensor for argument #1 ‘indices‘ to have scalar type Long; but got torch.cuda.FloatTensor i
-
Expected object of scalar type Long but got scalar type Int for argument #2 'target'
-
RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #2 'target'
-
Expected object of scalar type Long but got scalar type Double for argument #2 'target'
-
Expected object of scalar type Float but got scalar type Long for argument #2 'target'