full() and full_like() in PyTorch

Super Kai (Kazuya Ito) - Apr 24 - - Dev Community

full() can create the 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 is size(Required-Type:tuple of int, list of int or size()).
  • The 2nd argument with torch is fill_value(Required-Type:int, float, complex or bool). *The 0D tensor of an element(int, float, complex or bool) also works.
  • There is dtype argument with torch(Optional-Type:dtype): *Memos:
    • If dtype is not given, dtype is inferred from fill_value or dtype of set_default_dtype() is used for floating-point numbers.
    • dtype= must be used.
    • My post explains dtype argument.
  • There is device argument with torch(Optional-Type:str, int or device()): *Memos:
    • device= must be used.
    • My post explains device argument.
  • There is requires_grad argument with torch(Optional-Type:bool): *Memos:
    • requires_grad= must be used.
    • My post explains requires_grad argument.
  • There is out argument with torch(Optional-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
import torch

torch.full(size=(3,), fill_value=5)
torch.full(size=(3,), fill_value=torch.tensor(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)
# 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]]])
Enter fullscreen mode Exit fullscreen mode

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 with torch but not with a tensor.
  • The 1st argument with torch is input(Required-Type:tensor of int, float, complex or bool).
  • The 2nd argument with torch is fill_value(Required-Type:int, float, complex or bool). *The 0D tensor of an element(int, float, complex or bool) also works.
  • There is dtype argument with torch(Optional-Type:dtype): *Memos:
    • If dtype is not given, dtype is inferred from input tensor.
    • dtype= must be used.
    • My post explains dtype argument.
  • There is device argument with torch(Optional-Type:str, int or device()): *Memos:
    • If device is not given, device of input tensor is used.
    • device= must be used.
    • My post explains device argument.
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]]])
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .