Torch Unsqueeze not changing size

torch.unsqueeze seems to add a batch dimension here but why doesn’t tensor.size() change?

>>> x = torch.Tensor([1,2,3,4])

>>> x

tensor([1., 2., 3., 4.])

>>> x.size()

torch.Size([4])

>>> torch.unsqueeze(x,0)

tensor([[1., 2., 3., 4.]])

>>> x.size()

torch.Size([4])

torch.unsqueeze is not an inplace operation so either assign the result to a tensor or use the inplace version as tensor.unsqueeze_().

1 Like