DEV Community

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

Posted on • Updated on

equal(), eq() and ne() in PyTorch

My post explains gt(), lt(), ge() and le().

equal() can check the 1st 0D or more D tensor is equal to the 2nd 0D or more D tensor as shown below:

*Memos:

  • equal() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float, complex or bool) with torch or using a tensor(tensor of int, float, complex or bool) is input(Required).
  • The 2nd argument(tensor of int, float, complex or bool) with torch or the 1st argument with a tensor(tensor of int, float, complex or bool) is other(Required).
import torch

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

torch.equal(input=tensor1, other=tensor2)
tensor1.equal(other=tensor2)
torch.equal(input=tensor2, other=tensor1)
tensor2.equal(other=tensor1)
# True

tensor1 = torch.tensor([5., 9., 3.])
tensor2 = torch.tensor([5+0j, 9+0j, 3+0j])

torch.equal(input=tensor1, other=tensor2)
torch.equal(input=tensor2, other=tensor1)
# True

tensor1 = torch.tensor([1+0j, 0+0j, 1+0j])
tensor2 = torch.tensor([True, False, True])

torch.equal(input=tensor1, other=tensor2)
torch.equal(input=tensor2, other=tensor1)
# True

tensor1 = torch.tensor([5, 9, 3])
tensor2 = torch.tensor([7, 9, 3])

torch.equal(input=tensor1, other=tensor2)
torch.equal(input=tensor2, other=tensor1)
# False

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

torch.equal(input=tensor1, other=tensor2)
torch.equal(input=tensor2, other=tensor1)
# False
Enter fullscreen mode Exit fullscreen mode

eq() can check the zero or more elements of the 1st 0D or more D tensor are equal to the zero or more elements of the 2nd 0D or more D tensor element-wise as shown below:

*Memos:

  • eq() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float, complex or bool) with torch or using a tensor(tensor of int, float, complex or bool) is input(Required).
  • The 2nd argument(tensor of int, float, complex or bool or int, float, complex or bool) with torch or the 1st argument with a tensor(tensor of int, float, complex or bool or int, float, complex or bool) is other(Required).
  • The result is the higher D tensor which has more elements.
import torch

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

torch.eq(input=tensor1, other=3)
# tensor(False)

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

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

torch.eq(input=tensor1, other=3)
torch.eq(input=tensor1, other=3.)
torch.eq(input=tensor1, other=3+0j)
# tensor([False, False, True])

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

torch.eq(input=tensor2, other=3)
torch.eq(input=tensor2, other=3.)
torch.eq(input=tensor2, other=3+0j)
# tensor([[False, False, False],
#         [False, False, False],
#         [True, True, True]])

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

tensor1 = torch.tensor([5., 0., 3.])
tensor2 = torch.tensor([[5+0j, 5+0j, 5+0j],
                        [0+0j, 0+0j, 0+0j],
                        [3+0j, 3+0j, 3+0j]])
torch.eq(input=tensor1, other=tensor2)
torch.eq(input=tensor2, other=tensor1)
# tensor([[True, False, False],
#         [False, True, False], 
#         [False, False, True]])

tensor1 = torch.tensor([5+0j, 0+0j, 3+0j])
tensor2 = torch.tensor([[True, False, True],
                        [False, True, False],
                        [True, False, True]])
torch.eq(input=tensor1, other=tensor2)
torch.eq(input=tensor2, other=tensor1)
# tensor([[False, True, False],
#         [False, False, False],
#         [False, True, False]])

tensor1 = torch.tensor([[5, 0, 3], [6, 9, 1]])
tensor2 = torch.tensor([[[2, 7, 3], [6, 3, 1]],
                        [[5, 0, 4], [8, 9, 7]]])
torch.eq(input=tensor1, other=tensor2)
torch.eq(input=tensor2, other=tensor1)
# tensor([[[False, False, True], [True, False, True]],
#         [[True, True, False], [False, True, False]]])

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

torch.eq(input=tensor2, other=3)
# tensor([[[False, False, True], [False, True, False]],
#         [[False, False, False], [False, False, False]]])
Enter fullscreen mode Exit fullscreen mode

ne() can check the zero or more elements of the 1st 0D or more D tensor are not equal to the zero or more elements of the 2nd 0D or more D tensor element-wise as shown below:

*Memos:

  • ne() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float, complex or bool) with torch or using a tensor(tensor of int, float, complex or bool) is input(Required).
  • The 2nd argument(tensor of int, float, complex or bool or int, float, complex or bool) with torch or the 1st argument with a tensor(tensor of int, float, complex or bool or int, float, complex or bool) is other(Required).
  • The result is the higher D tensor which has more elements.
  • not_equal() is the alias of ne().
import torch

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

torch.ne(input=tensor1, other=3)
# tensor(True)

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

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

torch.ne(input=tensor1, other=3)
torch.ne(input=tensor1, other=3.)
torch.ne(input=tensor1, other=3+0j)
# tensor([True, True, False])

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

torch.ne(input=tensor2, other=3)
torch.ne(input=tensor2, other=3.)
torch.ne(input=tensor2, other=3+0j)
# tensor([[True, True, True],
#         [True, True, True],
#         [False, False, False]])

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

tensor1 = torch.tensor([5., 0., 3.])
tensor2 = torch.tensor([[5+0j, 5+0j, 5+0j],
                        [0+0j, 0+0j, 0+0j],
                        [3+0j, 3+0j, 3+0j]])
torch.ne(input=tensor1, other=tensor2)
torch.ne(input=tensor2, other=tensor1)
# tensor([[False, True, True],
#         [True, False, True],
#         [True, True, False]])

tensor1 = torch.tensor([5+0j, 0+0j, 3+0j])
tensor2 = torch.tensor([[True, False, True],
                        [False, True, False],
                        [True, False, True]])
torch.ne(input=tensor1, other=tensor2)
torch.ne(input=tensor2, other=tensor1)
# tensor([[True, False, True],
#         [True, True, True],
#         [True, False, True]])

tensor1 = torch.tensor([[5, 0, 3], [6, 9, 1]])
tensor2 = torch.tensor([[[2, 7, 3], [6, 3, 1]],
                        [[5, 0, 4], [8, 9, 7]]])
torch.ne(input=tensor1, other=tensor2)
torch.ne(input=tensor2, other=tensor1)
# tensor([[[True, True, False], [False, True, False]],
#         [[False, False, True], [True, False, True]]])

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

torch.ne(input=tensor2, other=3)
# tensor([[[True, True, False], [True, False, True]],
#         [[True, True, True], [True, True, True]]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)