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?

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.