Torch C++: Getting the value of a int tensor by using *.data()
In the C++ version of Libtorch, I found that I can get the value of a float tensor by *tensor_name[0].data&lt;float&gt;(), in which instead of 0 I can use any other valid index. But, when I have defined an int tensor by adding option at::kInt into the tensor creation, I cannot use this structure to get the value of the tensor, i.e. something like *tensor_name[0].data&lt;at::kInt&gt;() or *tensor_name[0].data&lt;int&gt;() does not work and the debugger keeps saying that Couldn't find method at::Tensor::data&lt;at::kInt&gt; or Couldn't find method at::Tensor::data&lt;int&gt;.
I can get the values by auto value_array = tensor_name=accessor&lt;int,1&gt;(), but it was easier to use *tensor_name[0].data&lt;int&gt;(). Can you please let me know how I can use data&lt;&gt;() to get the value of an int tensor?
I also have a same problem with bool type.

Use item&lt;dtype&gt;() to get a scalar out of a Tensor.

int main() {
  torch::Tensor tensor = torch::randint(20, {2, 3});
  std::cout &lt;&lt; tensor &lt;&lt; std::endl;
  int a = tensor[0][0].item&lt;int&gt;();
  std::cout &lt;&lt; a &lt;&lt; std::endl;
  return 0;
}

~/l/build ❯❯❯ ./example-app
  3  10   3
  2   5   8
[ Variable[CPUFloatType]{2,3} ]
3


The following code prints 0 (tested on Linux with the stable libtorch):

#include &lt;torch/script.h&gt;
#include &lt;iostream&gt;                                     

int main(int argc, const char* argv[])                  
{
    auto indx = torch::zeros({20},at::dtype(at::kLong));
    std::cout &lt;&lt; indx[0].item&lt;long&gt;() &lt;&lt; std::endl;

    return 0;
}

