full() can create a 1D or more D tensor filled with zero or more elements as shown below:
*Memos:
-
full()
can be used with torch but not with a tensor. - The 1st argument(
tuple
ofint
,list
ofint
or torch.Size) issize
(Required). - The 2nd argument(
int
,float
,complex
orbool
) isfill_value
(Required). - There is
dtype
argument(torch.dtype) (Optional) withtorch
. *Memos:- If
dtype
is not given, thedtype
offill_value
is used or thedtype
of set_default_dtype() is used for floating-point numbers. -
dtype
can also accept int(), float() and bool() but not complex() which are python built-in functions. -
dtype=
must be used.
- If
- There is
device
argument(int
,str
or torch.device) (Optional) withtorch
. *Memos:- If
device
is not given, thedevice
of set_default_device() is used. -
cpu
,cuda
,ipu
,xpu
,mkldnn
,opengl
,opencl
,ideep
,hip
,ve
,fpga
,ort
,xla
,lazy
,vulkan
,mps
,meta
,hpu
,mtia
orprivateuseone
can be set todevice
. - Setting
0
uses GPU(CUDA). -
device=
must be used.
- If
import torch
torch.full(size=(3,), fill_value=5)
torch.full(size=torch.tensor([0, 1, 2]).size(), fill_value=5)
# tensor([5, 5, 5])
torch.full(size=(3, 2), fill_value=5)
# tensor([[5, 5], [5, 5], [5, 5]])
torch.full(size=(3, 2, 4), fill_value=5,
dtype=torch.int64, device='cuda:0')
torch.full(size=(3, 2, 4), fill_value=5,
dtype=torch.int64, device='cuda')
torch.full(size=(3, 2, 4), fill_value=5,
dtype=torch.int64, device=0)
torch.full(size=(3, 2, 4), fill_value=5,
dtype=int, device=torch.device('cuda:0'))
torch.full(size=(3, 2, 4), fill_value=5,
dtype=int, device=torch.device(type='cuda'))
torch.full(size=(3, 2, 4), fill_value=5,
dtype=int, device=torch.device(type='cuda', index=0))
# 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., dtype=torch.float64)
torch.full(size=(3, 2, 4), fill_value=5., dtype=float)
# 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.]]], dtype=torch.float64)
torch.full(size=(3, 2, 4), fill_value=5.+6.j, dtype=torch.complex64)
# 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, dtype=torch.bool)
torch.full(size=(3, 2, 4), fill_value=True, dtype=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]]])
torch.full(size=(0,), fill_value=5)
torch.full(size=(0,), fill_value=5, device='cpu')
torch.full(size=(0,), fill_value=5, device=torch.device(device='cpu'))
torch.full(size=(0,), fill_value=5, device=torch.device(type='cpu'))
# tensor([], dtype=torch.int64)
torch.full(size=(0,), fill_value=5, device='cuda:0')
torch.full(size=(0,), fill_value=5, device='cuda')
torch.full(size=(0,), fill_value=5, device=0)
torch.full(size=(0,), fill_value=5, device=torch.device(device='cuda:0'))
torch.full(size=(0,), fill_value=5, device=torch.device(type='cuda'))
torch.full(size=(0,), fill_value=5, device=torch.device(type='cuda', index=0))
# tensor([], dtype=torch.int64)
full_like() can replace the zero or more elements of a 0D or more D tensor with zero or more elements as shown below:
*Memos:
-
full_like()
can be used withtorch
but not with a tensor. - The 2nd argument(
int
,float
,complex
orbool
) isfill_value
(Required). - There is
dtype
argument(torch.dtype) (Optional-Default:None
) withtorch
. *Memos:- If
dtype
is not given, the type of theinput
tensor is used. -
dtype
can also accept int(), float() and bool() but not complex() which are python built-in functions. -
dtype=
must be used.
- If
- There is
device
argument(int
,str
or torch.device) (Optional) withtorch
. *Memos:- If
device
is not given, thedevice
of set_default_device() is used. -
cpu
,cuda
,ipu
,xpu
,mkldnn
,opengl
,opencl
,ideep
,hip
,ve
,fpga
,ort
,xla
,lazy
,vulkan
,mps
,meta
,hpu
,mtia
orprivateuseone
can be set todevice
. - Setting
0
uses GPU(CUDA). -
device=
must be used.
- If
import torch
my_tensor = torch.tensor(7)
torch.full_like(input=my_tensor, fill_value=5)
torch.full_like(input=my_tensor, fill_value=5,
device=torch.device(device='cpu'))
torch.full_like(input=my_tensor, fill_value=5,
device=torch.device(type='cpu'))
# 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,
dtype=torch.int64, device='cuda:0')
torch.full_like(input=my_tensor, fill_value=5,
dtype=torch.int64, device='cuda')
torch.full_like(input=my_tensor, fill_value=5,
dtype=torch.int64, device=0)
torch.full_like(input=my_tensor, fill_value=5,
dtype=int, device=torch.device('cuda:0'))
torch.full_like(input=my_tensor, fill_value=5,
dtype=int, device=torch.device(type='cuda'))
torch.full_like(input=my_tensor, fill_value=5,
dtype=int, device=torch.device(type='cuda', index=0))
# 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., dtype=torch.float64)
torch.full_like(input=my_tensor, fill_value=5., dtype=float)
# 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,
dtype=torch.complex64)
# 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=False, dtype=torch.bool)
torch.full_like(input=my_tensor, fill_value=False, dtype=bool)
# tensor([[[False, False, False],
# [False, False, False]],
# [[False, False, False],
# [False, False, False]]])
Top comments (0)