What is the difference between view() and unsqueeze()?
<SCODE>input = torch.Tensor(2, 4, 3) # input: 2 x 4 x 3
print(input.unsqueeze(0).size()) # prints - torch.size([1, 2, 4, 3])
<ECODE> <SCODE>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])
<ECODE> Any help with good explanation would be appreciated!
wasiahmad
Also, in the latest versions of PyTorch you can add a new axis by indexing with None as: <SCODE>>>> input = torch.Tensor(2, 4, 3) # input: 2 x 4 x 3
>>> print(input[None].size())
torch.Size([1, 2, 4, 3])
>>> print(input[:, None].size())
torch.Size([2, 1, 4, 3])
<ECODE>
pranav
apaszke
ecolss
Yeah, but it works too. You can repeat a tensor along new dimensions as well.
apaszke
nicoliKim
alexis-jacq
In pytorch 0.4, I get the same result for the input input = torch.Tensor(2, 4, 3) and print(input.unsqueeze(0).size()) It seems to me that the two (unsqueeze, view) change only the representation of a tensor.
Wei
<SCODE>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])
<ECODE>
mimoralea
(1,12), (2,6), (3,4), (6,2), (12,1)
iacob