DEV Community

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

Posted on • Edited on

gt and lt in PyTorch

Buy Me a Coffee

*Memos:

gt() can check if the zero or more elements of the 1st 0D or more D tensor are greater than 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:

  • gt() 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() is the alias of gt().
import torch tensor1 = torch.tensor([5, 0, 3]) tensor2 = torch.tensor([3, 5, 4]) torch.gt(input=tensor1, other=tensor2) tensor1.gt(other=tensor2) # tensor([True, False, False])  torch.gt(input=tensor2, other=tensor1) # tensor([False, True, True])  tensor1 = torch.tensor([5, 0, 3]) tensor2 = torch.tensor([[3, 5, 4], [6, 3, 5]]) torch.gt(input=tensor1, other=tensor2) # tensor([[True, False, False], # [False, False, False]])  torch.gt(input=tensor2, other=tensor1) # tensor([[False, True, True], # [True, True, True]])  torch.gt(input=tensor1, other=3) # tensor([True, False, False])  torch.gt(input=tensor2, other=3) # tensor([[False, True, True], # [True, False, True]])  tensor1 = torch.tensor([5., 0., 3.]) tensor2 = torch.tensor([[3., 5., 4.], [6., 3., 5.]]) torch.gt(input=tensor1, other=tensor2) # tensor([[True, False, False], # [False, False, False]])  torch.gt(input=tensor1, other=3.) # tensor([True, False, False])  tensor1 = torch.tensor([True, False, True]) tensor2 = torch.tensor([[True, False, True], [False, True, False]]) torch.gt(input=tensor1, other=tensor2) # tensor([[False, False, False], # [True, False, True]])  torch.gt(input=tensor1, other=True) # tensor([False, False, False]) 
Enter fullscreen mode Exit fullscreen mode

lt() can check if the zero or more elements of the 1st 0D or more D tensor are less than 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:

  • lt() 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() is the alias of lt().
import torch tensor1 = torch.tensor([5, 0, 3]) tensor2 = torch.tensor([3, 5, 4]) torch.lt(input=tensor1, other=tensor2) tensor1.lt(other=tensor2) # tensor([False, True, True])  torch.lt(input=tensor2, other=tensor1) # tensor([True, False, False])  tensor1 = torch.tensor([5, 0, 3]) tensor2 = torch.tensor([[3, 5, 4], [6, 3, 5]]) torch.lt(input=tensor1, other=tensor2) # tensor([[False, True, True], # [True, True, True]])  torch.lt(input=tensor2, other=tensor1) # tensor([[True, False, False], # [False, False, False]])  torch.lt(input=tensor1, other=3) # tensor([False, True, False])  torch.lt(input=tensor2, other=3) # tensor([[False, False, False], # [False, False, False]])  tensor1 = torch.tensor([5., 0., 3.]) tensor2 = torch.tensor([[3., 5., 4.], [6., 3., 5.]]) torch.lt(input=tensor1, other=tensor2) # tensor([[False, True, True], # [True, True, True]])  torch.lt(input=tensor1, other=3.) # tensor([False, True, False])  tensor1 = torch.tensor([True, False, True]) tensor2 = torch.tensor([[True, False, True], [False, True, False]]) torch.lt(input=tensor1, other=tensor2) # tensor([[False, False, False], # [False, True, False]])  torch.lt(input=tensor1, other=True) # tensor([False, True, False]) 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)