DEV Community

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

Posted on • Updated on

Type conversion with type(), to() and a tensor in PyTorch

Buy Me a Coffee

*My post explains how to create and acceess a tensor.

type(), to() or a tensor can do type conversion as shown below:

*Memos:

  • type() or to() can be used with a tensor but not with torch.
  • The 1st argument with a tensor is dtype(Required-Type:dtype): *Memos:
  • You can check long(), float(), cfloat() and bool() in PyTorch.
  • tensor.dtype can get dtype.
  • tensor.type() can get the legacy constructor which is an old dtype.

int to float, complex and bool:

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')
Enter fullscreen mode Exit fullscreen mode

float to int, complex and bool:

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')
Enter fullscreen mode Exit fullscreen mode

complex to int, float and bool:

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')
Enter fullscreen mode Exit fullscreen mode

bool to int, float and complex:

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')
Enter fullscreen mode Exit fullscreen mode

Top comments (0)