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

equal() can check 2 tensors are the same:

*Memos:

  • equal() can be called both from torch and a tensor.
  • The tensors can be 0D or more D tensors.
import torch tensor1 = torch.tensor([5, 9, 0, 1])
tensor2 = torch.tensor([5, 9, 0, 1])
torch.equal(tensor1, tensor2)
tensor1.equal(tensor2)
torch.equal(tensor2, tensor1)
tensor2.equal(tensor1)
# True

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

tensor1 = torch.tensor([5, 9, 0, 1])
tensor2 = torch.tensor([7, 9, 0, 1])
torch.equal(tensor1, tensor2)
tensor1.equal(tensor2)
torch.equal(tensor2, tensor1)
tensor2.equal(tensor1)
# False

tensor1 = torch.tensor([5, 9, 0, 1])
tensor2 = torch.tensor([[5, 9, 0, 1]])
torch.equal(tensor1, tensor2)
tensor1.equal(tensor2)
torch.equal(tensor2, tensor1)
tensor2.equal(tensor1)
# False
Enter fullscreen mode
Exit fullscreen mode

eq() can check the equality of 2 tensors element-wise as shown below:

*Memos:

  • eq() can be called both from torch and a tensor.
  • The tensors can be 0D or more D tensors.
  • 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(tensor1, tensor2)
tensor1.eq(tensor2)
torch.eq(tensor2, tensor1)
tensor2.eq(tensor1)
# tensor([[False, True, False], [False, False, True]])

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

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

tensor1 = torch.tensor([[6, 9, 1], [5, 0, 3]])
tensor2 = torch.tensor([[[2, 7, 3], [6, 3, 1]], [[5, 0, 4], [8, 9, 7]]])
torch.eq(tensor1, tensor2)
tensor1.eq(tensor2)
torch.eq(tensor2, tensor1)
tensor2.eq(tensor1)
# tensor([[[False, False, False], [False, False, False]],
# [[False, False, False], [False, False, False]]])
Enter fullscreen mode
Exit fullscreen mode

ne() can check the inequality of 2 tensors element-wise as shown below:

*Memos:

  • ne() can be called both from torch and a tensor.
  • The tensors can be 0D or more D tensors.
  • 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.ne(tensor1, tensor2)
tensor1.ne(tensor2)
torch.ne(tensor2, tensor1)
tensor2.ne(tensor1)
# tensor([[True, False, True], [True, True, False]])

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

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

tensor1 = torch.tensor([[6, 9, 1], [5, 0, 3]])
tensor2 = torch.tensor([[[2, 7, 3], [6, 3, 1]], [[5, 0, 4], [8, 9, 7]]])
torch.ne(tensor1, tensor2)
tensor1.ne(tensor2)
torch.ne(tensor2, tensor1)
tensor2.ne(tensor1)
# tensor([[[True, True, True], [True, True, True]],
# [[True, True, True], [True, True, True]]])
Enter fullscreen mode
Exit fullscreen mode

Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top
Openprompt engineering tools : unleashing creativity. Mobile app development. However, if the viewport narrows significantly, the font size will decrease to maintain readability, not going below 16px.