Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

tensor([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]])
torch.Size([2, 5])

Tensor.resize_

Notice that Tensor.resize_ is in-place and the non in-place version Tensor.resize is deprecated.

tensor([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]])
False
tensor([[[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]]])
torch.Size([1, 2, 5])

You can resize a Tensor to have more elements. The Tensor is padded on the right.

  • A float Tensor is padded with the value 0.

  • It seems that an int Tensor is padded with random integers.

Resize an int Tensor.

tensor([1, 2, 3])
tensor([ 1, 2, 3, 112, 0])

Resize a float Tensor.

tensor([1., 2., 3.])
tensor([1.0000e+00, 2.0000e+00, 3.0000e+00, 4.5862e-41, 8.9683e-44])

Tensor.squeeze

tensor([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]])
torch.Size([2, 5])
tensor([[[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]]])
torch.Size([1, 2, 5])
tensor([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]])
torch.Size([2, 5])

Tensor.unsqueeze

tensor([[[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]]])
torch.Size([1, 2, 5])

Tensor.view

tensor([[[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]]])
torch.Size([1, 2, 5])

Tensor.view_as

View this tensor as the same size as other. self.view_as(other) is equivalent to self.view(other.size()).

Tensor.expand

Returns a new view of the self tensor with singleton dimensions expanded to a larger size.

Tensor.expand is used to replicate data in a tensor. If x is the tensor to be expanded. The new shape must be (k, *x.shape), where k is a non-negative integer.

tensor([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]])
tensor([[[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]], [[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]], [[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]]])
tensor([[[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]]])
torch.Size([1, 2, 5])
tensor([1, 2, 3])
tensor([1, 2, 3, 1, 2, 3])
tensor([[[1, 2, 3, 1, 2, 3, 1, 2, 3], [1, 2, 3, 1, 2, 3, 1, 2, 3], [1, 2, 3, 1, 2, 3, 1, 2, 3], [1, 2, 3, 1, 2, 3, 1, 2, 3]], [[1, 2, 3, 1, 2, 3, 1, 2, 3], [1, 2, 3, 1, 2, 3, 1, 2, 3], [1, 2, 3, 1, 2, 3, 1, 2, 3], [1, 2, 3, 1, 2, 3, 1, 2, 3]]])

Add a Dummy Dimension

If you need to add a dummy dimension at the beginning or the end, it is the easiest to use the method Tensor.unsqueeze since you only need to specify the position to add the dummy dimension instead of specifying the full new dimension. Otherwise, I’d suggest you to use the method Tensor.view as it can be used for both adding/removing a dummy dimension (at the slight inconvenience of specifying the full new dimension).

tensor([[[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]]])
tensor([[[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]]])
tensor([[[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]]])
tensor([[[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]]])

You can also use numpy-style slicing to add a dummy dimenson!!!

tensor([[[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]]])

Remove a Dummy Dimension

If you need to remove a dummy dimension from the beginning or the end, it is the easiest to use the method Tensor.squeeze since you only need to specify the position to remove the dummy dimension instead of specifying the full new dimension. Otherwise, I’d suggest you to use the method Tensor.view as it can be used for both adding/removing a dummy dimension (at the slight inconvenience of specifying the full new dimension).

tensor([[[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]]])
torch.Size([1, 2, 5])
tensor([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]])
tensor([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]])
tensor([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]])