full() can create the 0D or more D tensor of zero or more elements as shown below:
*Memos:
-
full()
can be used with torch but not with a tensor. - The 1st argument is
size
(Required-Type:tuple
ofint
,list
ofint
or size()). - The 2nd argument with
torch
isfill_value
(Required-Type:int
,float
,complex
orbool
). *The 0D tensor of an element(int
,float
,complex
orbool
) also works. - There is
dtype
argument withtorch
(Optional-Default:None
-Type:dtype): *Memos:- If it's
None
, it's inferred fromfill_value
, then for floating-point numbers, 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.full(size=(), fill_value=5)
torch.full(size=(), fill_value=torch.tensor(5))
torch.full(size=torch.tensor(8).size(), fill_value=5)
# tensor(5)
torch.full(size=(3,), fill_value=5)
torch.full(size=(3,), fill_value=torch.tensor(5))
torch.full(size=torch.tensor([8, 3, 6]).size(), fill_value=5)
# tensor([5, 5, 5])
torch.full(size=(3, 2), fill_value=5)
torch.full(size=torch.tensor([[8, 3], [6, 0], [2, 9]]).size(), fill_value=5)
# tensor([[5, 5], [5, 5], [5, 5]])
torch.full(size=(3, 2, 4), fill_value=5)
# tensor([[[5, 5, 5, 5], [5, 5, 5, 5]],
# [[5, 5, 5, 5], [5, 5, 5, 5]],
# [[5, 5, 5, 5], [5, 5, 5, 5]]])
torch.full(size=(3, 2, 4), fill_value=5.)
# tensor([[[5., 5., 5., 5.], [5., 5., 5., 5.]],
# [[5., 5., 5., 5.], [5., 5., 5., 5.]],
# [[5., 5., 5., 5.], [5., 5., 5., 5.]]])
torch.full(size=(3, 2, 4), fill_value=5.+6.j)
# tensor([[[5.+6.j, 5.+6.j, 5.+6.j, 5.+6.j],
# [5.+6.j, 5.+6.j, 5.+6.j, 5.+6.j]],
# [[5.+6.j, 5.+6.j, 5.+6.j, 5.+6.j],
# [5.+6.j, 5.+6.j, 5.+6.j, 5.+6.j]],
# [[5.+6.j, 5.+6.j, 5.+6.j, 5.+6.j],
# [5.+6.j, 5.+6.j, 5.+6.j, 5.+6.j]]])
torch.full(size=(3, 2, 4), fill_value=True)
# 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]]])
full_like() can get the 0D or more D tensor of the zero or more elements replaced with zero or more elements as shown below:
*Memos:
-
full_like()
can be used withtorch
but not with a tensor. - The 1st argument with
torch
isinput
(Required-Type:tensor
ofint
,float
,complex
orbool
). - The 2nd argument with
torch
isfill_value
(Required-Type:int
,float
,complex
orbool
). *The 0D tensor of an element(int
,float
,complex
orbool
) also works. - 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
import torch
my_tensor = torch.tensor(7)
torch.full_like(input=my_tensor, fill_value=5)
torch.full_like(input=my_tensor, fill_value=torch.tensor(5))
# tensor(5)
my_tensor = torch.tensor([7, 4, 5])
torch.full_like(input=my_tensor, fill_value=5)
# tensor([5, 5, 5])
my_tensor = torch.tensor([[7, 4, 5],
[2, 8, 3]])
torch.full_like(input=my_tensor, fill_value=5)
# tensor([[5, 5, 5], [5, 5, 5]])
my_tensor = torch.tensor([[[7, 4, 5], [2, 8, 3]],
[[6, 0, 1], [5, 9, 4]]])
torch.full_like(input=my_tensor, fill_value=5)
# tensor([[[5, 5, 5], [5, 5, 5]],
# [[5, 5, 5], [5, 5, 5]]])
my_tensor = torch.tensor([[[7., 4., 5.], [2., 8., 3.]],
[[6., 0., 1.], [5., 9., 4.]]])
torch.full_like(input=my_tensor, fill_value=5.)
# tensor([[[5., 5., 5.], [5., 5., 5.]],
# [[5., 5., 5.], [5., 5., 5.]]])
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.full_like(input=my_tensor, fill_value=5.+3.j)
# tensor([[[5.+3.j, 5.+3.j, 5.+3.j],
# [5.+3.j, 5.+3.j, 5.+3.j]],
# [[5.+3.j, 5.+3.j, 5.+3.j],
# [5.+3.j, 5.+3.j, 5.+3.j]]])
my_tensor = torch.tensor([[[True, False, True],
[False, True, False]],
[[True, False, True],
[False, True, False]]])
torch.full_like(input=my_tensor, fill_value=True)
# tensor([[[True, True, True],
# [True, True, True]],
# [[True, True, True],
# [True, True, True]]])
Top comments (0)