Converting list to tensor

111229: <p>There is a variable ‘tmp’ (3 dimension).</p>
<pre><code class="lang-auto">type(tmp) -&gt; &lt;class 'list'&gt;
type(tmp[0]) -&gt; &lt;class 'torch.Tensor'&gt;
type(tmp[0][0]) -&gt; &lt;class 'torch.Tensor'&gt;

</code></pre>
<p>I want to convert ‘tmp’ into torch.Tensor type.<br>
But, when I run this code below, an error occurs.</p>
<pre><code class="lang-auto">torch.Tensor(tmp)
&gt;&gt; ValueError: only one element tensors can be converted to Python scalars

</code></pre>
<p>How can I fix this?</p>

ptrblck: <p>You could use <code>torch.cat</code> or <code>torch.stack</code> to create a tensor from the list.<br>
Let me know, if that works for you or if you expect a specific shape.</p>

anantguptadbl: <aside class="quote no-group quote-modified" data-username="111229" data-post="1" data-topic="70120">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/1/77aa72/48.png" class="avatar"> 111229:</div>
<blockquote>
<p>type(tmp) → &lt;class ‘list’&gt; type(tmp[0]) → &lt;class ‘torch.Tensor’&gt; type(tmp[0][0]) → &lt;class ‘torch.Tensor’&gt;</p>
</blockquote>
</aside>
<pre><code class="lang-auto">import torch
tmp=[torch.rand(2,4),torch.rand(2,4)]
print(type(tmp))
print(type(tmp[0]))
print(type(tmp[0][0]))

# The full tmp
print(tmp)

# After a stack, you convert the list to a dimensions
torch.stack(tmp)
</code></pre>
<p><strong>Responses</strong></p>
<pre><code class="lang-auto">&lt;class 'list'&gt;
&lt;class 'torch.Tensor'&gt;
&lt;class 'torch.Tensor'&gt;
[tensor([[0.3083, 0.6300, 0.0910, 0.1102],
        [0.4926, 0.2878, 0.5105, 0.2535]]), tensor([[0.2245, 0.6037, 0.6991, 0.1598],
        [0.8157, 0.5108, 0.3417, 0.6245]])]
tensor([[[0.3083, 0.6300, 0.0910, 0.1102],
         [0.4926, 0.2878, 0.5105, 0.2535]],

        [[0.2245, 0.6037, 0.6991, 0.1598],
         [0.8157, 0.5108, 0.3417, 0.6245]]])
</code></pre>
<p>Hope this helps</p>

111229: <p>Thanks for kind answer. But, torch.stack cannot be effective in this case because the sizes of tensors are not the same. Any solution?</p>

ptrblck: <p><a href="https://github.com/pytorch/pytorch/issues/22169">Nested tensors</a> (WIP) might be usable.<br>
Since this feature is not implemented yet, you might need to keep the list.</p>
<p>Depending on your use case, you might be able to create tensors using padding or slicing.</p>

anantguptadbl: <aside class="quote no-group" data-username="anantguptadbl" data-post="3" data-topic="70120">
<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/anantguptadbl/48/17784_2.png" class="avatar"> anantguptadbl:</div>
<blockquote>
<p>import torch tmp=[torch.rand(2,4),torch.rand(2,4)] print(type(tmp)) print(type(tmp[0])) print(type(tmp[0][0])) # The full tmp print(tmp) # After a stack, you convert the list to a dimensions torch.stack(tmp)</p>
</blockquote>
</aside>
<p><a class="mention" href="/u/ptrblck">@ptrblck</a>  I was trying something with subclasses. Can you check whether it is in the right direction</p>
<pre><code class="lang-auto">class nestedTensor(torch.Tensor):
  def __new__(cls, x, *args, **kwargs): 
    print(super(nestedTensor,cls))
    return([super(torch.Tensor,cls).__new__(cls, y, *args, **kwargs) for y in x])
      
  def __init__(self, x): 
    print("Created nestedTensor")

tmp=nestedTensor([torch.rand(2,4),torch.rand(2,3),torch.rand(4,2)])
tmp
</code></pre>

ptrblck: <p>What would be the difference of this class to a Python list?</p>

Brando_Miranda: <p>is nested tensors library/feature for things like what I speak here <a href="https://discuss.pytorch.org/t/best-way-to-convert-a-list-to-a-tensor/59949/6" class="inline-onebox">Best way to convert a list to a tensor? - #6 by Brando_Miranda</a> though I am also interested for deeper nesting where the final element is a tensor…</p>
<hr>
<p>I guess this works?</p>
<pre><code class="lang-auto"># %%

import torch

# trying to convert a list of tensors to a torch.tensor

x = torch.randn(3)
xs = [x.numpy(), x.numpy()]
# xs = torch.tensor(xs)
xs = torch.as_tensor(xs)

print(xs)
print(xs.size())

# %%

import torch

# trying to convert a list of tensors to a torch.tensor

x = torch.randn(3)
xs = [x.numpy(), x.numpy(), x.numpy()]
xs = [xs, xs]
# xs = torch.tensor(xs)
xs = torch.as_tensor(xs)

print(xs)
print(xs.size())
</code></pre>
<p>whats wrong with this solution…?</p>
<p>output:</p>
<pre><code class="lang-auto">import torch
# trying to convert a list of tensors to a torch.tensor
x = torch.randn(3)
xs = [x.numpy(), x.numpy(), x.numpy()]
xs = [xs, xs]
# xs = torch.tensor(xs)
xs = torch.as_tensor(xs)
print(xs)
print(xs.size())
tensor([[[0.3423, 1.6793, 0.0863],
         [0.3423, 1.6793, 0.0863],
         [0.3423, 1.6793, 0.0863]],
        [[0.3423, 1.6793, 0.0863],
         [0.3423, 1.6793, 0.0863],
         [0.3423, 1.6793, 0.0863]]])
torch.Size([2, 3, 3])
</code></pre>

ptrblck: <aside class="quote no-group" data-username="Brando_Miranda" data-post="8" data-topic="70120">
<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"> Brando_Miranda:</div>
<blockquote>
<p>whats wrong with this solution…?</p>
</blockquote>
</aside>
<p>I don’t see anything wrong with your approach, but as described in the other topic, you could use <code>torch.stack</code> instead of transforming the tensors to numpy arrays and call <code>torch.as_tensor</code>.</p>
<p>Nested tensors would allow you to create a tensor object containing tensors with different shapes, which doesn’t seem to be the use case you are working on.</p>

Brando_Miranda: <p>I wish that <code>torch.tensor(nested_list_of_tensors)</code> gave you the corresponding tensors with many dimensions that respected the original list of tensors. Anyway, I have a small code that might be helpful here: <a href="https://discuss.pytorch.org/t/best-way-to-convert-a-list-to-a-tensor/59949/8" class="inline-onebox">Best way to convert a list to a tensor? - #8 by Brando_Miranda</a></p>