How to concatenate list of pytorch tensors?

erogol: <p>Suppose I have a list tensors in the same size. Is there any unified function to merge all these like <code>np.array(array_list)</code> in case you have list or numpy arrays.</p>
<p>This is my current solution</p>
<pre><code> data = th.zeros([len(imgs), imgs[0].size()[0], imgs[0].size()[1], imgs[0].size()[2]])
 for i, img in enumerate(imgs): 
       print(img.size())
       print(img.type())
       data[i] = img</code></pre>

fmassa: <p>You can use <a href="http://pytorch.org/docs/torch.html#torch.cat"><code>torch.cat</code></a> or <a href="http://pytorch.org/docs/torch.html#torch.stack"><code>torch.stack</code></a></p>

Brando_Miranda: <p>just in case you were wondering about the difference:</p>
<aside class="onebox stackexchange" data-onebox-src="https://stackoverflow.com/questions/54307225/whats-the-difference-between-torch-stack-and-torch-cat-functions/54307331#54307331">
  <header class="source">

      <a href="https://stackoverflow.com/questions/54307225/whats-the-difference-between-torch-stack-and-torch-cat-functions/54307331#54307331" target="_blank" rel="noopener nofollow ugc">stackoverflow.com</a>
  </header>

  <article class="onebox-body">
      <a href="https://stackoverflow.com/users/4280242/jatentaki" target="_blank" rel="noopener nofollow ugc">
    <img alt="Jatentaki" src="https://www.gravatar.com/avatar/ad477495413dec267326a2945e06d8bf?s=256&amp;d=identicon&amp;r=PG&amp;f=y&amp;so-version=2" class="thumbnail onebox-avatar" width="256" height="256">
  </a>

<h4>
  <a href="https://stackoverflow.com/questions/54307225/whats-the-difference-between-torch-stack-and-torch-cat-functions/54307331#54307331" target="_blank" rel="noopener nofollow ugc">What's the difference between torch.stack() and torch.cat() functions?</a>
</h4>

<div class="tags">
  <strong>python, machine-learning, deep-learning, pytorch</strong>
</div>

<div class="date">
  
  answered by
  <a href="https://stackoverflow.com/users/4280242/jatentaki" target="_blank" rel="noopener nofollow ugc">
    Jatentaki
  </a>
  on <a href="https://stackoverflow.com/questions/54307225/whats-the-difference-between-torch-stack-and-torch-cat-functions/54307331#54307331" target="_blank" rel="noopener nofollow ugc">11:31AM - 22 Jan 19 UTC</a>
</div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<hr>
<p><code>stack</code></p>
<blockquote>
<p>Concatenates sequence of tensors along a <strong>new dimension</strong>.</p>
</blockquote>
<p><code>cat</code></p>
<blockquote>
<p>Concatenates the given sequence of seq tensors <strong>in the given dimension</strong>.</p>
</blockquote>
<p>So if <code>A</code> and <code>B</code> are of shape (3, 4), <code>torch.cat([A, B], dim=0)</code> will be of shape (6, 4) and <code>torch.stack([A, B], dim=0)</code> will be of shape (2, 3, 4).</p>

Hiperdyne19012: <p>What if A is of shape (1,3,4) and B is (3,4)?</p>
<p>I want the result to be (2,3,4). How do I do this?</p>

harsha_g: <pre><code class="lang-auto">a = torch.rand(1, 3, 4)
print(a.shape)
b = torch.rand(3, 4)
print(b.shape)
b = b.unsqueeze(0)
print(b.shape)
c = torch.cat([a, b], dim=0)
print(c.shape)
</code></pre>

Hiperdyne19012: <p>Thank you, it works.</p>