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:

I selected some popular dtype argument functions such as tensor(), arange(), rand(), rand_like(), sum() and view().
*Memos:

dty…


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

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')

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')

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')

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')

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')

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')


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)


Print Share Comment Cite Upload Translate Updates
APA

Super Kai (Kazuya Ito) | Sciencx (2024-06-25T02:15:40+00:00) Set dtype with dtype argument functions and get it in PyTorch. Retrieved from https://www.scien.cx/2024/06/25/set-dtype-with-dtype-argument-functions-and-get-it-in-pytorch/

MLA
" » Set dtype with dtype argument functions and get it in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Tuesday June 25, 2024, https://www.scien.cx/2024/06/25/set-dtype-with-dtype-argument-functions-and-get-it-in-pytorch/
HARVARD
Super Kai (Kazuya Ito) | Sciencx Tuesday June 25, 2024 » Set dtype with dtype argument functions and get it in PyTorch., viewed ,<https://www.scien.cx/2024/06/25/set-dtype-with-dtype-argument-functions-and-get-it-in-pytorch/>
VANCOUVER
Super Kai (Kazuya Ito) | Sciencx - » Set dtype with dtype argument functions and get it in PyTorch. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/25/set-dtype-with-dtype-argument-functions-and-get-it-in-pytorch/
CHICAGO
" » Set dtype with dtype argument functions and get it in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2024/06/25/set-dtype-with-dtype-argument-functions-and-get-it-in-pytorch/
IEEE
" » Set dtype with dtype argument functions and get it in PyTorch." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2024/06/25/set-dtype-with-dtype-argument-functions-and-get-it-in-pytorch/. [Accessed: ]
rf:citation
» Set dtype with dtype argument functions and get it in PyTorch | Super Kai (Kazuya Ito) | Sciencx | https://www.scien.cx/2024/06/25/set-dtype-with-dtype-argument-functions-and-get-it-in-pytorch/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.