DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

full() and full_like() in PyTorch

full() can create a 1D or more D tensor filled with the zero or more integers, floating-point numbers, complex numbers or boolean values as shown below:

*Memos:

  • full() can be used with torch but not with a tensor.
  • The 1st argument(tuple of int, list of int or torch.Size) is size(Required).
  • The 2nd argument(int, float, complex or bool) is fill_value(Required).
import torch

torch.full(size=(0,), fill_value=5)
# tensor([], dtype=torch.int64)

torch.full(size=(3,), 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+6j)
# 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 replace the zero or more integers, floating-point numbers, complex numbers or boolean values of 0D or more D tensor with the zero or more integers, floating-point numbers, complex numbers or boolean values as shown below:

*Memos:

  • full_like() can be used with torch but not with a tensor.
  • The 2nd argument(int, float, complex or bool) is fill_value(Required).
import torch

my_tensor = torch.tensor(7)
torch.full_like(my_tensor, fill_value=5)
# tensor(5)

my_tensor = torch.tensor([7, 4, 5])
torch.full_like(my_tensor, fill_value=5)
# tensor([5, 5, 5])

my_tensor = torch.tensor([[7, 4, 5],
                          [2, 8, 3]])
torch.full_like(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(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(my_tensor, fill_value=5.)
# tensor([[[5., 5., 5.], [5., 5., 5.]],
#         [[5., 5., 5.], [5., 5., 5.]]])

my_tensor = torch.tensor([[[7+4j, 4+2j, 5+3j], [2+5j, 8+1j, 3+9j]],
                          [[6+9j, 0+3j, 1+8j], [5+3j, 9+4j, 4+6j]]])
torch.full_like(my_tensor, fill_value=5+3j)
# 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(my_tensor, fill_value=False)
# tensor([[[False, False, False],
#          [False, False, False]],
#         [[False, False, False],
#          [False, False, False]]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)