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]])
unsqueeze is a method to change the tensor dimensions, such that operations such as tensor multiplication can be possible. This basically alters the dimension to produce a tensor that has a different dimension.
For example: If you want to multiply your tensor of size(4), with a tensor that has the size (4, N, N) then you'll get an error. But using the unsqueeze method, you can convert the tensor to size (4,1,1). Now since this has an operand of size 1, you'll be able to multiply both the tensors.
Unsqueeze
turns an n.d. tensor into an (n+1).d. 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.
e.g. unsqueeze
can be applied to a 2d tensor three different ways:
The resulting unsqueezed tensors have the same information, but the indices used to access them are different.
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: