DEV Community

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

Posted on • Updated on

ones() and ones_like() in PyTorch

Buy Me a Coffee

*My post explains zeros() and zeros_like().

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

*Memos:

  • ones() 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-Type:dtype): *Memos:
  • There is device argument with torch(Optional-Type:str, int or device()): *Memos:
  • 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.ones(size=())
torch.ones(size=torch.tensor(8).size())
# tensor(1.)

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

torch.ones(size=(3,))
torch.ones(3)
torch.ones(size=torch.tensor([8, 3, 6]).size())
# tensor([1., 1., 1.])

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

torch.ones(size=(3, 2, 4))
torch.ones(3, 2, 4)
# tensor([[[1., 1., 1., 1.], [1., 1., 1., 1.]],
#         [[1., 1., 1., 1.], [1., 1., 1., 1.]],
#         [[1., 1., 1., 1.], [1., 1., 1., 1.]]])

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

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

torch.ones(size=(3, 2, 4), dtype=torch.bool)
torch.ones(3, 2, 4, dtype=torch.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]]])
Enter fullscreen mode Exit fullscreen mode

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

*Memos:

  • ones_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-Type:dtype): *Memos:
    • If dtype is not given, it is inferred from input.
    • 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, it is inferred from input.
    • 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.
import torch

my_tensor = torch.tensor(7.)

torch.ones_like(input=my_tensor)
# tensor(1.)

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

torch.ones_like(input=my_tensor)
# tensor([1., 1., 1.])

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

torch.ones_like(input=my_tensor)
# tensor([[1., 1., 1.], [1., 1., 1.]])

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

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

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

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

Top comments (0)