Set dtype with dtype argument functions and get it in PyTorch
Tags: pytorch, dtype, argument, function
Published: 2024-06-25 02:15:40

You can set `dtype` with the functions which have `dtype` arguments and get it with [dtype](https://pytorch.org/docs/stable/tensor_attributes.html#torch-dtype) and [type()](https://pytorch.org/docs/stable/generated/torch.Tensor.type.html) as shown below:

*Memos:
- I selected some popular `dtype` argument functions such as [tensor()](https://pytorch.org/docs/stable/generated/torch.tensor.html), [arange()](https://pytorch.org/docs/stable/generated/torch.arange.html), [rand()](https://pytorch.org/docs/stable/generated/torch.rand.html), [rand_like()](https://pytorch.org/docs/stable/generated/torch.rand_like.html), [sum()](https://pytorch.org/docs/stable/generated/torch.sum.html) and [view()](https://pytorch.org/docs/stable/generated/torch.Tensor.view.html).
*Memos:
- `dtype`(Optional-Type:[dtype](https://pytorch.org/docs/stable/tensor_attributes.html#torch.dtype)).
- Basically, if `dtype` is not given, `dtype` is inferred from data or `dtype` of [set_default_dtype()](https://pytorch.org/docs/stable/generated/torch.set_default_dtype.html) is used for floating-point numbers.
- `dtype` can also accept [int()](https://docs.python.org/3/library/functions.html#int), [float()](https://docs.python.org/3/library/functions.html#float) and [bool()](https://docs.python.org/3/library/functions.html#bool) but not [complex()](https://docs.python.org/3/library/functions.html#complex) which are [python built-in functions](https://docs.python.org/3/library/functions.html).
- Basically, `dtype=` is needed.
- `dtype` cannot be `int` or `float` for a `complex` tensor.

`tensor()`. *[My post](https://dev.to/hyperkai/create-a-tensor-in-pytorch-127g) explains `tensor()`:

```python
import torch

my_tensor = torch.tensor(data=[0, 1, 2], dtype=torch.float64)
my_tensor = torch.tensor(data=[0, 1, 2], dtype=float)

my_tensor, my_tensor.dtype, my_tensor.type()
# (tensor([0., 1., 2.],
#  dtype=torch.float64), torch.float64,
#  'torch.DoubleTensor')

my_tensor = torch.tensor(data=[0, 1, 2], dtype=torch.complex64)

my_tensor, my_tensor.dtype, my_tensor.type()
# (tensor([0.+0.j, 1.+0.j, 2.+0.j]),
#  torch.complex64,
#  'torch.ComplexFloatTensor')

my_tensor = torch.tensor(data=[0, 1, 2], dtype=torch.bool)
my_tensor = torch.tensor(data=[0, 1, 2], dtype=bool)

my_tensor, my_tensor.dtype, my_tensor.type()
# (tensor([False, True, True]), torch.bool, 'torch.BoolTensor')

my_tensor = torch.tensor(data=[0., 1., 2.], dtype=torch.int64)
my_tensor = torch.tensor(data=[0., 1., 2.], dtype=int)

my_tensor, my_tensor.dtype, my_tensor.type()
# (tensor([0, 1, 2]), torch.int64, 'torch.LongTensor')

my_tensor = torch.tensor(data=[0., 1., 2.], dtype=torch.complex64)

my_tensor, my_tensor.dtype, my_tensor.type()
# (tensor([0.+0.j, 1.+0.j, 2.+0.j]),
#  torch.complex64,
#  'torch.ComplexFloatTensor')

my_tensor = torch.tensor(data=[0., 1., 2.], dtype=torch.bool)
my_tensor = torch.tensor(data=[0., 1., 2.], dtype=bool)

my_tensor, my_tensor.dtype, my_tensor.type()
# (tensor([False, True, True]), torch.bool, 'torch.BoolTensor')

my_tensor = torch.tensor(data=[0.+0.j, 0.+7.j, 2.+0.j], dtype=torch.bool)
my_tensor = torch.tensor(data=[0.+0.j, 0.+7.j, 2.+0.j], dtype=bool)

my_tensor, my_tensor.dtype, my_tensor.type()
# (tensor([False, True, True]), torch.bool, 'torch.BoolTensor')

my_tensor = torch.tensor(data=[True, False, True], dtype=torch.int64)
my_tensor = torch.tensor(data=[True, False, True], dtype=int)

my_tensor, my_tensor.dtype, my_tensor.type()
# (tensor([1, 0, 1]), torch.int64, 'torch.LongTensor')

my_tensor = torch.tensor(data=[True, False, True], dtype=torch.float64)
my_tensor = torch.tensor(data=[True, False, True], dtype=float)

my_tensor, my_tensor.dtype, my_tensor.type()
# (tensor([1., 0., 1.], dtype=torch.float64),
#  torch.float64,
#  'torch.DoubleTensor')

my_tensor = torch.tensor(data=[True, False, True], dtype=torch.complex64)

my_tensor, my_tensor.dtype, my_tensor.type()
# (tensor([1.+0.j, 0.+0.j, 1.+0.j]),
#  torch.complex64,
#  'torch.ComplexFloatTensor')
```

`arange()`. *[My post](https://dev.to/hyperkai/arange-linspace-logspace-and-normal-in-pytorch-a87) explains `arange()`:

```python
import torch

my_tensor = torch.arange(start=5, end=15, step=3, dtype=torch.float64)

my_tensor, my_tensor.dtype, my_tensor.type()
# (tensor([5., 8., 11., 14.], dtype=torch.float64),
#  torch.float64,
#  'torch.DoubleTensor')
```

`rand()`. *[My post](https://dev.to/hyperkai/rand-randlike-randn-randnlike-randint-and-randperm-in-pytorch-31nc) explains `rand()`:

```python
import torch

my_tensor = torch.rand(size=(3,), dtype=torch.float64)

my_tensor, my_tensor.dtype, my_tensor.type()
# (tensor([0.4620, 0.6369, 0.5189], dtype=torch.float64),
#  torch.float64,
#  'torch.DoubleTensor')
```

`rand_like()`. *[My post](https://dev.to/hyperkai/rand-randlike-randn-randnlike-randint-and-randperm-in-pytorch-31nc) explains `rand_like()`:

```python
import torch

my_tensor = torch.rand_like(input=torch.tensor([7., 4., 5.]), 
                            dtype=torch.float64)
my_tensor, my_tensor.dtype, my_tensor.type()
# (tensor([0.7677, 0.2914, 0.3266], dtype=torch.float64),
#  torch.float64,
#  'torch.DoubleTensor')
```

`sum()`. *[My post](https://dev.to/hyperkai/sum-prod-and-cartesianprod-in-pytorch-22li) explains `sum()`:

```python
import torch

my_tensor = torch.sum(input=torch.tensor([0., 1., 2., 3.]),
                      dtype=torch.float64)
my_tensor, my_tensor.dtype, my_tensor.type()
# (tensor(6., dtype=torch.float64), torch.float64, 'torch.DoubleTensor')
```

`view()`. *[My post](https://dev.to/hyperkai/transpose-adjoint-mt-mh-view-reshape-in-pytorch-3fcp) explains `view()`:

```python
import torch

my_tensor1 = torch.tensor([0., 1., 2.]).view(size=(3, 1))

my_tensor2 = my_tensor.view(dtype=torch.bool)

my_tensor1, my_tensor2, my_tensor.dtype, my_tensor.type()
# (tensor([[0.],
#          [1.],
#          [2.]]),
#  tensor([[False, False, False, False],
#          [False, False,  True,  True],
#          [False, False, False,  True]]),
#  torch.bool,
#  'torch.BoolTensor')
```