set_default_dtype(), set_default_device() and set_printoptions() in PyTorch
Tags: pytorch, setdefaultdtype, setdefaultdevice, setprintoptions
Published: 2024-05-24 22:26:30

*Memos:
- [My post](https://dev.to/hyperkai/check-pytorch-version-cpu-and-gpucuda-in-pytorch-6jk) explains how to check PyTorch version, CPU and GPU(CUDA).
- [My post](https://dev.to/hyperkai/create-a-tensor-in-pytorch-127g) explains how to create a tensor.
- [My post](https://dev.to/hyperkai/access-a-tensor-in-pytorch-1f4e) explains how to access a tensor.
- [My post](https://dev.to/hyperkai/istensor-numel-and-device-in-pytorch-2eha) explains [is_tensor()](https://pytorch.org/docs/stable/generated/torch.is_tensor.html), [numel()](https://pytorch.org/docs/stable/generated/torch.numel.html) and [device()](https://pytorch.org/docs/stable/tensor_attributes.html#torch-device).
- [My post](https://dev.to/hyperkai/type-conversion-with-type-to-and-a-tensor-in-pytorch-2a0g) explains type conversion with [type()](https://pytorch.org/docs/stable/generated/torch.Tensor.type.html), [to()](https://pytorch.org/docs/stable/generated/torch.Tensor.to.html) and a tensor.
- [My post](https://dev.to/hyperkai/type-promotion-resulttype-promotetypes-and-cancast-in-pytorch-33p8) explains type promotion, [result_type()](https://pytorch.org/docs/stable/generated/torch.result_type.html), [promote_types()](https://pytorch.org/docs/stable/generated/torch.promote_types.html) and [can_cast()](https://pytorch.org/docs/stable/generated/torch.can_cast.html).
- [My post](https://dev.to/hyperkai/device-conversion-fromnumpy-and-numpy-in-pytorch-1iih) explains device conversion, [from_numpy()](https://pytorch.org/docs/stable/generated/torch.from_numpy.html) and [numpy()](https://pytorch.org/docs/stable/generated/torch.Tensor.numpy.html).
- [My post](https://dev.to/hyperkai/manualseed-initialseed-and-seed-in-pytorch-5gm8) explains [manual_seed()](https://pytorch.org/docs/stable/generated/torch.manual_seed.html), [initial_seed()](https://pytorch.org/docs/stable/generated/torch.initial_seed.html) and [seed()](https://pytorch.org/docs/stable/generated/torch.seed.html).

[set_default_dtype()](https://pytorch.org/docs/stable/generated/torch.set_default_dtype.html) can set the default dtype of a 0D or more D tensor as shown below:

*Memos:
- `set_default_dtype()` can be used with [torch](https://pytorch.org/docs/stable/torch.html) but not with a tensor but `dtype` can be used with a tensor.
- The 1st argument with `torch` is `d`(Required-Type:[dtype](https://pytorch.org/docs/stable/tensor_attributes.html#torch.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()](https://pytorch.org/docs/stable/generated/torch.set_default_tensor_type.html) but it is deprecated.
- [get_default_dtype()](https://pytorch.org/docs/stable/generated/torch.get_default_dtype.html) can return the default dtype of a 0D or more D tensor.

```python
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()](https://pytorch.org/docs/stable/generated/torch.set_default_device.html) 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 with `torch` is `device`(Required-Type:`str`, `int` or [device()](https://pytorch.org/docs/stable/tensor_attributes.html#torch-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](https://dev.to/hyperkai/istensor-numel-and-device-in-pytorch-2eha) explains `device()`.
- The effect of `set_default_device()` lasts until `set_default_device()` is used next time.
- [get_default_device()](https://pytorch.org/docs/stable/generated/torch.get_default_device.html) can return the default device of a 0D or more D tensor.

```python
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()](https://pytorch.org/docs/stable/generated/torch.set_printoptions.html) 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.

```python
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])
```