DEV Community

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

Posted on • Updated on

Set dtype with dtype argument functions and get it in PyTorch

You can set dtype with the functions which have dtype arguments and get it with dtype and type() as shown below:

*Memos:

tensor(). *My post explains tensor():

import torch

my_tensor = torch.tensor([0, 1, 2])
my_tensor = torch.tensor([0, 1, 2], dtype=torch.int64)
my_tensor = torch.tensor([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([0., 1., 2.], dtype=torch.float64)
my_tensor = torch.tensor([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([0.+7.j, 1.+4.j, 2.+5.j], dtype=torch.complex32)

my_tensor, my_tensor.dtype, my_tensor.type()
# (tensor([0.+7.j, 1.+4.j, 2.+5.j], dtype=torch.complex32),
#  torch.complex32,
#  'torch.ComplexHalfTensor')

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

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

arange(). *My post explains arange():

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

rand(). *My post explains rand():

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

rand_like(). *My post explains rand_like():

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

sum(). *My post explains sum():

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

view(). *My post explains view():

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

Top comments (0)