DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Updated on

dim, size, item and tolist in PyTorch

Buy Me a Coffee

*My post explains how to create and acceess a tensor.

dim() can get the number of dimensions from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • dim() can be used with a tensor but not with torch.
  • ndim is the alias of dim().
import torch

my_tensor = torch.tensor(-3) # 0D tensor

my_tensor.dim() # 0

my_tensor = torch.tensor([-3, 7, -5, 2, 6, 3])
                         # 1D tensor
my_tensor.dim() # 1
my_tensor[0].dim() # 0

my_tensor = torch.tensor([[-3, 7, -5], [2, 6, 3], # 2D tensor
                          [8, 0, -1], [4, 9, -6]])
my_tensor.dim() # 2
my_tensor[0].dim() # 1
my_tensor[0][0].dim() # 0

my_tensor = torch.tensor([[[-3, 7], [-5, 2], [6, 3]], # 3D tensor
                          [[8, 0], [-1, 4], [9, -6]],
                          [[5, -2], [-7, 9], [1, 1]], 
                          [[6, -4], [0, -9], [3, 5]]])
my_tensor.dim() # 3
my_tensor[0].dim() # 2
my_tensor[0][0].dim() # 1
my_tensor[0][0][0].ndim # 0
Enter fullscreen mode Exit fullscreen mode

size() can get a size from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • size() can be used with a tensor but not with torch.
  • The 1st argument with a tensor is dim(Optional-Type:int).
  • shape is the alias of dim().
import torch

my_tensor = torch.tensor(-3) # 0D tensor

my_tensor.size()
# torch.Size([])

my_tensor = torch.tensor([-3, 7, -5, 2, 6, 3]) # 1D tensor

my_tensor.size()
# torch.Size([6])

my_tensor.size(dim=0)
my_tensor.size(dim=-1)
# 6

my_tensor[0].size()
# torch.Size([])

my_tensor = torch.tensor([[-3, 7, -5], [2, 6, 3], # 2D tensor
                          [8, 0, -1], [4, 9, -6]])
my_tensor.size()
# torch.Size([4, 3])

my_tensor.size(dim=0)
my_tensor.size(dim=-2)
# 4

my_tensor.size(dim=1)
my_tensor.size(dim=-1)
my_tensor[0].size(dim=0)
my_tensor[0].size(dim=-1)
# 3

my_tensor[0].size()
# torch.Size([3])

my_tensor[0][0].size()
# torch.Size([])

my_tensor = torch.tensor([[[-3, 7], [-5, 2], [6, 3]], # 3D tensor
                          [[8, 0], [-1, 4], [9, -6]],
                          [[5, -2], [-7, 9], [1, 1]], 
                          [[6, -4], [0, -9], [3, 5]]])
my_tensor.size()
# torch.Size([4, 3, 2])

my_tensor.size(dim=0)
my_tensor.size(dim=-3)
# 4

my_tensor.size(dim=1)
my_tensor.size(dim=-2)
my_tensor[0].size(dim=0)
my_tensor[0].size(dim=-2)
# 3

my_tensor.size(dim=2)
my_tensor.size(dim=-1)
my_tensor[0].size(dim=1)
my_tensor[0].size(dim=-1)
my_tensor[0][0].size(dim=0)
my_tensor[0][0].size(dim=-1)
# 2

my_tensor[0].size()
# torch.Size([3, 2])

my_tensor[0][0].size()
# torch.Size([2])

my_tensor[0][0][0].size()
# torch.Size([])
Enter fullscreen mode Exit fullscreen mode

item() can get a standard Python number from the 0D or more D tensor of only one element as shown below:

*Memos:

  • item() can be used with a tensor but not with torch.
  • A tensor must be a scalar.
import torch

my_tensor = torch.tensor(-3) # 0D tensor
my_tensor = torch.tensor([-3]) # 1D tensor
my_tensor = torch.tensor([[-3]]) # 2D tensor
my_tensor = torch.tensor([[[-3]]]) # 3D tensor

my_tensor.item()
# -3
Enter fullscreen mode Exit fullscreen mode

tolist() can get a standard Python number or list from the 0D or more D tensor of zero or more elements as shown below. *tolist() can be used with a tensor but not with torch:

import torch

my_tensor = torch.tensor(-3) # 0D tensor

my_tensor.tolist()
# -3

my_tensor = torch.tensor([-3, 7, -5, 2, 6, 3]) # 1D tensor

my_tensor.tolist()
# [-3, 7, -5, 2, 6, 3]

my_tensor[0].tolist()
# -3

my_tensor = torch.tensor([[-3, 7, -5], [2, 6, 3], # 2D tensor
                          [8, 0, -1], [4, 9, -6]])
my_tensor.tolist()
# [[-3, 7, -5], [2, 6, 3],
#  [8, 0, -1], [4, 9, -6]]

my_tensor[0].tolist()
# [-3, 7, -5]

my_tensor[0][0].tolist()
# -3

my_tensor = torch.tensor([[[-3, 7], [-5, 2], [6, 3]], # 3D tensor
                          [[8, 0], [-1, 4], [9, -6]],
                          [[5, -2], [-7, 9], [1, 1]], 
                          [[6, -4], [0, -9], [3, 5]]])
my_tensor.tolist()
# [[[-3, 7], [-5, 2], [6, 3]],
#  [[8, 0], [-1, 4], [9, -6]],
#  [[5, -2], [-7, 9], [1, 1]],
#  [[6, -4], [0, -9], [3, 5]]]

my_tensor[0].tolist()
# [[-3, 7], [-5, 2], [6, 3]]

my_tensor[0][0].tolist()
# [-3, 7]

my_tensor[0][0][0].tolist()
# -3
Enter fullscreen mode Exit fullscreen mode

Top comments (0)