如何在LibTorch(C++)中将torch::torch的类型从Float更改为Long 我用LIGRBARPy火炬C++ ++编写C++代码。 这里,我传递的是预测值和目标值,它们都是{1,1}大小的torch::张量 torch::Tensor loss = torch::nll_loss(predicted_value, target_value);

如何在LibTorch(C++)中将torch::torch的类型从Float更改为Long 我用LIGRBARPy火炬C++ ++编写C++代码。 这里,我传递的是预测值和目标值,它们都是{1,1}大小的torch::张量 torch::Tensor loss = torch::nll_loss(predicted_value, target_value);,c++,libtorch,C++,Libtorch,当我尝试评估上述内容时,我得到以下错误: 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 o

当我尝试评估上述内容时,我得到以下错误:

 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)
我试图搜索如何将浮点型张量转换为长型张量,但只能找到Python的文档。非常感谢您提出解决此问题的建议

张量。托托许::克龙给你长型

以下是张量to函数的重载定义:

内联张量张量::toScalarType数据类型,布尔非阻塞,布尔复制常量{ 静态自动表格=globalATenDispatch.getOpTableaten::传感器自身,ScalarType数据类型,bool non_blocking=False,bool copy=False->Tensor; return table->getOptensorTypeIdToBackendtype\u id,is_variable*this,dtype,non_blocking,copy; }
也许一开始就使用正确的类型。。?