Index a tensor using tensor does not work in Libtorch

In python, I can easily index a tensor using another tensor like the example below.
image

But in C++, when I executed the following code, I got an error.
auto category = torch::tensor({1,2,7,9});
auto sample = torch::tensor({1,0,2,2});
std::cout<<category[sample]<<std::endl;
image

What would be the right way to do this? Thanks!

Hi,

Yes we couldn’t overload the [] operator to do be the same as in python. You can check the doc for the corresponding c++ API. I think category.index({sample}); in this case.

1 Like