*Memos:
- My post explains how to check PyTorch version, CPU and GPU(CUDA).
- My post explains how to create a tensor.
- My post explains how to access a tensor.
- My post explains type conversion with type(), to() and a tensor.
- My post explains type promotion, result_type(), promote_types() and can_cast().
- My post explains device conversion, from_numpy() and numpy().
- My post explains set_default_dtype(), set_default_device() and set_printoptions().
- My post explains manual_seed(), initial_seed() and seed().
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
) withtorch
isobj
(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
numel() can get the total number of the elements of a 0D or more D tensor as shown below:
*Memos:
-
numel()
can be used withtorch
or a tensor. - The 1st argument(
tensor
ofint
,float
,complex
orbool
) withtorch
isinput
(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
device() can represent a device as shown below:
*Memos:
-
device()
can be used withtorch
but not with a tensor. - The 1st argument(
str
,int
or device()) withtorch
isdevice
(Required). - The 1st argument(
str
) withtorch
istype
(Required). - The 2nd argument(
int
) withtorch
isindex
(Optional). *It 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
. -
cuda
is selected if setting0
todevice
.
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')
Top comments (0)