DEV Community

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

Posted on • Edited on

1

set_default_dtype, set_default_device and set_printoptions in PyTorch

Buy Me a Coffee

*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 is d(Required-Type:dtype). *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 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
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.
  • The 1st argument with torch is device(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 or privateuseone can be set to device.
  • Setting 0 to device uses cuda(GPU). *The number must be zero or positive.
  • My post explains device().
  • The effect of set_default_device() lasts until set_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)
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 with torch is precision(Optional-Default:4-Type:int). *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

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more