*My post explains how to create and acceess a tensor.
set_default_dtype() can set the default dtype of a 0D or more D tensor as shown below:
*Memos:
-
set_default_dtype()
can be used with torch but not with a tensor. - The 1st argument with
torch
isd
(Required-Type:dtype). *Only floating-point types can be set. - The effect of
set_default_dtype()
lasts untilset_default_dtype()
is used next time. - You can also use set_default_tensor_type() but it is deprecated.
- get_default_dtype() can return the default dtype of a 0D or more D tensor.
import torch
my_tensor = torch.tensor([0., 1., 2.])
my_tensor.dtype
torch.get_default_dtype()
# torch.float32
torch.set_default_dtype(d=torch.float64)
my_tensor.device
torch.get_default_device()
# torch.float64
set_default_device() can set the default device of a 0D or more D tensor as shown below:
*Memos:
-
set_default_device()
can be used withtorch
but not with a tensor. - The 1st argument with
torch
isdevice
(Required-Type:str
,int
or device()). -
cpu
,cuda
,ipu
,xpu
,mkldnn
,opengl
,opencl
,ideep
,hip
,ve
,fpga
,ort
,xla
,lazy
,vulkan
,mps
,meta
,hpu
,mtia
orprivateuseone
can be set todevice
. - Setting
0
todevice
usescuda
(GPU). *The number must be zero or positive. -
My post explains
device()
. - The effect of
set_default_device()
lasts untilset_default_device()
is used next time. - get_default_device() can return the default device of a 0D or more D tensor.
import torch
my_tensor = torch.tensor([0, 1, 2])
my_tensor.device
torch.get_default_device()
# device(type='cpu')
torch.set_default_device(device='cuda:0')
torch.set_default_device(device='cuda')
torch.set_default_device(device=0)
torch.set_default_device(device=torch.device(device='cuda:0'))
torch.set_default_device(device='cuda')
torch.set_default_device(device=0)
torch.set_default_device(device=torch.device(type='cuda', index=0))
torch.set_default_device(device=torch.device(type='cuda'))
my_tensor.device
torch.get_default_device()
# device(type='cuda', index=0)
set_printoptions() can set the default precision of the zero or more floating-point numbers and complex numbers of a 0D or more D tensor as shown below:
*Memos:
-
set_printoptions()
can be used withtorch
but not with a tensor. - The 1st argument with
torch
isprecision
(Optional-Default:4
-Type:int
). *It must be zero or a positive number. - The effect of
set_printoptions()
lasts untilset_printoptions()
is used next time.
import torch
torch.set_printoptions()
torch.set_printoptions(precision=4)
torch.tensor([-3.635251, 7.270649, -5.164872])
# tensor([-3.6353, 7.2706, -5.1649])
torch.tensor([-3.635251+4.634852j,
7.270649+2.586449j,
-5.164872-3.450984j])
# tensor([-3.6353+4.6349j,
# 7.2706+2.5864j,
# -5.1649-3.4510j])
torch.rand(3)
# tensor([0.4249, 0.7562, 0.5942])
torch.rand(3, dtype=torch.complex64)
# tensor([0.9534+0.6484j, 0.1216+0.3275j, 0.8730+0.9752j])
torch.set_printoptions(precision=2)
torch.tensor([-3.635251, 7.270649, -5.164872])
# tensor([-3.64, 7.27, -5.16])
torch.tensor([-3.635251+4.634852j,
7.270649+2.586449j,
-5.164872-3.450984j])
# tensor([-3.64+4.63j, 7.27+2.59j, -5.16-3.45j])
torch.rand(3)
# tensor([0.95, 0.86, 0.32])
torch.rand(3, dtype=torch.complex64)
# tensor([0.28+0.95j, 0.27+0.75j, 0.78+0.45j])
torch.set_printoptions(precision=8)
torch.tensor([-3.635251, 7.270649, -5.164872])
# tensor([-3.63525105, 7.27064896, -5.16487217])
torch.tensor([-3.635251+4.634852j,
7.270649+2.586449j,
-5.164872-3.450984j])
# tensor([-3.63525105+4.63485193j,
# 7.27064896+2.58644891j,
# -5.16487217-3.45098400j])
torch.rand(3)
# tensor([0.12433541, 0.90939915, 0.81334412])
torch.rand(3, dtype=torch.complex64)
# tensor([0.23186535+0.95299882j,
# 0.97718322+0.48021430j,
# 0.73880774+0.09643537j])
Top comments (0)