logo

Handling the Dimensions and Sizes of PyTorch Tensors 📂Machine Learning

Handling the Dimensions and Sizes of PyTorch Tensors

Definition

Let’s call AA a PyTorch tensor.

  • The following pair (a0,a1,,an1)(a_{0}, a_{1}, \dots, a_{n-1}) is called the size of AA.

    A.size() = torch.Size([a0,a1,,an1]) \text{A.size() = torch.Size}([a_{0}, a_{1}, \dots, a_{n-1} ])

  • Let’s refer to i=0n1ai=a0×a1×an1\prod \limits_{i=0}^{n-1} a_{i} = a_{0} \times a_{1} \times \cdots a_{n-1} as the dimension of AA.

  • Call AA a nn-dimensional tensor.

    aia_{i} are the sizes of the respective iith dimensions, which are integers greater than 11. Since this is Python, note that it starts from the 00th dimension.

>>> A = torch.ones(2,3,4)
tensor([[[1., 1., 1., 1.],
         [1., 1., 1., 1.],
         [1., 1., 1., 1.]],

        [[1., 1., 1., 1.],
         [1., 1., 1., 1.],
         [1., 1., 1., 1.]]])

For example, the dimension of such a tensor AA is 33, its size is 24=23424=2 \cdot 3 \cdot 4, and its dimension is (2,3,4)(2,3,4).

.dim(), .ndim

Returns the dimension of the tensor.

>>> A.dim()
3

>>> A.ndim
3

.shape, .size()

Returns the size of the tensor.

>>> A.shape
torch.Size([2, 3, 4])
>>> A.shape[1]
3

>>> A.size()
torch.Size([2, 3, 4])
>>> A.size(2)
4

.view(), .reshape()

Changes the size of the tensor while keeping its dimension.

If you use 1-1 as an argument, the size is adjusted automatically. For instance, as in the following example, changing a tensor of size (2,3,4)(2,3,4) with .view(4,-1) changes its size to (4,6)(4,6).

>>> A.reshape(8,3)
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])

>>> A.view(3,-1)
tensor([[1., 1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1., 1.]])

>>> A.view(-1,4)
tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]])