DEV Community

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

Posted on • Updated on

ge and le in PyTorch

Buy Me a Coffee

*Memos:

ge() can check if the zero or more elements of the 1st 0D or more D tensor are greater than or equal to the zero or more elements of the 2nd 0D or more D tensor element-wise, getting the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • ge() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float or bool).
  • The 2nd argument with torch or the 1st argument with a tensor is other(Required-Type:tensor or scalar of int, float or bool).
  • There is out argument with torch(Optional-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • greater_equal() is the alias of ge().
import torch

tensor1 = torch.tensor([5, 0, 3])
tensor2 = torch.tensor([3, 5, 4])

torch.ge(input=tensor1, other=tensor2)
tensor1.ge(other=tensor2)
# tensor([True, False, False])

torch.ge(input=tensor2, other=tensor1)
# tensor([False, True, True])

tensor1 = torch.tensor([5, 0, 3])
tensor2 = torch.tensor([[3, 5, 4],
                        [6, 3, 5]])
torch.ge(input=tensor1, other=tensor2)
# tensor([[True, False, False],
#         [False, False, False]])

torch.ge(input=tensor2, other=tensor1)
# tensor([[False, True, True],
#         [True, True, True]])

torch.ge(input=tensor1, other=3)
# tensor([True, False, True])

torch.ge(input=tensor2, other=3)
# tensor([[True, True, True],
#         [True, True, True]])

tensor1 = torch.tensor([5., 0., 3.])
tensor2 = torch.tensor([[3., 5., 4.],
                        [6., 3., 5.]])
torch.ge(input=tensor1, other=tensor2)
# tensor([[True, False, False],
#         [False, False, False]])

torch.ge(input=tensor1, other=3.)
# tensor([True, False, True])

tensor1 = torch.tensor([True, False, True])
tensor2 = torch.tensor([[True, False, True],
                        [False, True, False]])
torch.ge(input=tensor1, other=tensor2)
# tensor([[True, True, True],
#         [True, False, True]])

torch.ge(input=tensor1, other=True)
# tensor([True, False, True])
Enter fullscreen mode Exit fullscreen mode

le() can check if the zero or more elements of the 1st 0D or more D tensor are less than or equal to the zero or more elements of the 2nd 0D or more D tensor element-wise, getting the 0D or more D tensor of zero or more element as shown below:

*Memos:

  • le() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float or bool).
  • The 2nd argument with torch or the 1st argument with a tensor is other(Required-Type:tensor or scalar of int, float or bool).
  • There is out argument with torch(Optional-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • less_equal() is the alias of le().
import torch

tensor1 = torch.tensor([5, 0, 3])
tensor2 = torch.tensor([3, 5, 4])

torch.le(input=tensor1, other=tensor2)
tensor1.le(other=tensor2)
# tensor([False, True, True])

torch.le(input=tensor2, other=tensor1)
# tensor([True, False, False])

tensor1 = torch.tensor([5, 0, 3])
tensor2 = torch.tensor([[3, 5, 4],
                        [6, 3, 5]])
torch.le(input=tensor1, other=tensor2)
# tensor([[False, True, True],
#         [True, True, True]])

torch.le(input=tensor2, other=tensor1)
# tensor([[True, False, False],
#         [False, False, False]])

torch.le(input=tensor1, other=3)
# tensor([False, True, True])

torch.le(input=tensor2, other=3)
# tensor([[True, False, False],
#         [False, True, False]])

tensor1 = torch.tensor([5., 0., 3.])
tensor2 = torch.tensor([[3., 5., 4.],
                        [6., 3., 5.]])
torch.le(input=tensor1, other=tensor2)
# tensor([[False, True, True],
#         [True, True, True]])

torch.le(input=tensor1, other=3.)
# tensor([False, True, True])

tensor1 = torch.tensor([True, False, True])
tensor2 = torch.tensor([[True, False, True],
                        [False, True, False]])
torch.le(input=tensor1, other=tensor2)
# tensor([[True, True, True],
#         [False, True, False]])

torch.le(input=tensor1, other=True)
# tensor([True, True, True])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)