Type conversion with type(), to() and a tensor in PyTorch
Tags: pytorch, typeconversion, type, to
Published: 2024-05-11 08:41:59

*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-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/setdefaultdtype-setdefaultdevice-and-setprintoptions-in-pytorch-55g8) explains [set_default_dtype()](https://pytorch.org/docs/stable/generated/torch.set_default_dtype.html), [set_default_device()](https://pytorch.org/docs/stable/generated/torch.set_default_device.html) and [set_printoptions()](https://pytorch.org/docs/stable/generated/torch.set_printoptions.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).

[type()](https://pytorch.org/docs/stable/generated/torch.Tensor.type.html), [to()](https://pytorch.org/docs/stable/generated/torch.Tensor.to.html) or a tensor can do type conversion as shown below:

*Memos:
- `type()` or `to()` can be used with a tensor but not with [torch](https://pytorch.org/docs/stable/torch.html).
- The 1st argument with a tensor is `dtype`(Required-Type:[dtype](https://pytorch.org/docs/stable/tensor_attributes.html#torch-dtype)):
  *Memos:
  - `type()` can also accept legacy constructors(`str`) such as `'torch.LongTensor'`, `'torch.FloatTensor'`, `'torch.ComplexFloatTensor'` and `'torch.BoolTensor'`. 
  - `to()` 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 [python built-in functions](https://docs.python.org/3/library/functions.html).
- You can check [long()](https://pytorch.org/docs/stable/generated/torch.Tensor.long.html), [float()](https://pytorch.org/docs/stable/generated/torch.Tensor.float.html), [cfloat()](https://pytorch.org/docs/stable/generated/torch.Tensor.cfloat.html) and [bool()](https://pytorch.org/docs/stable/generated/torch.Tensor.bool.html) in PyTorch.
- `tensor.dtype` can get `dtype`.
- `tensor.type()` can get a legacy constructor.

`int` to `float`, `complex` and `bool`:

```python
import torch

tensor1 = torch.tensor([7, 0, 4])

tensor1, tensor1.dtype, tensor1.dtype
# (tensor([7, 0, 4]), torch.int64, torch.int64)

tensor2 = tensor1.type(dtype=torch.float32)
tensor2 = tensor1.type(dtype=torch.float)
tensor2 = tensor1.type(dtype='torch.FloatTensor')
tensor2 = tensor1.to(dtype=torch.float32)
tensor2 = tensor1.to(dtype=torch.float)
tensor2 = tensor1.float()

tensor2, tensor2.dtype, tensor2.type()
# (tensor([7., 0., 4.]), torch.float32, 'torch.FloatTensor')

tensor2 = tensor1.to(dtype=float)

tensor2, tensor2.dtype, tensor2.type()
# (tensor([7., 0., 4.], dtype=torch.float64),
#  torch.float64,
#  'torch.DoubleTensor')

tensor2 = tensor1.type(dtype=torch.complex64)
tensor2 = tensor1.type(dtype=torch.cfloat)
tensor2 = tensor1.type(dtype='torch.ComplexFloatTensor')
tensor2 = tensor1.to(dtype=torch.complex64)
tensor2 = tensor1.to(dtype=torch.cfloat)
tensor2 = tensor1.cfloat()

tensor2, tensor2.dtype, tensor2.type()
# (tensor([7.+0.j, 0.+0.j, 4.+0.j]),
#  torch.complex64,
#  'torch.ComplexFloatTensor')

tensor2 = tensor1.type(dtype=torch.bool)
tensor2 = tensor1.type(dtype='torch.BoolTensor')
tensor2 = tensor1.to(dtype=torch.bool)
tensor2 = tensor1.to(dtype=bool)
tensor2 = tensor1.bool()

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

`float` to `int`, `complex` and `bool`:

```python
import torch

tensor1 = torch.tensor([7., 0., 4.])

tensor1, tensor1.dtype, tensor1.dtype
# (tensor([7., 0., 4.]), torch.float32, torch.float32)

tensor2 = tensor1.type(dtype=torch.int64)
tensor2 = tensor1.type(dtype=torch.long)
tensor2 = tensor1.type(dtype='torch.LongTensor')
tensor2 = tensor1.to(dtype=torch.int64)
tensor2 = tensor1.to(dtype=torch.long)
tensor2 = tensor1.to(dtype=int)
tensor2 = tensor1.long()

tensor2, tensor2.dtype, tensor2.type()
# (tensor([7, 0, 4]), torch.int64, 'torch.LongTensor')

tensor2 = tensor1.type(dtype=torch.complex64)
tensor2 = tensor1.type(dtype=torch.cfloat)
tensor2 = tensor1.type(dtype='torch.ComplexFloatTensor')
tensor2 = tensor1.to(dtype=torch.complex64)
tensor2 = tensor1.to(dtype=torch.cfloat)
tensor2 = tensor1.cfloat()

tensor2, tensor2.dtype, tensor2.type()
# (tensor([7.+0.j, 0.+0.j, 4.+0.j]),
#  torch.complex64,
#  'torch.ComplexFloatTensor')

tensor2 = tensor1.type(dtype=torch.bool)
tensor2 = tensor1.type(dtype='torch.BoolTensor')
tensor2 = tensor1.to(dtype=torch.bool)
tensor2 = tensor1.to(dtype=bool)
tensor2 = tensor1.bool()

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

`complex` to `int`, `float` and `bool`:

```python
import torch

tensor1= torch.tensor([7.+0.j, 0.+0.j, 4.+0.j])

tensor1, tensor1.dtype, tensor1.dtype
# (tensor([7.+0.j, 0.+0.j, 4.+0.j]), torch.complex64, torch.complex64)

tensor2 = tensor1.type(dtype=torch.int64)
tensor2 = tensor1.type(dtype=torch.long)
tensor2 = tensor1.type(dtype='torch.LongTensor')
tensor2 = tensor1.to(dtype=torch.int64)
tensor2 = tensor1.to(dtype=torch.long)
tensor2 = tensor1.to(dtype=int)
tensor2 = tensor1.long()

tensor2, tensor2.dtype, tensor2.type()
# (tensor([7, 0, 4]), torch.int64, 'torch.LongTensor')

tensor2 = tensor1.type(dtype=torch.float32)
tensor2 = tensor1.type(dtype=torch.float)
tensor2 = tensor1.type(dtype='torch.FloatTensor')
tensor2 = tensor1.to(dtype=torch.float32)
tensor2 = tensor1.to(dtype=torch.float)
tensor2 = tensor1.float()

tensor2, tensor2.dtype, tensor2.type()
# (tensor([7., 0., 4.]), torch.float32, 'torch.FloatTensor')

tensor2 = tensor1.to(dtype=float)

tensor2, tensor2.dtype, tensor2.type()
# (tensor([7., 0., 4.], dtype=torch.float64),
#  torch.float64,
#  'torch.DoubleTensor')

tensor2 = tensor1.type(dtype=torch.bool)
tensor2 = tensor1.type(dtype='torch.BoolTensor')
tensor2 = tensor1.to(dtype=torch.bool)
tensor2 = tensor1.to(dtype=bool)
tensor2 = tensor1.bool()

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

`bool` to `int`, `float` and `complex`:

```python
import torch

tensor1 = torch.tensor([True, False, True])

tensor1, tensor1.dtype, tensor1.dtype
# (tensor([True, False, True]), torch.bool, torch.bool)

tensor2 = tensor1.type(dtype=torch.int64)
tensor2 = tensor1.type(dtype=torch.long)
tensor2 = tensor1.type(dtype='torch.LongTensor')
tensor2 = tensor1.to(dtype=torch.int64)
tensor2 = tensor1.to(dtype=torch.long)
tensor2 = tensor1.to(dtype=int)
tensor2 = tensor1.long()

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

tensor2 = tensor1.type(dtype=torch.float32)
tensor2 = tensor1.type(dtype=torch.float)
tensor2 = tensor1.type(dtype='torch.FloatTensor')
tensor2 = tensor1.to(dtype=torch.float32)
tensor2 = tensor1.to(dtype=torch.float)
tensor2 = tensor1.float()

tensor2, tensor2.dtype, tensor2.type()
# (tensor([1., 0., 1.]), torch.float32, 'torch.FloatTensor')

tensor2 = tensor1.to(dtype=float)

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

tensor2 = tensor1.type(dtype=torch.complex64)
tensor2 = tensor1.type(dtype=torch.cfloat)
tensor2 = tensor1.type(dtype='torch.ComplexFloatTensor')
tensor2 = tensor1.to(dtype=torch.complex64)
tensor2 = tensor1.to(dtype=torch.cfloat)
tensor2 = tensor1.cfloat()

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