請我喝杯咖啡☕
*備忘錄:
eq() 可以檢查第一個0D 或更多D 張量的零個或多個元素是否等於第二個0D 或更多D 張量的零個或多個元素,得到0D 或更多D 張量零個或多個元素,如下所示:
*備忘錄:
import torch tensor1 = 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 torch tensor1 = 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])
以上是PyTorch 中的 eq 和 ne的詳細內容。更多資訊請關注PHP中文網其他相關文章!