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});  

-1.7520e+00
-1.5310e+24
 2.6897e-02
-1.4977e-36
 1.4053e+00
-8.6043e+18
 1.8074e+00
 5.1051e-21
-2.1511e-02
 3.1887e-37
-1.7960e+00
-1.0073e-10
-1.7839e-02
 1.6544e+38
 3.4738e-02
 7.4359e-09
-1.7433e+00
-1.3105e+24
 2.9631e-02
 4.4223e-22
-1.3094e+00
 1.4495e-36
 8.8353e-01
 4.5674e-36
-1.4315e+00
 5.7768e-37
-2.0999e+00
[ 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;  

0x55c510d887c0

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