*My post explains how to create and acceess a tensor.
type(), to() or a tensor can do type conversion as shown below:
*Memos:
-
type()
orto()
can be used with a tensor but not with torch. - The 1st argument with a tensor is
dtype
(Required-Type: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(), float() and bool() but not complex() which python built-in functions.
-
- You can check long(), float(), cfloat() and bool() in PyTorch.
-
tensor.dtype
can getdtype
. -
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')
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')
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')
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')
Top comments (0)