Stack tensor data to batch

s0H57: <p>Hello all,<br>
i couldn’t really find any good resource on what is the best way to stack my 3D tensor data into a batch which i can feed into my <code>model.forward()</code> .<br>
Currently what i got from the tutorials is a<br>
<code>std::vector&lt;torch::jit::IValue&gt; inputs;</code><br>
When i <code>push_back</code> my tensors into the vector and pass it directly into the forward function it interprets the number of elements as input arguments which is not what i want.<br>
How can i concat my tensors into a given batch size?<br>
I also found <code>torch::cat()</code> but i need a dynamic way to stack the data tensors into batches.</p>

Sunburnt-Vampire: <p>Created an account just to answer this question after finding no answer and despairing. Specifically to answer the question of “how to submit different sized tensors as forward input”. Original poster seems to have wanted this to implement a batch with different sized samples within each batch which… is a whole extra can of worms. I’ll be focusing on how to implement forward(self, dynamicListOfInputs).</p>
<p>As best I can tell you simply cannot. The methods which others suggest such as</p>
<blockquote>
<p>std::vector<a>torch::jit::IValue</a> inputs;</p>
</blockquote>
<p>Assume that the forward function itself (e.g. the python code if transferring to C++ like me) is defined as forward(self, x, y, z).</p>
<p>However if you tried to keep things neat and tidy like me and defined your function as forward(self, listOfInputs) then you’ll get no end of errors. Libtorch does not like lists of undefined size.</p>
<p>You only option in libtorch seem to be:</p>
<ol>
<li>use cat() to make one large super-tensor and break it up in the forward function</li>
<li>if tensors are different sizes and cat() is no an option, explicitly pass each one in separately as forward(x,y,z).</li>
</ol>
<p>The tensors themselves can be dynamically sized (e.g. to have different batch sizes based on available GPU) however the number of tensors in the list cannot. You must explicitly list each tensor as a separate argument in the definition of the forward function.</p>
<p>Hopefully this helps someone out there.</p>

Tanmay_Pani: <p>I am new to using libtorch, but if you have you tensors in something like,</p>
<blockquote>
<p><code>std::vector&lt;torch::Tensor&gt; tensors;</code></p>
</blockquote>
<p>then this should work well:</p>
<blockquote>
<p><code>torch::stack(tensors, 0);</code></p>
</blockquote>
<p>Better yet, you could load your inputs into a <code>std::vector&lt;torch::Tensor&gt;</code> wrapped custom dataset class inheriting from <code>torch::data::Dataset&lt;&gt;</code> followed by using the <code>torch::data::transforms::Stack&lt;&gt;()</code> transformation through the <code>map</code> function in <code>Dataset</code> class, which I think (not sure of it yet) will lazily apply the <code>torch::stack</code> operation on batches of input in your training loop. You can find a good demo for MNIST images <a href="https://krshrimali.github.io/posts/2019/07/custom-data-loading-using-pytorch-c-api/" rel="noopener nofollow ugc">here</a>.</p>

fangniu: <p>thank you very much! I meet the same problems now.</p>