RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #2 'target'
程序员文章站
2022-03-09 13:35:55
...
pytorch报错
RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #2 'target'
原因 使用了loss = criterion(output,target)
loss = criterion(output,target)
# 而output 和 target的类型都是
'torch.cuda.FloatTensor'
'torch.cuda.FloatTensor'
output.shape :
torch.Size([64, 10])
target.shape :
torch.Size([64, 1, 28, 28])
解决办法
target1 = torch.LongTensor(target)
loss = criterion(output,target1)
当然这里是我自己比较错了,应该比较label的
推荐阅读
-
报错: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'