How to convert vector<int> into c++ torch tensor?

Hi,
I have a vector of int. All I want is to convert such vector into torch tensor.
I tried this:

vector<int> x = {10, 15, 20, 100, 500};
vector<torch::jit::IValue> inputs;
inputs.push_back(torch::from_blob(x.data(), {1, x.size()}, torch::kInt64));

But that returns me different numbers(see below).

6.4425e+10  4.2950e+11  1.3972e+14  2.5700e+02  9.4817e+13
[ Variable[CPULongType]{1,5} ]

What I can still do is this:

vector<int> x = {10, 15, 20, 100, 500};
vector<torch::jit::IValue> inputs;
torch::Tensor t = torch::zeros({1, (int)x.size()}, torch::kInt64);
int counter = 0;
for (int i : x)
{
    t[0][counter] = i;
    counter++;
}
inputs.push_back(t);

But is not there other way how to convert vector into torch::Tensor?

1 Like

I had some issues creating tensors with a long type directly, but I’m sure that is more that I don’t know what I’m doing in c++ rather than it not working. However, I did something like the following recently for this.

vector<int> v({1, 2, 3});
auto opts = torch::TensorOptions().dtype(torch::kInt32);
torch::Tensor t = torch::from_blob(t.data(), {3}, opts).to(torch::kInt64);

Thanks for the reply. It worked :slight_smile: I think my problem was setting its type directly to torch::kInt64 instead of setting it to torch::kInt32 and then converting it to torch::kInt64.

No worries, mini-update to the answer. If one makes vector<int> a vector<long> then you can directly convert to a long tensor.

I ran into a similar issue and figured the problem is due to torch::from_blob not taking ownership of the vector from which the tensor has been created. Solution for this is to use torch::from_blob with clone().

For example, in the OP’s question, if the inputs are created from a vector vec in a certain scope, but used when vec is no longer in scope, then the inputs is likely to have garbage values.

    torch::Tensor inputs, cloned_inputs;
    {
        std::vector<long> vec = {10, 15, 20, 100, 500};
        auto options = torch::TensorOptions().dtype(at::kLong);
        inputs = torch::from_blob(vec.data(), {1, vec.size()}, options);
        cloned_inputs = torch::from_blob(vec.data(), {1, vec.size()}, options).clone();
        std::cout << "inputs within scope of vec: \n" << inputs << std::endl;
        std::cout << "cloned_inputs within scope of vec: \n" << cloned_inputs << std::endl;
    }
    std::cout << "inputs beyond scope of vec: \n" << inputs << std::endl;
    std::cout << "cloned_inputs beyond scope of vec: \n" << cloned_inputs << std::endl;

This ouputs:

inputs within scope of vec: 
  10   15   20  100  500
[ CPULongType{1,5} ]
cloned_inputs within scope of vec: 
  10   15   20  100  500
[ CPULongType{1,5} ]
inputs beyond scope of vec: 
 9.4045e+13  1.0000e+00  1.0000e+00  0.0000e+00  5.0000e+02
[ CPULongType{1,5} ]
cloned_inputs beyond scope of vec: 
  10   15   20  100  500
[ CPULongType{1,5} ]