DEV Community

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

Posted on • Updated on

set_default_dtype(), set_default_device() and set_printoptions() in PyTorch

*Memos:

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 but dtype can be used with a tensor.
  • The 1st argument(torch.dtype()) with torch is d(Required). *Only floating-point types can be set.
  • The effect of set_default_dtype() lasts until set_default_dtype() is used next time.
  • You can also use set_default_tensor_type() but it is deprecated.
  • get_default_dtype() can get 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
Enter fullscreen mode Exit fullscreen mode

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 with torch but not with a tensor but device can be used with a tensor.
  • The 1st argument(str, int or device()) with torch is device(Required).
  • 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.
  • cuda is selected if setting 0 to device.
  • The effect of set_default_device() lasts until set_default_device() is used next time.
  • get_default_device() can get 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')

my_tensor.device
torch.get_default_device()
# device(type='cuda', index=0)
Enter fullscreen mode Exit fullscreen mode

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 with torch but not with a tensor.
  • The 1st argument(int) with torch is precision(Optional-Default:4). *It must be zero or a positive number.
  • The effect of set_printoptions() lasts until set_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])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)