DEV Community

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

Posted on • Updated on

is_tensor(), numel() and device() in PyTorch

*Memos:

is_tensor() can check if an object is a PyTorch tensor as shown below:

*Memos:

  • is_tensor() can be used with torch but not with a tensor.
  • The 1st argument(object) with torch is obj(Required).
import torch
import numpy as np

pytorch_tensor = torch.tensor([0, 1, 2])

torch.is_tensor(obj=pytorch_tensor) # True

numpy_tensor = np.array([0., 1., 2.])

torch.is_tensor(obj=numpy_tensor) # False

torch.is_tensor(obj=7) # False

torch.is_tensor(obj=7.) # False

torch.is_tensor(obj=7.+0.j) # False

torch.is_tensor(obj=True) # False

torch.is_tensor(obj='Hello') # False
Enter fullscreen mode Exit fullscreen mode

numel() can get the total number of the elements of a 0D or more D tensor as shown below:

*Memos:

  • numel() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float, complex or bool) with torch is input(Required).
import torch

my_tensor = torch.tensor(7)

torch.numel(input=my_tensor)
my_tensor.numel()
# 1

my_tensor = torch.tensor([7, 5, 8])

torch.numel(input=my_tensor) # 3

my_tensor = torch.tensor([[7, 5, 8],
                          [3, 1, 6]])
torch.numel(input=my_tensor) # 6

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

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

my_tensor = torch.tensor([[[7.+0.j, -5.+0.j, 8.+0.j],
                           [-3.+0.j, 1.+0.j, 6.+0.j]],
                          [[0.+0.j, 9.+0.j, 2.+0.j],
                           [4.+0.j, -7.+0.j, -9.+0.j]]])
torch.numel(input=my_tensor) # 12

my_tensor = torch.tensor([[[True, False, True], [True, False, True]],
                          [[False, True, False], [False, True, False]]])
torch.numel(input=my_tensor) # 12
Enter fullscreen mode Exit fullscreen mode

device() can represent a device as shown below:

*Memos:

  • device() can be used with torch but not with a tensor.
  • The 1st argument(str, int or device()) with torch is device(Required).
  • The 1st argument(str) with torch is type(Required).
  • The 2nd argument(int) with torch is index(Optional). *It must be used with type.
  • cpu, cuda, ipu, xpu, mkldnn, opengl, opencl, ideep, hip, ve, fpga, ort, xla, lazy, vulkan, mps, meta, hpu, mtia or privateuseone can be set to device or type.
  • cuda is selected if setting 0 to device.
import torch

torch.device(device='cuda:0')
torch.device(device=0)
torch.device(type='cuda', index=0)
torch.device(device=torch.device(device='cuda:0'))
torch.device(device=torch.device(device=0))
torch.device(device=torch.device(type='cuda', index=0))
# device(type='cuda', index=0)

torch.device(device='cuda')
torch.device(type='cuda')
torch.device(device=torch.device(device='cuda'))
torch.device(device=torch.device(type='cuda'))
# device(type='cuda')
Enter fullscreen mode Exit fullscreen mode

Top comments (0)