DEV Community

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

Posted on • Edited on

zeros and zeros_like in PyTorch

Buy Me a Coffee

*My post explains ones() and ones_like().

zeros() can create the 1D or more D tensor of zero or more 0., 0, 0.+0.j or False as shown below:

*Memos:

  • zeros() can be used with torch but not with a tensor.
  • The 1st or more arguments with torch are size(Required-Type:int, tuple of int, list of int or size()).
  • 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.zeros(size=())
torch.zeros(size=torch.tensor(8).size())
# tensor(0.)

torch.zeros(size=(0,))
torch.zeros(0)
torch.zeros(size=torch.tensor([]).size())
# tensor([])

torch.zeros(size=(3,))
torch.zeros(3)
torch.zeros(size=torch.tensor([8, 3, 6]).size())
# tensor([0., 0., 0.])

torch.zeros(size=(3, 2))
torch.zeros(3, 2)
torch.zeros(size=torch.tensor([[8, 3], [6, 0], [2, 9]]).size())
# tensor([[0., 0.], [0., 0.], [0., 0.]])

torch.zeros(size=(3, 2, 4))
torch.zeros(3, 2, 4)
# tensor([[[0., 0., 0., 0.], [0., 0., 0., 0.]],
#         [[0., 0., 0., 0.], [0., 0., 0., 0.]],
#         [[0., 0., 0., 0.], [0., 0., 0., 0.]]])

torch.zeros(size=(3, 2, 4), dtype=torch.int64)
torch.zeros(3, 2, 4, dtype=torch.int64)
# tensor([[[0, 0, 0, 0], [0, 0, 0, 0]],
#         [[0, 0, 0, 0], [0, 0, 0, 0]],
#         [[0, 0, 0, 0], [0, 0, 0, 0]]])

torch.zeros(size=(3, 2, 4), dtype=torch.complex64)
torch.zeros(3, 2, 4, dtype=torch.complex64)
# tensor([[[0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
#          [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j]],
#         [[0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
#          [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j]],
#         [[0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
#          [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j]]])

torch.zeros(size=(3, 2, 4), dtype=torch.bool)
torch.zeros(3, 2, 4, dtype=torch.bool)
# tensor([[[False, False, False, False],
#          [False, False, False, False]],
#         [[False, False, False, False],
#          [False, False, False, False]],
#         [[False, False, False, False],
#          [False, False, False, False]]])
Enter fullscreen mode Exit fullscreen mode

zeros_like() can replace the zero or more floating-point numbers, integers, complex numbers or boolean values of a 0D or more D tensor with zero or more 0., 0, 0.+0.j or False as shown below:

*Memos:

  • zeros_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).
  • 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.
  • There is requires_grad argument with torch(Optional-Default:False-Type:bool): *Memos:
    • requires_grad= must be used.
    • My post explains requires_grad argument.
import torch

my_tensor = torch.tensor(7.)

torch.zeros_like(input=my_tensor)
# tensor(0.)

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

torch.zeros_like(input=my_tensor)
# tensor([0., 0., 0.])

my_tensor = torch.tensor([[7., 4., 5.], [2., 8., 3.]])

torch.zeros_like(input=my_tensor)
# tensor([[0., 0., 0.], [0., 0., 0.]])

my_tensor = torch.tensor([[[7., 4., 5.], [2., 8., 3.]],
                          [[6., 0., 1.], [5., 9., 4.]]])
torch.zeros_like(input=my_tensor)
# tensor([[[0., 0., 0.], [0., 0., 0.]],
#         [[0., 0., 0.], [0., 0., 0.]]])

my_tensor = torch.tensor([[[7, 4, 5], [2, 8, 3]],
                          [[6, 0, 1], [5, 9, 4]]])
torch.zeros_like(input=my_tensor)
# tensor([[[0, 0, 0], [0, 0, 0]],
#         [[0, 0, 0], [0, 0, 0]]])

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.zeros_like(input=my_tensor)
# tensor([[[0.+0.j, 0.+0.j, 0.+0.j],
#          [0.+0.j, 0.+0.j, 0.+0.j]],
#         [[0.+0.j, 0.+0.j, 0.+0.j],
#          [0.+0.j, 0.+0.j, 0.+0.j]]])

my_tensor = torch.tensor([[[True, False, True], [False, True, False]], 
                          [[False, True, False], [True, False, True]]])
torch.zeros_like(input=my_tensor)
# tensor([[[False, False, False], [False, False, False]],
#         [[False, False, False], [False, False, False]]])
Enter fullscreen mode Exit fullscreen mode

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

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