What is the difference between view() and unsqueeze()?

username: wasiahmad
cooked: <p>Use of <code>unsqueeze()</code>:</p>
<pre><code>input = torch.Tensor(2, 4, 3) # input: 2 x 4 x 3
print(input.unsqueeze(0).size()) # prints - torch.size([1, 2, 4, 3])
</code></pre>
<p>Use of <code>view()</code>:</p>
<pre><code>input = torch.Tensor(2, 4, 3) # input: 2 x 4 x 3
print(input.view(1, -1, -1, -1).size()) # prints - torch.size([1, 2, 4, 3])
</code></pre>
<p>According to documentation, <code>unsqueeze()</code> inserts singleton dim at position given as parameter and view() creates a view with different dimensions of the storage associated with <code>tensor</code>.</p>
<p>What <code>view()</code> does is clear to me but I am unable to distinguish it from <code>unsqueeze()</code>. Moreover, I am not understanding when to use <code>view()</code> and when to use <code>unsqueeze()</code>?</p>
<p>Any help with good explanation would be appreciated!</p>

username: pranav
cooked: <p>You cannot use the <code>view</code> function as you have written it - only one of the missing dimensions can be inferred (not more than one as you have written). This means that when adding a new axis to a tensor using <code>view</code> you have to specify all the other dimensions manually (except maybe 1). <code>squeeze</code> &amp; <code>unsqueeze</code> pair of functions are utilities that make this very convenient, wherein we just specify where we want add or remove an axis.</p>
<p>Also, in the latest versions of PyTorch you can add a new axis by indexing with None as:</p>
<pre><code class="lang-python">&gt;&gt;&gt; input = torch.Tensor(2, 4, 3) # input: 2 x 4 x 3
&gt;&gt;&gt; print(input[None].size())
torch.Size([1, 2, 4, 3])
&gt;&gt;&gt; print(input[:, None].size())
torch.Size([2, 1, 4, 3])
</code></pre>

username: apaszke
cooked: <p>Or you could use <code>expand</code></p>

username: ecolss
cooked: <p><code>expand</code> seems more like repeating the elements, not adding a new dim/axis?</p>

username: apaszke
cooked: <p>Yeah, but it works too. You can repeat a tensor along new dimensions as well.</p>

username: nicoliKim
cooked: <p>What about using <code>resize_</code> or <code>unsqueeze</code>?<br>
Is there a difference between those two?</p>
<p>If a have a tensor s=(300) and I want it to be (1,1,300)<br>
Shall I use s.resize_(1,1,300) instead of using unsqueeze multiple times?<br>
Does this give the same result?</p>

username: alexis-jacq
cooked: <aside class="quote no-group" data-username="nicoliKim" data-post="6" data-topic="1155">
<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/nicolikim/48/3814_2.png" class="avatar"> nicoliKim:</div>
<blockquote>
<p>If a have a tensor s=(300) and I want it to be (1,1,300)</p>
</blockquote>
</aside>
<p><code>s.view(1,1,-1)</code> is the best way in that case.</p>

username: nicoliKim
cooked: <p>and what is the difference between s.view and s.resize_ in this case?</p>
<p><em>Kim Nicoli</em></p>

username: Wei
cooked: <p>In pytorch 0.4, I get the same result for the input</p>
<p>input = torch.Tensor(2, 4, 3)</p>
<p>with<br>
print(input.view(1, 2, 4, 3).size())</p>
<p>and</p>
<p>print(input.unsqueeze(0).size())</p>
<p>There are no difference between unsequeeze() and view(), if the both are used correctly.<br>
They do not change the data storage in tensor (.storage());<br>
they have the same id as well ( print(id(input.unsqueeze(0).storage)) ).</p>
<p>It seems to me that the two (unsqueeze, view) change only the representation of a tensor.</p>

username: mimoralea
cooked: <p>How can you <code>unsqueeze</code> a 2+d tensor at once?</p>
<pre><code class="lang-auto">In [1]: import torch                                                                                                                                                                                                                           

In [2]: im = torch.Tensor(40,40)                                                                                                                                                                                                               

In [3]: im.size()                                                                                                                                                                                                                              
Out[3]: torch.Size([40, 40])

In [4]: im.view(1,1,-1).size()                                                                                                                                                                                                                 
Out[4]: torch.Size([1, 1, 1600])
</code></pre>
<p><code>im.view(1,1,-1,-1)</code> throws an error. Best I came up with is <code>im.view((1,1) + im.size()).size()</code>, but that just looks ugly.</p>

username: iacob
cooked: <p><strong>What is <code>view()</code>?</strong></p>
<p><code>view()</code> takes a tensor and reshapes it. A requirement being that the product of the lengths of each dimension in the new shape equals that of the original. Hence a tensor with shape (4,3) can be reshaped with <code>view</code> to one of shape:</p>
<ul>
<li>(1,12), (2,6), (3,4), (6,2), (12,1)</li>
</ul>
<p>but also, any number of superficial dimensions of length 1 can be removed (i.e. <code>view(12)</code>), or added (e.g. (2,6,1), (3,1,1,4), (1,4,1,3,1) etc).</p>
<p><code>squeeze</code> and <code>unsqueeze</code> are convenient synonyms for these latter two special cases where that is the only change in shape.</p>
<p>Likewise <code>flatten</code> is a <a href="https://discuss.pytorch.org/t/what-is-the-difference-of-flatten-and-view-1-in-pytorch/51790/4">convenient synonym</a> for <code>view(-1)</code>.</p>
<p><strong>When to use <code>unsqueeze()</code>?</strong></p>
<p>Use <code>view</code> (or <code>reshape</code>) when you want to generically reshape a tensor.</p>
<p>If you want to specifically add a superficial dimension (e.g. for treating a single element like a batch, or to concatenate with another tensor), <code>unsqueeze</code> is a more convenient (and explicit) synonym, but the underlying operation is the same.</p>
<hr>
<p><strong>Note:</strong> <code>view(1, -1, -1, -1)</code> will not work (-1 can only be used once to infer one dimension’s size). What you want to do can be achieved with <code>view(1, *input.shape)</code>.</p>