請我喝杯咖啡☕
*備忘錄:
isclose() 可以檢查第一個0D 或更多D 張量的零個或多個元素是否等於或接近等於第二個0D 或更多D 張量的零個或多個元素,得到0D或更多零個或多個元素的D 張量如下所示:
*備忘錄:
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])
equal() 可以檢查兩個 0D 或更多 D 張量是否具有相同的大小和元素,得到布林值的標量,如下所示:
*備忘錄:
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
以上是PyTorch 中的接近和相等的詳細內容。更多資訊請關注PHP中文網其他相關文章!