I don't understand what squeeze
and unsqueeze
do to a tensor, even after looking at the docs and related questions.
I tried to understand it by exploring it myself in python. I first created a random tensor with
x = torch.rand(3,2,dtype=torch.float)
>>> x
tensor([[0.3703, 0.9588],
[0.8064, 0.9716],
[0.9585, 0.7860]])
But regardless of how I squeeze it, I end up with the same results:
torch.equal(x.squeeze(0), x.squeeze(1))
>>> True
If I now try to unsqueeze I get the following,
>>> x.unsqueeze(1)
tensor([[[0.3703, 0.9588]],
[[0.8064, 0.9716]],
[[0.9585, 0.7860]]])
>>> x.unsqueeze(0)
tensor([[[0.3703, 0.9588],
[0.8064, 0.9716],
[0.9585, 0.7860]]])
>>> x.unsqueeze(-1)
tensor([[[0.3703],
[0.9588]],
[[0.8064],
[0.9716]],
[[0.9585],
[0.7860]]])
However if I now create a tensor x = torch.tensor([1,2,3,4])
, and I try to unsqueeze it then it appears that 1
and -1
makes it a column where as 0
remains the same.
x.unsqueeze(0)
tensor([[1, 2, 3, 4]])
>>> x.unsqueeze(1)
tensor([[1],
[2],
[3],
[4]])
>>> x.unsqueeze(-1)
tensor([[1],
[2],
[3],
[4]])
Can someone provide an explanation of what squeeze and unsqueeze are doing to a tensor? And what's the difference between providing the arguements 0
, 1
and -1
?
In this article, we will understand how to squeeze and unsqueeze a PyTorch Tensor. To squeeze a tensor we can apply the torch. squeeze() method and to unsqueeze a tensor we use the torch. unsqueeze() method.
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.
squeeze. Returns a tensor with all the dimensions of input of size 1 removed. For example, if input is of shape: ( A × 1 × B × C × 1 × D ) (A \times 1 \times B \times C \times 1 \times D) (A×1×B×C×1×D) then the out tensor will be of shape: ( A × B × C × D ) (A \times B \times C \times D) (A×B×C×D).
unsqueeze (input, dim) → Tensor. Returns a new tensor with a dimension of size one inserted at the specified position. The returned tensor shares the same underlying data with this tensor. A dim value within the range [-input.
In this article, we will understand how to squeeze and unsqueeze a PyTorch Tensor. To squeeze a tensor we can apply the torch.squeeze () method and to unsqueeze a tensor we use the torch.unsqueeze () method.
PyTorch Unsqueeze : torch.unsqueeze () PyTorch unsqueeze function is used to generates a new tensor as output by adding a new dimension of size one at the desired position. Again in this case as well, the data and all the elements remain the same in the tensor obtained as output.
We will go through the following PyTorch functions Reshape, Squeeze, Unsqueeze, Flatten, and View along with their syntax and examples. These functions will be very useful while manipulating tensor shapes in your PyTorch deep learning projects.
To squeeze a tensor, we use the torch.squeeze () method. It returns a new tensor with all the dimensions of the input tensor but removes size 1. For example, if the shape of the input tensor is (M ☓ 1 ☓ N ☓ 1 ☓ P), then the squeezed tensor will have the shape (M ☓ M ☓ P). To unsqueeze a tensor, we use the torch.unsqueeze () method.
Here is a visual representation of what squeeze
/unsqueeze
do for an effectively 2d matrix:
When you are unsqueezing a tensor, it is ambiguous which dimension you wish to 'unsqueeze' it across (as a row or column etc). The dim
argument dictates this - i.e. position of the new dimension to be added.
Hence the resulting unsqueezed tensors have the same information, but the indices used to access them are different.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With