How can I convert a NumCPP array to a tensor?

Alio: <p>I have an array in a C++ implementation of Numpy called <code>NumCPP</code>. How can I convert it into a tensor in LibTorch?</p>
<p>I try<br>
<code>torch::Tensor a;</code></p>
<p>I’m getting the error</p>
<pre><code class="lang-auto">
/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&lt;double&gt; a’
  176 |             nc::NdArray &lt;double&gt; a = {d-&gt;qpos[0]};
      |                                  ^
</code></pre>
<p>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?</p>

eqy: <p>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 <code>from_blob</code> could be used:<br>
<a href="https://pytorch.org/cppdocs/api/function_namespacetorch_1aff6f8e6185457b2b67a1a9f292effe6b.html" rel="noopener nofollow ugc">Function torch::from_blob(void *, at::IntArrayRef, const at::TensorOptions&amp;) — PyTorch master documentation</a></p>

Alio: <p>Thank you. What is “blob”? Why is it called that?<br>
Also, what is IntArrayRef?</p>

eqy: <p>My understanding is that a “blob” refers to the fact that it is just a raw “blob” of binary data. See <a href="https://pytorch.org/cppdocs/notes/tensor_creation.html" class="inline-onebox" rel="noopener nofollow ugc">Tensor Creation API — PyTorch master documentation</a> which should have more of a description on <code>IntArrayRef</code>, but it really is just a way to specify the shape  of the tensor that should be created from the blob.</p>

Alio: <p>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 <code>d-&gt;qpos</code> is a single (double) number.</p>
<p>I’ll be using <code>torch::cat</code> to plug about 2000 doubles into a 1D tensor as a my final goal.<br>
I appreciate the spoon feeding. I’m not sure how else to pick up the best practices. Thank you.</p>
<pre><code class="lang-auto">    double array[] = {d-&gt;qpos[10]};
    auto options = torch::TensorOptions().dtype(torch::kFloat32);
    torch::Tensor a = torch::from_blob(array, {1}, options);
</code></pre>

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

Alio: <aside class="quote no-group" data-username="eqy" data-post="6" data-topic="158068">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/eqy/48/46233_2.png" class="avatar"> eqy:</div>
<blockquote>
<p><code>d-&gt;qpos</code> directly (assuming this has 2000 doubles),</p>
</blockquote>
</aside>
<p>Oh, thank you. I didn’t know that would work. I was asking a contributor from the library this <code>d-&gt;qpos</code> 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.</p>
<p>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 <code>d-&gt;qpos[i]</code>. When I print <code>d-&gt;qpos</code> I get an address in memory.<br>
When I loop over <code>d-&gt;qpos</code> I get the first 28 values and zeros after that.<br>
With <code>torch::Tensor a = torch::from_blob(d-&gt;qpos, {1000}, options);</code> I get a tensor with 1000 values though I should only have 28 number from this and the rest zeros.</p>
<p>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.</p>
<p>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.</p>