DEV Community

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

Posted on • Edited on

full and full_like in PyTorch

Buy Me a Coffee

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 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-Default:None-Type:dtype): *Memos:
  • There is device argument with torch(Optional-Default:None-Type:str, int or device()): *Memos:
  • There is requires_grad argument with torch(Optional-Default:False-Type:bool): *Memos:
    • requires_grad= must be used.
    • My post explains requires_grad argument.
  • There is out argument with torch(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]]])
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-Default:None-Type:dtype): *Memos:
    • If it's None, it's inferred from input.
    • dtype= must be used.
    • My post explains dtype argument.
  • There is device argument with torch(Optional-Default:None-Type:str, int or device()): *Memos:
    • If it's None, it's inferred from input.
    • 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

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay