dim(), size(), item() and tolist() in PyTorch
Tags: pytorch, dim, size, item
Published: 2024-05-15 04:00:41

[dim()](https://pytorch.org/docs/stable/generated/torch.Tensor.dim.html) 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](https://pytorch.org/docs/stable/torch.html).
- [ndim](https://pytorch.org/docs/stable/generated/torch.Tensor.ndim.html) is the alias of `dim()`.

```python
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
```

[size()](https://pytorch.org/docs/stable/generated/torch.Tensor.size.html) 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](https://pytorch.org/docs/stable/generated/torch.Tensor.shape.html) is the alias of `dim()`.

```python
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([])
```

[item()](https://pytorch.org/docs/stable/generated/torch.Tensor.item.html) 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.

```python
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
```

[tolist()](https://pytorch.org/docs/stable/generated/torch.Tensor.tolist.html) 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`:

```python
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
```