我使用 LibTorch (PyTorch C++ API) 用 C++ 进行编码。在这里,我传递了 Predicted_value 和 target_value,它们都是大小为 {1, 1} 的 torch::Tensor。
torch::Tensor loss = torch::nll_loss(predicted_value, target_value);
Run Code Online (Sandbox Code Playgroud)
当我尝试评估上述内容时,出现以下错误:
0.4997 [ Variable[CPUFloatType]{1,1} ] # printout of predicted_value
-0.5392 [ Variable[CPUFloatType]{1,1} ] # printout of target_value
terminate called after throwing an instance of 'c10::Error'
what(): Expected object of scalar type Long but got scalar type Float for argument #2 'target' in call to _thnn_nll_loss_forward (checked_dense_tensor_unwrap at ../../aten/src/ATen/Utils.h:84)
Run Code Online (Sandbox Code Playgroud)
我尝试搜索如何将 float 类型张量转换为 long 类型张量,但只能找到 Python 的文档。非常感谢解决这个问题的建议!