DEV Community

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

Posted on • Edited on

is_tensor, numel and device in PyTorch

Buy Me a Coffee

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

is_tensor() can check if an object is a PyTorch tensor, getting the scalar of a boolean value as shown below:

*Memos:

  • is_tensor() can be used with torch but not with a tensor.
  • The 1st argument with torch is obj(Required-Type:object).
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 scalar of the total number of the elements from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • numel() can be used with torch or a tensor.
  • The 1st argument(input) with torch or a tensor(Required-Type:tensor of int, float, complex or bool).
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 with torch is device(Required-Type:str, int or device()).
  • The 1st argument with torch is type(Required-Type:str).
  • The 2nd argument with torch is index(Optional-Type:int). *index 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.
  • Setting 0 to device uses cuda(GPU). *The number must be zero or positive.
  • My post explains how to set and get device.
import torch torch.device(device='cpu') torch.device(type='cpu') # device(type='cpu')  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)