How to reshape a list?

Nil_MSh: <p>I have act[‘a’] of torch.Size([1, 64, 32, 32]) and act[‘b’] of torch.Size([1, 128, 16, 16]) and act[‘c’] of torch.Size([1, 256, 8, 8]).<br>
and I have:</p>
<pre><code class="lang-auto">target = {'a': [], 'b': [], 'c': []}
for i in range(800):
                target['a'].append(act['a'])
                target['b'].append(act['b'])
                target['c'].append(act['c'])
</code></pre>
<p>so now the size of target[‘a’] is [800, 1, 64, 32, 32]. how can I reshape it to [1, 64, 800, 32, 32]?</p>

Ada: <p>It sounds like you want to permute the dimensions - shifting the 0th dimension to be the 2nd dimension.<br>
You could use <code>permute()</code>: <code>target['a'].permute((1,2,0,3,4))</code></p>
<p>Note that <code>permute()</code> only returns a view of the tensor, but doesn’t change the underlying shape.  If you want to keep the new shape, you’ll need to save it:</p>
<pre><code class="lang-auto">target['a'] = target['a'].permute((1,2,0,3,4))
</code></pre>
<p>Also, your code looks like <code>target['a']</code> is actually a list of tensors.  It would need to be a tensor for you to call PyTorch methods.  You can either convert it, or preallocate a tensor and then fill it in, or use something like <code>torch.cat()</code>.</p>

Nil_MSh: <p>Thank you for your answer.<br>
you mean i should use</p>
<pre><code class="lang-auto">torch.unsqueeze(target['a'], 0)
</code></pre>
<p>and then torch.cat the result?<br>
cause when I do this it gives this error:</p>
<blockquote>
<p>TypeError: unsqueeze(): argument ‘input’ (position 1) must be Tensor, not list</p>
</blockquote>
<p>what other way is there to use torch.cat for this problem?</p>

Ada: <p>So, the way you currently have it written, <code>target['a']</code> is a list, not a Tensor, as you can see in the error.  Because it’s a list, you can’t use any PyTorch methods on it.</p>
<p>Normally, if <code>target['a']</code> were a list of lists, you could just call <code>torch.tensor(target['a'])</code> to convert it, and from there, use PyTorch methods like <code>permute()</code>.  For example:</p>
<pre><code class="lang-auto">a = [1,2,3]
b = []
for i in range(3):
    b.append(a)
b = torch.tensor(b)
</code></pre>
<p>However, your <code>target['a']</code> is a list of tensors, and it seems that you can’t simply cast it to a tensor in this case.</p>
<p>That’s why I suggested either preallocating a large tensor, and then saving to each row.  Or concatenating tensors together.  According to <a href="https://discuss.pytorch.org/t/how-to-concatenate-list-of-pytorch-tensors/1350">this post</a>, you can put a list of tensors into <code>torch.cat()</code> to concatenate them together.  Meaning, you can do <code>torch.cat(target['a'])</code>.  However, this will give you a tensor of shape <code>[800,64,32,32]</code>.</p>
<p>Also mentioned in the post linked above is that <code>torch.cat()</code> concatenates into a <em>current</em> dimension, while <code>torch.stack()</code> stacks in a <em>new</em> dimension.  Therefore, if you want the shape after concatenation to be <code>[800,1,64,32,32]</code>, you should use <code>torch.stack()</code>.</p>
<p>Note, both <code>torch.cat()</code> and <code>torch.stack()</code> allow you to specify which dimension you want things concatenated / stacked in, so you may be able to avoid needing to permute afterward altogether. I.e.:</p>
<pre><code class="lang-auto">torch.stack(target['a'],dim=2)</code></pre>

Nil_MSh: <p>I used</p>
<pre><code class="lang-auto">torch.stack(target['a'],dim=2)
</code></pre>
<p>it gives this error:</p>
<blockquote>
<p>ValueError: Attempt to convert a value (tensor([[[[7.7345e-01,…]]]])  with an unsupported type (&lt;class ‘torch.Tensor’&gt;) to a Tensor.</p>
</blockquote>

Ada: <p>Interesting…  I was unable to reproduce this error with:</p>
<pre><code class="lang-auto">a = torch.rand((1,64,32,32))
b = []
for i in range(800):
    b.append(a)

c = torch.stack(b,dim=2)
</code></pre>
<p>Output <code>c</code> has shape <code>torch.Size([1,64,800,32,32])</code>.</p>
<p>Is there maybe something funky with your <code>act['a']</code>?  Is it actually a tensor?  What is the output of <code>type(act['a'])</code>?</p>

Nil_MSh: <p>&lt;class ‘torch.Tensor’&gt;</p>

Ada: <p>Sorry, I’m not sure what the problem is - it seems weird that PyTorch can’t convert a tensor into a tensor… My best guess is something strange is in <code>act['a']</code>, but it’s hard to say what exactly it could be.</p>
<p>Similar errors seem to happen <a href="https://stackoverflow.com/questions/59049360/valueerror-attempt-to-convert-a-value-tensor" rel="noopener nofollow ugc">here</a> (where the dataset was loaded in incorrectly), and <a href="https://github.com/tensorflow/tensorflow/issues/30120" rel="noopener nofollow ugc">here</a> (where there seem to have been compatibility issues), but both of those posts are with respect to tensorflow, not PyTorch, and they are only somewhat similar to your error.</p>

Nil_MSh: <p>I don’t know why this happened but I’ve had imported tensorflow but wasn’t using it. I deleted it and now the code works!! :)))  but</p>
<pre><code class="lang-auto">torch.stack(target['a'], dim=2)
print(type(target['a']))
</code></pre>
<p>and</p>
<pre><code class="lang-auto">torch.cat(target['a'])
print(type(target['a']))
</code></pre>
<p>give me</p>
<blockquote>
<p>&lt;class ‘list’&gt;</p>
</blockquote>
<p>I don’t know what is happening.</p>

Ada: <p>Yes, <code>target['a']</code> is supposed to be a list.</p>
<p>In your first post, you showed:</p>
<aside class="quote no-group" data-username="Nil_MSh" data-post="1" data-topic="130477">
<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/n/e495f1/48.png" class="avatar"> Nil_MSh:</div>
<blockquote>
<pre><code class="lang-auto">target = {'a': [], 'b': [], 'c': []}
for i in range(800):
                target['a'].append(act['a'])
                target['b'].append(act['b'])
                target['c'].append(act['c'])
</code></pre>
</blockquote>
</aside>
<p>This means you initialized <code>target['a']</code> to be a list, and then, in the loop, appended a tensor to the list at each iteration.  I.e., at the end of the loop, we should have <code>target['a'] = [act['a'],...,act['a']]</code> where <code>act['a']</code> is a tensor, and <code>target['a']</code> is now a <em>list</em> of tensors.</p>
<p>When you encountered your error above, I thought something was wrong with <code>act['a']</code> and we were checking its type, which is supposed to be a tensor.  You were just now checking the type of <code>target['a']</code>, which is supposed to be a list.</p>
<p>In summary, going off my example from above:</p>
<aside class="quote no-group" data-username="Ada" data-post="6" data-topic="130477">
<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/ada/48/32203_2.png" class="avatar"> Ada:</div>
<blockquote>
<pre><code class="lang-auto">a = torch.rand((1,64,32,32))
b = []
for i in range(800):
    b.append(a)

c = torch.stack(b,dim=2)
</code></pre>
</blockquote>
</aside>
<p>we should have <code>a</code> is a tensor, <code>b</code> is a list, and <code>c</code> is a tensor.</p>