PyTorch 中的 eq 和 ne
请我喝杯咖啡☕
*备忘录:
eq() 可以检查第一个 0d 或更多 d 张量的零个或多个元素是否等于第二个 0d 或更多 d 张量的零个或多个元素,得到 0d 或更多 d 张量零个或多个元素,如下所示:
*备忘录:
import torchtensor1 = torch.tensor([5, 0, 3])tensor2 = torch.tensor([7, 0, 3])torch.eq(input=tensor1, other=tensor2)tensor1.eq(other=tensor2)torch.eq(input=tensor2, other=tensor1)# tensor([false, true, true])tensor1 = torch.tensor(5)tensor2 = torch.tensor([[3, 5, 4], [6, 3, 5]])torch.eq(input=tensor1, other=tensor2)torch.eq(input=tensor2, other=tensor1)# tensor([[false, true, false],# [false, false, true]])torch.eq(input=tensor1, other=3)# tensor(false)torch.eq(input=tensor2, other=3)# tensor([[true, false, false],# [false, true, false]])tensor1 = torch.tensor([5, 0, 3])tensor2 = torch.tensor([[5, 5, 5], [0, 0, 0], [3, 3, 3]])torch.eq(input=tensor1, other=tensor2)torch.eq(input=tensor2, other=tensor1)# tensor([[true, false, false],# [false, true, false], # [false, false, true]])torch.eq(input=tensor1, other=3)# tensor([false, false, true])torch.eq(input=tensor2, other=3)# tensor([[false, false, false],# [false, false, false],# [true, true, true]])tensor1 = torch.tensor([5., 0., 3.])tensor2 = torch.tensor([[5., 5., 5.], [0., 0., 0.], [3., 3., 3.]])torch.eq(input=tensor1, other=tensor2)# tensor([[true, false, false],# [false, true, false], # [false, false, true]])torch.eq(input=tensor1, other=3.)# tensor([false, false, true])tensor1 = torch.tensor([5.+0.j, 0.+0.j, 3.+0.j])tensor2 = torch.tensor([[5.+0.j, 5.+0.j, 5.+0.j], [0.+0.j, 0.+0.j, 0.+0.j], [3.+0.j, 3.+0.j, 3.+.0j]])torch.eq(input=tensor1, other=tensor2)# tensor([[true, false, false],# [false, true, false],# [false, false, true]])torch.eq(input=tensor1, other=3.+0.j)# tensor([false, false, true])tensor1 = torch.tensor([true, false, true])tensor2 = torch.tensor([[true, false, true], [false, true, false], [true, false, true]])torch.eq(input=tensor1, other=tensor2)# tensor([[true, true, true],# [false, false, false],# [true, true, true]])torch.eq(input=tensor1, other=true)# tensor([true, false, true])
ne() 可以按元素检查第一个 0d 或更多 d 张量的零个或多个元素是否不等于第二个 0d 或更多 d 张量的零个或多个元素,得到 0d 或更多 d 张量零个或多个元素,如下所示:
*备忘录:
import torchtensor1 = torch.tensor([5, 0, 3])tensor2 = torch.tensor([7, 0, 3])torch.ne(input=tensor1, other=tensor2)tensor1.ne(other=tensor2)torch.ne(input=tensor2, other=tensor1)# tensor([True, False, False])tensor1 = torch.tensor(5)tensor2 = torch.tensor([[3, 5, 4], [6, 3, 5]])torch.ne(input=tensor1, other=tensor2)torch.ne(input=tensor2, other=tensor1)# tensor([[True, False, True],# [True, True, False]])torch.ne(input=tensor1, other=3)# tensor(True)torch.ne(input=tensor2, other=3)# tensor([[False, True, True],# [True, False, True]])tensor1 = torch.tensor([5, 0, 3])tensor2 = torch.tensor([[5, 5, 5], [0, 0, 0], [3, 3, 3]])torch.ne(input=tensor1, other=tensor2)torch.ne(input=tensor2, other=tensor1)# tensor([[False, True, True],# [True, False, True],# [True, True, False]])torch.ne(input=tensor1, other=3)# tensor([True, True, False])torch.ne(input=tensor2, other=3)# tensor([[True, True, True],# [True, True, True],# [False, False, False]])tensor1 = torch.tensor([5., 0., 3.])tensor2 = torch.tensor([[5., 5., 5.], [0., 0., 0.], [3., 3., 3.]])torch.ne(input=tensor1, other=tensor2)# tensor([[False, True, True],# [True, False, True],# [True, True, False]])torch.ne(input=tensor1, other=3.)# tensor([True, True, False])tensor1 = torch.tensor([5.+0.j, 0.+0.j, 3.+0.j])tensor2 = torch.tensor([[5.+0.j, 5.+0.j, 5.+0.j], [0.+0.j, 0.+0.j, 0.+0.j], [3.+0.j, 3.+0.j, 3.+.0j]])torch.ne(input=tensor1, other=tensor2)# tensor([[False, True, True],# [True, False, True],# [True, True, False]])torch.ne(input=tensor1, other=3.+0.j)# tensor([True, True, False])tensor1 = torch.tensor([True, False, True])tensor2 = torch.tensor([[True, False, True], [False, True, False], [True, False, True]])torch.ne(input=tensor1, other=tensor2)# tensor([[False, False, False],# [True, True, True],# [False, False, False]])torch.ne(input=tensor1, other=True)# tensor([False, True, False])