RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #2 'target'
程序员文章站
2022-06-13 08:00:00
...
在用pytorch的时候,报错RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #2 'target'。研究了一下,发现是关于label的错误。修改办法总结一下:
1、RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #2 'target',在这个报错中,标红的地方是关键,找到程序中的label,比如说在loss处,如下所示:
Loss = torch.nn.CrossEntropyLoss()
loss = Loss(out, label)
修改的时候,直接在label后面加上.long(),如下所示:
Loss = torch.nn.CrossEntropyLoss()
loss = Loss(out, label.long())
2、多次报这个错误后发现,它说是什么类型,直接在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'