*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
isobj
(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
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 withtorch
or a tensor. - The 1st argument(
input
) withtorch
or a tensor(Required-Type:tensor
ofint
,float
,complex
orbool
).
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
device() can represent a device as shown below:
*Memos:
-
device()
can be used withtorch
but not with a tensor. - The 1st argument with
torch
isdevice
(Required-Type:str
,int
or device()). - The 1st argument with
torch
istype
(Required-Type:str
). - The 2nd argument with
torch
isindex
(Optional-Type:int
). *index
must be used withtype
. -
cpu
,cuda
,ipu
,xpu
,mkldnn
,opengl
,opencl
,ideep
,hip
,ve
,fpga
,ort
,xla
,lazy
,vulkan
,mps
,meta
,hpu
,mtia
orprivateuseone
can be set todevice
ortype
. - 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')
Top comments (0)