Home  >  Article  >  Backend Development  >  eq and ne in PyTorch

eq and ne in PyTorch

Susan Sarandon
Susan SarandonOriginal
2024-11-05 19:12:02653browse

eq and ne in PyTorch

Buy Me a Coffee☕

*Memos:

  • My post explains gt() and lt().
  • My post explains ge() and le().
  • My post explains isclose() and equal().

eq() can check if 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, getting the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • eq() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float, complex or bool).
  • The 2nd argument with torch or the 1st argument with a tensor is other(Required-Type:tensor or scalar of int, float, complex or bool).
  • There is out argument with torch(Optional-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • The result is the higher D tensor which has more elements.
import torch

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

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

tensor1 = torch.tensor(5)
tensor2 = torch.tensor([[3, 5, 4],
                        [6, 3, 5]])
torch.eq(input=tensor1, other=tensor2)
torch.eq(input=tensor2, 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)
# tensor([False, False, True])

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

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

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

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

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

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

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

ne() can check if 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, getting the 0D or more D tensor of zero or more elements as shown below:

*Memos:

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

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

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

tensor1 = torch.tensor(5)
tensor2 = torch.tensor([[3, 5, 4],
                        [6, 3, 5]])
torch.ne(input=tensor1, other=tensor2)
torch.ne(input=tensor2, 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)
# tensor([True, True, False])

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

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

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

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

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

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

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

The above is the detailed content of eq and ne in PyTorch. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn