*My post explains zeros() and zeros_like().
ones() can create the 1D or more D tensor of zero or more 1.
, 1
, 1.+0.j
or True
as shown below:
*Memos:
-
ones()
can be used withtorch
but not with a tensor. - The 1st or more arguments with
torch
aresize
(Required-Type:int
,tuple
ofint
,list
ofint
or size()). - There is
dtype
argument withtorch
(Optional-Default:None
-Type:dtype): *Memos:- If it's
None
, get_default_dtype() is used. *My post explainsget_default_dtype()
and set_default_dtype(). -
dtype=
must be used. -
My post explains
dtype
argument.
- If it's
- There is
device
argument withtorch
(Optional-Default:None
-Type:str
,int
or device()): *Memos:- If it's
None
, get_default_device() is used. *My post explainsget_default_device()
and set_default_device(). -
device=
must be used. -
My post explains
device
argument.
- If it's
- There is
requires_grad
argument withtorch
(Optional-Default:False
-Type:bool
): *Memos:-
requires_grad=
must be used. -
My post explains
requires_grad
argument.
-
- There is
out
argument withtorch
(Optional-Default:None
-Type:tensor
): *Memos:-
out=
must be used. -
My post explains
out
argument.
-
import torch
torch.ones(size=())
torch.ones(size=torch.tensor(8).size())
# tensor(1.)
torch.ones(size=(0,))
torch.ones(0)
torch.ones(size=torch.tensor([]).size())
# tensor([])
torch.ones(size=(3,))
torch.ones(3)
torch.ones(size=torch.tensor([8, 3, 6]).size())
# tensor([1., 1., 1.])
torch.ones(size=(3, 2))
torch.ones(3, 2)
torch.ones(size=torch.tensor([[8, 3], [6, 0], [2, 9]]).size())
# tensor([[1., 1.], [1., 1.], [1., 1.]])
torch.ones(size=(3, 2, 4))
torch.ones(3, 2, 4)
# tensor([[[1., 1., 1., 1.], [1., 1., 1., 1.]],
# [[1., 1., 1., 1.], [1., 1., 1., 1.]],
# [[1., 1., 1., 1.], [1., 1., 1., 1.]]])
torch.ones(size=(3, 2, 4), dtype=torch.int64)
torch.ones(3, 2, 4, dtype=torch.int64)
# tensor([[[1, 1, 1, 1], [1, 1, 1, 1]],
# [[1, 1, 1, 1], [1, 1, 1, 1]],
# [[1, 1, 1, 1], [1, 1, 1, 1]]])
torch.ones(size=(3, 2, 4), dtype=torch.complex64)
torch.ones(3, 2, 4, dtype=torch.complex64)
# tensor([[[1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j],
# [1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j]],
# [[1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j],
# [1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j]],
# [[1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j],
# [1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j]]])
torch.ones(size=(3, 2, 4), dtype=torch.bool)
torch.ones(3, 2, 4, dtype=torch.bool)
# tensor([[[True, True, True, True],
# [True, True, True, True]],
# [[True, True, True, True],
# [True, True, True, True]],
# [[True, True, True, True],
# [True, True, True, True]]])
ones_like() can replace the zero or more integers, floating-point numbers, integers, complex numbers or boolean values of a 0D or more D tensor with zero or more 1.
, 1
, 1.+0.j
or True
as shown below:
*Memos:
-
ones_like()
can be used withtorch
but not with a tensor. - The 1st argument with
torch
isinput
(Required-Type:tensor
ofint
,float
,complex
orbool
). - There is
dtype
argument withtorch
(Optional-Default:None
-Type:dtype): *Memos:- If it's
None
, it's inferred frominput
. -
dtype=
must be used. -
My post explains
dtype
argument.
- If it's
- There is
device
argument withtorch
(Optional-Default:None
-Type:str
,int
or device()): *Memos:- If it's
None
, it's inferred frominput
. -
device=
must be used. -
My post explains
device
argument.
- If it's
- There is
requires_grad
argument withtorch
(Optional-Default:False
-Type:bool
): *Memos:-
requires_grad=
must be used. -
My post explains
requires_grad
argument.
-
import torch
my_tensor = torch.tensor(7.)
torch.ones_like(input=my_tensor)
# tensor(1.)
my_tensor = torch.tensor([7., 4., 5.])
torch.ones_like(input=my_tensor)
# tensor([1., 1., 1.])
my_tensor = torch.tensor([[7., 4., 5.], [2., 8., 3.]])
torch.ones_like(input=my_tensor)
# tensor([[1., 1., 1.], [1., 1., 1.]])
my_tensor = torch.tensor([[[7., 4., 5.], [2., 8., 3.]],
[[6., 0., 1.], [5., 9., 4.]]])
torch.ones_like(input=my_tensor)
# tensor([[[1., 1., 1.], [1., 1., 1.]],
# [[1., 1., 1.], [1., 1., 1.]]])
my_tensor = torch.tensor([[[7, 4, 5], [2, 8, 3]],
[[6, 0, 1], [5, 9, 4]]])
torch.ones_like(input=my_tensor)
# tensor([[[1, 1, 1], [1, 1, 1]],
# [[1, 1, 1], [1, 1, 1]]])
my_tensor = torch.tensor([[[7.+4.j, 4.+2.j, 5.+3.j],
[2.+5.j, 8.+1.j, 3.+9.j]],
[[6.+9.j, 0.+3.j, 1.+8.j],
[5.+3.j, 9.+4.j, 4.+6.j]]])
torch.ones_like(input=my_tensor)
# tensor([[[1.+0.j, 1.+0.j, 1.+0.j],
# [1.+0.j, 1.+0.j, 1.+0.j]],
# [[1.+0.j, 1.+0.j, 1.+0.j],
# [1.+0.j, 1.+0.j, 1.+0.j]]])
my_tensor = torch.tensor([[[True, False, True], [False, True, False]],
[[False, True, False], [True, False, True]]])
torch.ones_like(input=my_tensor)
# tensor([[[True, True, True], [True, True, True]],
# [[True, True, True], [True, True, True]]])
Top comments (0)