Home > Article > Backend Development > isclose in PyTorch
Buy Me a Coffee☕
*Memos:
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:
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])
The above is the detailed content of isclose in PyTorch. For more information, please follow other related articles on the PHP Chinese website!