The documentation of torch.squeeze (PyTorch 0.4) states that “As an exception to the above, a 1-dimensional tensor of size 1 will not have its dimensions changed.”
However, when I torch.squeeze() a 1-dimensional tensor, it becomes zero-dimensional: i.e. it does get squeezed!
>>> import torch
>>> torch.__version__
'0.4.0'
>>> a = torch.tensor([512])
>>> a.shape
torch.Size([1])
>>> a = a.squeeze()
>>> a.shape
torch.Size([])
Is this an implementation bug, a documentation bug, or am I the bug?
This is expected. Now the notion of 0-dimensional tensor exists and represent a tensor containing a single element. You can see it as a number.
For example, if you call .sum() on a tensor, you will see that you get a 0-dimensionnal tensor as output.
Thanks, but I was referring to the documentation being misaligned with the behavior.
The docs say that torch.squeeze() “Returns a tensor with all the dimensions of input of size 1 removed… As an exception to the above, a 1-dimensional tensor of size 1 will not have its dimensions changed," but I show that I create a 1-dim tensor and squeeze() does change the tensor’s shape.
What am I missing?
Note that I intentionally created a 1-dim tensor with 1 element in it: