What is the proper way to create a tensor from this object?

When I try putting the entire object into a tensor I seem to be getting different numbers somehow from a list of numbers that should be the same.

I’m using a library called MuJoCo and an object d->qpos, which I have been running a loop over to get the doubles from. It contains 28 doubles and have been accessing the doubles like so,

            int i;
            for (i = 0; i < 28; i++)
                std::cout << d->qpos[i] << " ";

This shows the numbers are ...  

1.57335e-15 0.0776263 0.72953 -2.25124e-16 -0.683949 -6.29293e-17 1.34495e-14 -0.486588 4.16118e-15 -0.0461098 0.00221961 -0.0907435 -2.7992 -0.708316 -0.0272932 -0.0461098 0.00221961 -0.0907435 -2.7992 -0.708316 0.0272932 0.514133 -0.646928 -1.57181 -0.514133 0.646928 -1.57181 1.1801e+17

When I try putting the whole entire object into a tensor without any indexing, for what should be the exact same sequence of numbers, I get…

            torch::Tensor a = torch::from_blob(d->qpos, {28});  

[ CPUFloatType{28} ]

Is there a problem with the data type? Should there be another option or is Libtorch not able to grab this entire object?
If I print this d->qpos object without Libtorch or anything else I get a memory address.

std::cout << d->qpos;  


I think I would rather grab the entire object at once since it would probably be faster than iterating over every number in the object. About that, if that is what I have to do, how should I go about it? Should I make a tensor out of each number and torch::cat them into a tensor? Because

Since you’re using C++, my guess is that this is a type issue. from_blob lets you pass an arbitrary pointer, and since you aren’t specifying a data type, it seems to be assuming that it should be interpreted as float (single-precision), but your writeup says the values are actually double-precision. If that’s the case, I’d change your call to:

torch::Tensor a = torch::from_blob(d->qpos, {28}, torch::kFloat64)

and see if that resolves your issue.

1 Like