Question

[Solved] What does “unsqueeze” do in Pytorch?

I cannot understand how the example in the PyTorch documentation corresponds to the explanation:

Returns a new tensor with a dimension of size one inserted at the specified position. […]

>>> x = torch.tensor([1, 2, 3, 4])
>>> torch.unsqueeze(x, 0)
tensor([[ 1,  2,  3,  4]])
>>> torch.unsqueeze(x, 1)
tensor([[ 1],
        [ 2],
        [ 3],
        [ 4]])

Solution #1:

If you look at the shape of the array before and after, you see that before it was (4,) and after it is (1, 4) (when second parameter is 0) and (4, 1) (when second parameter is 1). So a 1 was inserted in the shape of the array at axis 0 or 1, depending on the value of the second parameter.

That is opposite of np.squeeze() (nomenclature borrowed from MATLAB) which removes axes of size 1 (singletons).

Respondent: norok2

Solution #2:

It indicates the position on where to add the dimension. torch.unsqueeze adds an additional dimension to the tensor. So let’s say you have a tensor of shape (3), if you add a dimension at the 0 position, it will be of shape (1,3), which means 1 row and 3 columns. If you add at the 1 position, it will be (3,1), which means 3 rows and 1 column. If you have a 2D tensor of shape (2,2) add add an extra dimension at the 0 position, this will result of the tensor having a shape of (1,2,2), which means one channel, 2 rows and 2 columns. If you add at the 1 position, it will be of shape (2,1,2), so it will have 2 channels, 1 row and 2 columns. If you add it at the 2 position, the tensor will be of shape (2,2,1), which means 2 channels, 2 rows and one column.

Respondent: Voontent

Solution #3:

I am not sure why PyTorch references are not mentioned here since this is PyTorch legacy.

torch.squeeze

enter image description here

torch.unsqueeze

enter image description here

Respondent: prosti

Solution #4:

unsqueeze turns an n-dimensionsal tensor into an n+1-dimensional one, by adding an extra dimension of depth 1. However, since it is ambiguous which axis the new dimension should lie across (i.e. in which direction it should be “unsqueezed”), this needs to be specified by the dim argument.

Hence the resulting unsqueezed tensors have the same information, but the indices used to access them are different.

Here is a visual representation of what squeeze/unsqueeze do for an effectively 2d matrix, where it is going from a 2d tensor to a 3d one, and hence there are 3 choices for the new dimension’s position:

enter image description here

Respondent: iacob

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy