*Memos:
- My post explains how to set and get device.
- My post explains how to set requires_grad and get grad.
-
My post explains how to set
keepdim
argument. -
My post explains how to set
out
argument.
You can set and get dtype as shown below:
*Memos:
- I selected some popular
dtype
argument functions such as tensor(), arange(), rand(), rand_like(), sum() and view(). *Memos: - Basically,
dtype
(Optional-Default:None
-Type:dtype). - Basically, if
dtype
isNone
, get_default_dtype() is used or it's inferred from data, then additionally for floating point numbers,get_default_dtype()
is used. *My post explainsget_default_dtype()
and set_default_dtype(). -
dtype
can also accept int(), float() and bool() but not complex() which are python built-in functions. - Basically,
dtype=
must be used. -
dtype
cannot beint
orfloat
for acomplex
tensor. - type() can get the legacy constructor which is an old dtype.
tensor()
. *My post explains tensor()
:
import torch
my_tensor = torch.tensor(data=[0, 1, 2], dtype=torch.float64)
my_tensor = torch.tensor(data=[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(data=[0, 1, 2], dtype=torch.complex64)
my_tensor, my_tensor.dtype, my_tensor.type()
# (tensor([0.+0.j, 1.+0.j, 2.+0.j]),
# torch.complex64,
# 'torch.ComplexFloatTensor')
my_tensor = torch.tensor(data=[0, 1, 2], dtype=torch.bool)
my_tensor = torch.tensor(data=[0, 1, 2], dtype=bool)
my_tensor, my_tensor.dtype, my_tensor.type()
# (tensor([False, True, True]), torch.bool, 'torch.BoolTensor')
my_tensor = torch.tensor(data=[0., 1., 2.], dtype=torch.int64)
my_tensor = torch.tensor(data=[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(data=[0., 1., 2.], dtype=torch.complex64)
my_tensor, my_tensor.dtype, my_tensor.type()
# (tensor([0.+0.j, 1.+0.j, 2.+0.j]),
# torch.complex64,
# 'torch.ComplexFloatTensor')
my_tensor = torch.tensor(data=[0., 1., 2.], dtype=torch.bool)
my_tensor = torch.tensor(data=[0., 1., 2.], dtype=bool)
my_tensor, my_tensor.dtype, my_tensor.type()
# (tensor([False, True, True]), torch.bool, 'torch.BoolTensor')
my_tensor = torch.tensor(data=[0.+0.j, 0.+7.j, 2.+0.j], dtype=torch.bool)
my_tensor = torch.tensor(data=[0.+0.j, 0.+7.j, 2.+0.j], dtype=bool)
my_tensor, my_tensor.dtype, my_tensor.type()
# (tensor([False, True, True]), torch.bool, 'torch.BoolTensor')
my_tensor = torch.tensor(data=[True, False, True], dtype=torch.int64)
my_tensor = torch.tensor(data=[True, False, True], dtype=int)
my_tensor, my_tensor.dtype, my_tensor.type()
# (tensor([1, 0, 1]), torch.int64, 'torch.LongTensor')
my_tensor = torch.tensor(data=[True, False, True], dtype=torch.float64)
my_tensor = torch.tensor(data=[True, False, True], dtype=float)
my_tensor, my_tensor.dtype, my_tensor.type()
# (tensor([1., 0., 1.], dtype=torch.float64),
# torch.float64,
# 'torch.DoubleTensor')
my_tensor = torch.tensor(data=[True, False, True], dtype=torch.complex64)
my_tensor, my_tensor.dtype, my_tensor.type()
# (tensor([1.+0.j, 0.+0.j, 1.+0.j]),
# torch.complex64,
# 'torch.ComplexFloatTensor')
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')
Top comments (0)