How can I convert a NumCPP array to a tensor?

I have an array in a C++ implementation of Numpy called NumCPP. How can I convert it into a tensor in LibTorch?

I try
torch::Tensor a;

I’m getting the error

/home/iii/tor/m_gym/mujoco_gym.cpp:207:27: error: conflicting declaration ‘at::Tensor a’
  207 |             torch::Tensor a;
      |                           ^
/home/iii/tor/m_gym/mujoco_gym.cpp:176:34: note: previous declaration as ‘nc::NdArray<double> a’
  176 |             nc::NdArray <double> a = {d->qpos[0]};
      |                                  ^

I have a second question which would make the first question moot, if it’s possible. I was using NumCPP to append a one dimensional array. Is this possible in LibTorch? I have about 2000 doubles I want to get into a 1D LibTorch tensor. Is there anyway to do this in LibTorch? Can I append the doubles to a LibTorch array?

If you know the underlying data layout of the array and/or how to get it to match the expected Tensor layout, then you could check if from_blob could be used:
Function torch::from_blob(void *, at::IntArrayRef, const at::TensorOptions&) — PyTorch master documentation

Thank you. What is “blob”? Why is it called that?
Also, what is IntArrayRef?

My understanding is that a “blob” refers to the fact that it is just a raw “blob” of binary data. See Tensor Creation API — PyTorch master documentation which should have more of a description on IntArrayRef, but it really is just a way to specify the shape of the tensor that should be created from the blob.

This is what I was able to get into a tensor without throwing an error. I’m new to C++ and Libtorch so if you wouldn’t mind, is this good practice? It seems like a lot of code. The d->qpos is a single (double) number.

I’ll be using torch::cat to plug about 2000 doubles into a 1D tensor as a my final goal.
I appreciate the spoon feeding. I’m not sure how else to pick up the best practices. Thank you.

    double array[] = {d->qpos[10]};
    auto options = torch::TensorOptions().dtype(torch::kFloat32);
    torch::Tensor a = torch::from_blob(array, {1}, options);

I would check what happens if you passed in e.g., d->qpos directly (assuming this has 2000 doubles), and setting the shape to something like {2000}. Even casting to a double pointer should work, as long as the array isn’t liable to fall out of scope etc., as from_blob doesn’t take ownership of the memory. However, taking in a double array and then setting the dtype to kFloat32 looks suspicious—the data should either be converted beforehand or the dtype setting should be changed.

Oh, thank you. I didn’t know that would work. I was asking a contributor from the library this d->qpos object comes from if I can grab the whole thing and put it into a tensor and he assured me I would have to run a loop over it.

This is putting numbers in a tensor, but I don’t know these are the same as what I would be getting from a loop d->qpos[i]. When I print d->qpos I get an address in memory.
When I loop over d->qpos I get the first 28 values and zeros after that.
With torch::Tensor a = torch::from_blob(d->qpos, {1000}, options); I get a tensor with 1000 values though I should only have 28 number from this and the rest zeros.

Though I’m not sure about the options and the data type. I get wildly different numbers when I set it to 16 vs 32.

I guess I’ll looks at the first 28 numbers from a loop and the first 28 numbers from this method and see if they are the same.