(libtorch) How to print tensor without [ Variable[CPULongType]{} ]?


(Heng Ding) #1

Hi everyone, I am trying to use C++ frontend for predicting.

// Execute the model and turn its output into a tensor.
at::Tensor output = module->forward(inputs).toTensor();
std::cout << output << '\n';

Following the C++ tutorial, the standard c++ output function (std::cout) will generate type information “[ Variable[CPULongType]{} ]”.

0
[ Variable[CPULongType]{} ]
1
[ Variable[CPULongType]{} ]
0
[ Variable[CPULongType]{} ]
0
[ Variable[CPULongType]{} ]
1
[ Variable[CPULongType]{} ]
0
[ Variable[CPULongType]{} ]
1
[ Variable[CPULongType]{} ]
0
[ Variable[CPULongType]{} ]
1
[ Variable[CPULongType]{} ]

Does anyone know how to print predictions without [ Variable[CPULongType]{} ] ?


(Peter Goldsborough) #2

You can get the raw integer by calling output.item<int64_t>().

output.item() (without template arguments) returns a torch::Scalar and might also be useful output.


(Heng Ding) #3

Thanks, it works now!