gt(), lt(), ge() and le() in PyTorch
Tags: pytorch, gt, lt, ge
Published: 2024-04-15 04:26:06

*[My post](https://dev.to/hyperkai/equal-eq-and-ne-in-pytorch-1361) explains [equal()](https://pytorch.org/docs/stable/generated/torch.equal.html), [eq()](https://pytorch.org/docs/stable/generated/torch.eq.html) and [ne()](https://pytorch.org/docs/stable/generated/torch.ne.html).

[gt()](https://pytorch.org/docs/stable/generated/torch.gt.html) can check 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](https://pytorch.org/docs/stable/torch.html) or a tensor.
- The 1st argument with `torch` or using a tensor is `input`(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-Type:`tensor`):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.
- [greater()](https://pytorch.org/docs/stable/generated/torch.greater.html) is the alias of `gt()`.

```python
import torch

tensor1 = torch.tensor([5, 0, 3])
tensor2 = torch.tensor([[3, 5, 4],
                        [6, 3, 5]])
torch.gt(input=tensor1, other=tensor2)
tensor1.gt(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])
```

[lt()](https://pytorch.org/docs/stable/generated/torch.lt.html) can check 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 with `torch` or using a tensor is `input`(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-Type:`tensor`):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.
- [less()](https://pytorch.org/docs/stable/generated/torch.less.html) is the alias of `lt()`.

```python
import torch

tensor1 = torch.tensor([5, 0, 3])
tensor2 = torch.tensor([[3, 5, 4],
                        [6, 3, 5]])
torch.lt(input=tensor1, other=tensor2)
tensor1.lt(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])
```

[ge()](https://pytorch.org/docs/stable/generated/torch.ge.html) can check 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` and a tensor.
- The 1st argument with `torch` or using a tensor is `input`(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-Type:`tensor`):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.
- [greater_equal()](https://pytorch.org/docs/stable/generated/torch.greater_equal.html) is the alias of `ge()`.

```python
import torch

tensor1 = torch.tensor([5, 0, 3])
tensor2 = torch.tensor([[3, 5, 4],
                        [6, 3, 5]])
torch.ge(input=tensor1, other=tensor2)
tensor1.ge(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])
```

[le()](https://pytorch.org/docs/stable/generated/torch.le.html) can check 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` and a tensor.
- The 1st argument with `torch` or using a tensor is `input`(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-Type:`tensor`):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.
- [less_equal()](https://pytorch.org/docs/stable/generated/torch.less_equal.html) is the alias of `le()`.

```python
import torch

tensor1 = torch.tensor([5, 0, 3])
tensor2 = torch.tensor([[3, 5, 4],
                        [6, 3, 5]])
torch.le(input=tensor1, other=tensor2)
tensor1.le(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])
```