How to turn a list of tensor to tensor?

11129: <p>I have a list and there are many tensor in the list<br>
I want to turn it to just tensor, and I can put it to dataloader</p>
<p>I use for loop and cat the tensor but it is very slow, data size is about 4,800,000</p>

richard: <p>If they’re all the same size, then you could <code>torch.unsqueeze</code> them in dimension 0 and then <code>torch.cat</code> the results together.</p>

Hyungjoo_Cho: <pre><code>a = []
for i in range(100000):
    a.append(torch.rand(1, 100, 100)

b = torch.Tensor(100000, 100, 100)
torch.cat(a, out=b)</code></pre>

dmadeka1: <p>I was always doing something like:</p>
<pre><code class="lang-python">a = [torch.FloatTensor([1]).view(1, -1), torch.FloatTensor([2]).view(1, -1)]
torch.stack(a)
</code></pre>
<p>Gives me:</p>
<pre><code class="lang-python">(0 ,.,.) = 
  1

(1 ,.,.) = 
  2
[torch.FloatTensor of size 2x1x1]
</code></pre>

11129: <p>Actually</p>
<p>I have two list</p>
<p>list 1<br>
a = [[tensor 40], [tensor 40], [tensor 40], …] (2400000 tensor in list each tensor size is 40)<br>
b = [[tensor 40], [tensor 40], [tensor 40], …] (2400000 tensor in list each tensor size is 40)</p>
<p>I want to concat a and b to c<br>
c is a tensor and size is torch.Size([4800000, 40])</p>
<p>I use this method to solve my problem<br>
a = torch.stack(a)<br>
b = torch.stack(b)<br>
c = torch.cat((a, b))</p>
<p>Thank you for all your help !</p>

SimonW: <p>Or you can just <code>torch.stack(a + b)</code></p>

11129: <p>It works.<br>
Thank you.</p>

NLPpupil: <p>This is how it should be done.</p>

Nithin_Vasisth: <p>a = torch.stack(a) worked for me</p>

Brando_Miranda: <p>To me it’s sort of unintuitive, why wouldn’t using the tensor class work?</p>
<pre><code class="lang-auto">torch.tensor([ torch.tensor([i]).repeat(15) for i in range(0,5)])
</code></pre>
<p>the list is the same size and it’s really a matrix/tensor already…but somehow only:</p>
<pre><code class="lang-auto">torch.stack([ torch.tensor([i]).repeat(15) for i in range(0,5)])
tensor([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
        [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
        [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]])
</code></pre>
<p>worked.</p>
<p>Btw, is this this most efficient way to do it in a vectorized way?</p>

Brando_Miranda: <p>check this out but summary use <code>torch.stack</code> if you want to respect the original nesting of the lists (by having a tensor with many indices and dimensions) and incrementally create it. There might be better ways but that works for me.</p>
<aside class="quote quote-modified" data-post="8" data-topic="59949">
  <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/brando_miranda/48/14355_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/best-way-to-convert-a-list-to-a-tensor/59949/8">Best way to convert a list to a tensor?</a> 
  </div>
  <blockquote>
    hahaha, yea I see I didn’t make a very solid memory/understanding when I wrote that last year (or it’s been to long since?). Perhaps I can finally sort this out in my head, though in my defence, there does seem to be a lot of discussions surrounding this topic (which isn’t helping to digest this): 

<a href="https://discuss.pytorch.org/t/how-to-convert-array-to-tensor/28809/25" class="inline-onebox">How to convert array to tensor? - #25 by mehran2020</a>
<a href="https://discuss.pytorch.org/t/how-to-turn-a-list-of-tensor-to-tensor/8868/9" class="inline-onebox">How to turn a list of tensor to tensor? - #9 by Nithin_Vasisth</a>
<a href="https://discuss.pytorch.org/t/how-to-turn-a-list-of-tensor-to-tensor/8868/10" class="inline-onebox">How to turn a list of tensor to tensor? - #10 by Brando_Miranda</a>
<a href="https://discuss.pytorch.org/t/converting-list-to-tensor/70120/8" class="inline-onebox">Converting list to t…</a>
  </blockquote>
</aside>

<pre><code class="lang-auto">
# %%

import torch

# stack vs cat

# cat "extends" a list in the given dimension e.g. adds more rows or columns

x = torch.randn(2, 3)
print(f'{x.size()}')

# add more rows (thus increasing the dimensionality of the column space to 2 -&gt; 6)
xnew_from_cat = torch.cat((x, x, x), 0)
print(f'{xnew_from_cat.size()}')

# add more columns (thus increasing the dimensionality of the row space to 3 -&gt; 9)
xnew_from_cat = torch.cat((x, x, x), 1)
print(f'{xnew_from_cat.size()}')

print()

# stack serves the same role as append in lists. i.e. it doesn't change the original
# vector space but instead adds a new index to the new tensor, so you retain the ability
# get the original tensor you added to the list by indexing in the new dimension
xnew_from_stack = torch.stack((x, x, x, x), 0)
print(f'{xnew_from_stack.size()}')

xnew_from_stack = torch.stack((x, x, x, x), 1)
print(f'{xnew_from_stack.size()}')

xnew_from_stack = torch.stack((x, x, x, x), 2)
print(f'{xnew_from_stack.size()}')

# default appends at the from
xnew_from_stack = torch.stack((x, x, x, x))
print(f'{xnew_from_stack.size()}')

print('I like to think of xnew_from_stack as a \"tensor list\" that you can pop from the front')

print()

lst = []
print(f'{x.size()}')
for i in range(10):
    x += i  # say we do something with x at iteration i
    lst.append(x)
# lstt = torch.stack([x for _ in range(10)])
lstt = torch.stack(lst)
print(lstt.size())

print()

# lst = []
# print(f'{x.size()}')
# for i in range(10):
#     x += i  # say we do something with x at iteration i
#     for j in range(11):
#         x += j
#         lstx
#     lst.append(x)
# # lstt = torch.stack([x for _ in range(10)])
# lstt = torch.stack(lst)
# print(lstt.size())
</code></pre>

Nil_MSh: <p>Sorry to bother you after all this time.<br>
I have target['a] which is a list of tensors. when I use</p>
<pre><code class="lang-auto">torch.unsqueeze(target['a'], 0)
</code></pre>
<p>it gives me this error:</p>
<blockquote>
<p>TypeError: unsqueeze(): argument ‘input’ (position 1) must be Tensor, not list</p>
</blockquote>
<p>what is the correct way to use torch.unsqueeze?</p>

Brando_Miranda: <p>tensorify is the best! <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>
<aside class="quote quote-modified" data-post="8" data-topic="59949">
  <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/brando_miranda/48/14355_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/best-way-to-convert-a-list-to-a-tensor/59949/8">Best way to convert a list to a tensor?</a> 
  </div>
  <blockquote>
    hahaha, yea I see I didn’t make a very solid memory/understanding when I wrote that last year (or it’s been to long since?). Perhaps I can finally sort this out in my head, though in my defence, there does seem to be a lot of discussions surrounding this topic (which isn’t helping to digest this): 

<a href="https://discuss.pytorch.org/t/how-to-convert-array-to-tensor/28809/25" class="inline-onebox">How to convert array to tensor? - #25 by mehran2020</a>
<a href="https://discuss.pytorch.org/t/how-to-turn-a-list-of-tensor-to-tensor/8868/9" class="inline-onebox">How to turn a list of tensor to tensor? - #9 by Nithin_Vasisth</a>
<a href="https://discuss.pytorch.org/t/how-to-turn-a-list-of-tensor-to-tensor/8868/10" class="inline-onebox">How to turn a list of tensor to tensor? - #10 by Brando_Miranda</a>
<a href="https://discuss.pytorch.org/t/converting-list-to-tensor/70120/8" class="inline-onebox">Converting list to t…</a>
  </blockquote>
</aside>