isclose and equal in PyTorch

Susan Sarandon
Release: 2024-11-05 18:54:02
Original
215 people have browsed it

isclose and equal in PyTorch

Buy Me a Coffee☕

*Memos:

  • My post explains eq() and ne().
  • My post explains gt() and lt().
  • My post explains ge() and le().
  • My post explains torch.nan and torch.inf.

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

  • isclose() 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 of int, float, complex or bool).
  • The 3rd argument with torch or the 2nd argument with a tensor is rtol(Optional-Default:1e-05-Type:float).
  • The 4th argument with torch or the 3rd argument with a tensor is atol(Optional-Default:1e-08-Type:float).
  • The 5th argument with torch or the 4th argument with a tensor is equal_nan(Optional-Default:False-Type:bool): *Memos:
    • If it's True, nan and nan return True.
    • Basically, nan and nan return False.
  • The formula is |input - other| <= rtol x |other| atol.
import torch

tensor1 = torch.tensor([1.00001001, 1.00000996, 1.00000995, torch.nan])
tensor2 = torch.tensor([1., 1., 1., torch.nan])

torch.isclose(input=tensor1, other=tensor2)
torch.isclose(input=tensor1, other=tensor2,
              rtol=1e-05, atol=1e-08, equal_nan=False)
            # 0.00001   # 0.00000001
tensor1.isclose(other=tensor2)
torch.isclose(input=tensor2, other=tensor1)
# tensor([False, False, True, False])

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

tensor1 = torch.tensor([[1.00001001, 1.00000996],
                        [1.00000995, torch.nan]])
tensor2 = torch.tensor([[1., 1.],
                        [1., torch.nan]])
torch.isclose(input=tensor1, other=tensor2)
torch.isclose(input=tensor2, other=tensor1)
# tensor([[False, False],
#         [True, False]])

tensor1 = torch.tensor([[[1.00001001],
                         [1.00000996]],
                        [[1.00000995],
                         [torch.nan]]])
tensor2 = torch.tensor([[[1.], [1.]],
                        [[1.], [torch.nan]]])
torch.isclose(input=tensor1, other=tensor2)
torch.isclose(input=tensor2, other=tensor1)
# tensor([[[False], [False]],
#         [[True], [False]]])

tensor1 = torch.tensor([[1.00001001, 1.00000996],
                        [1.00000995, torch.nan]])
tensor2 = torch.tensor([1., 1.])

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

tensor1 = torch.tensor([[1.00001001, 1.00000996],
                        [1.00000995, torch.nan]])
tensor2 = torch.tensor(1.)

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

tensor1 = torch.tensor([0, 1, 2])
tensor2 = torch.tensor(1)

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

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

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

tensor1 = torch.tensor([False, True, False])
tensor2 = torch.tensor(True)

torch.isclose(input=tensor1, other=tensor2)
# tensor([False, True, False])
Copy after login

equal() can check if two of 0D or more D tensors have the same size and elements, getting the scalar of a boolean value as shown below:

*Memos:

  • equal() 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 of int, float, complex or bool).
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)
# 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

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

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

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

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

tensor1 = torch.tensor([], dtype=torch.int64)
tensor2 = torch.tensor([], dtype=torch.float32)

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

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

source:dev.to
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!