PHP前端开发

PyTorch 中的接近和相等

百变鹏仔 5天前 #Python
文章标签 PyTorch

请我喝杯咖啡☕

*备忘录:

isclose() 可以检查第一个 0d 或更多 d 张量的零个或多个元素是否等于或接近等于第二个 0d 或更多 d 张量的零个或多个元素,得到 0d 或更多零个或多个元素的 d 张量如下所示:

*备忘录:

  • 公式为 |输入 - 其他|
  • import torchtensor1 = 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.00000001tensor1.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 torchtensor1 = 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)# Truetensor1 = torch.tensor([5, 9, 3])tensor2 = torch.tensor([7, 9, 3])torch.equal(input=tensor1, other=tensor2)torch.equal(input=tensor2, other=tensor1)# Falsetensor1 = torch.tensor([5, 9, 3])tensor2 = torch.tensor([[5, 9, 3]])torch.equal(input=tensor1, other=tensor2)torch.equal(input=tensor2, other=tensor1)# Falsetensor1 = 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)# Truetensor1 = 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)# Truetensor1 = torch.tensor([], dtype=torch.int64)tensor2 = torch.tensor([], dtype=torch.float32)torch.equal(input=tensor1, other=tensor2)torch.equal(input=tensor2, other=tensor1)# True