*My post explains ones() and ones_like().
zeros() can create the 1D or more D tensor of zero or more 0.
, 0
, 0.+0.j
or False
as shown below:
*Memos:
-
zeros()
can be used with torch 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-Type:dtype): *Memos:- If
dtype
is not given, get_default_dtype() is used. *My post explainsget_default_dtype()
and set_default_dtype(). -
dtype=
must be used. -
My post explains
dtype
argument.
- If
- There is
device
argument withtorch
(Optional-Type:str
,int
or device()): *Memos:- If
device
is not given, get_default_device() is used. *My post explainsget_default_device()
and set_default_device(). -
device=
must be used. -
My post explains
device
argument.
- If
- There is
requires_grad
argument withtorch
(Optional-Type:bool
): *Memos:-
requires_grad=
must be used. -
My post explains
requires_grad
argument.
-
- There is
out
argument withtorch
(Optional-Type:tensor
): *Memos:-
out=
must be used. -
My post explains
out
argument.
-
import torch
torch.zeros(size=())
torch.zeros(size=torch.tensor(8).size())
# tensor(0.)
torch.zeros(size=(0,))
torch.zeros(0)
torch.zeros(size=torch.tensor([]).size())
# tensor([])
torch.zeros(size=(3,))
torch.zeros(3)
torch.zeros(size=torch.tensor([8, 3, 6]).size())
# tensor([0., 0., 0.])
torch.zeros(size=(3, 2))
torch.zeros(3, 2)
torch.zeros(size=torch.tensor([[8, 3], [6, 0], [2, 9]]).size())
# tensor([[0., 0.], [0., 0.], [0., 0.]])
torch.zeros(size=(3, 2, 4))
torch.zeros(3, 2, 4)
# tensor([[[0., 0., 0., 0.], [0., 0., 0., 0.]],
# [[0., 0., 0., 0.], [0., 0., 0., 0.]],
# [[0., 0., 0., 0.], [0., 0., 0., 0.]]])
torch.zeros(size=(3, 2, 4), dtype=torch.int64)
torch.zeros(3, 2, 4, dtype=torch.int64)
# tensor([[[0, 0, 0, 0], [0, 0, 0, 0]],
# [[0, 0, 0, 0], [0, 0, 0, 0]],
# [[0, 0, 0, 0], [0, 0, 0, 0]]])
torch.zeros(size=(3, 2, 4), dtype=torch.complex64)
torch.zeros(3, 2, 4, dtype=torch.complex64)
# tensor([[[0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
# [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j]],
# [[0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
# [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j]],
# [[0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
# [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j]]])
torch.zeros(size=(3, 2, 4), dtype=torch.bool)
torch.zeros(3, 2, 4, dtype=torch.bool)
# tensor([[[False, False, False, False],
# [False, False, False, False]],
# [[False, False, False, False],
# [False, False, False, False]],
# [[False, False, False, False],
# [False, False, False, False]]])
zeros_like() can replace the zero or more floating-point numbers, integers, complex numbers or boolean values of a 0D or more D tensor with zero or more 0.
, 0
, 0.+0.j
or False
as shown below:
*Memos:
-
zeros_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-Type:dtype): *Memos:- If
dtype
is not given, it is inferred frominput
. -
dtype=
must be used. -
My post explains
dtype
argument.
- If
- There is
device
argument withtorch
(Optional-Type:str
,int
or device()): *Memos:- If
device
is not given, it is inferred frominput
. -
device=
must be used. -
My post explains
device
argument.
- If
- There is
requires_grad
argument withtorch
(Optional-Type:bool
): *Memos:-
requires_grad=
must be used. -
My post explains
requires_grad
argument.
-
import torch
my_tensor = torch.tensor(7.)
torch.zeros_like(input=my_tensor)
# tensor(0.)
my_tensor = torch.tensor([7., 4., 5.])
torch.zeros_like(input=my_tensor)
# tensor([0., 0., 0.])
my_tensor = torch.tensor([[7., 4., 5.], [2., 8., 3.]])
torch.zeros_like(input=my_tensor)
# tensor([[0., 0., 0.], [0., 0., 0.]])
my_tensor = torch.tensor([[[7., 4., 5.], [2., 8., 3.]],
[[6., 0., 1.], [5., 9., 4.]]])
torch.zeros_like(input=my_tensor)
# tensor([[[0., 0., 0.], [0., 0., 0.]],
# [[0., 0., 0.], [0., 0., 0.]]])
my_tensor = torch.tensor([[[7, 4, 5], [2, 8, 3]],
[[6, 0, 1], [5, 9, 4]]])
torch.zeros_like(input=my_tensor)
# tensor([[[0, 0, 0], [0, 0, 0]],
# [[0, 0, 0], [0, 0, 0]]])
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.zeros_like(input=my_tensor)
# tensor([[[0.+0.j, 0.+0.j, 0.+0.j],
# [0.+0.j, 0.+0.j, 0.+0.j]],
# [[0.+0.j, 0.+0.j, 0.+0.j],
# [0.+0.j, 0.+0.j, 0.+0.j]]])
my_tensor = torch.tensor([[[True, False, True], [False, True, False]],
[[False, True, False], [True, False, True]]])
torch.zeros_like(input=my_tensor)
# tensor([[[False, False, False], [False, False, False]],
# [[False, False, False], [False, False, False]]])
Top comments (0)