📜  在 PyTorch 中计算张量的逐元素逻辑 AND、OR 和 NOT

📅  最后修改于: 2022-05-13 01:55:23.856000             🧑  作者: Mango

在 PyTorch 中计算张量的逐元素逻辑 AND、OR 和 NOT

在本文中,我们将了解如何在 PyTorch 中计算给定张量的元素逻辑 AND、OR 和 NOT。我们可以使用 torch.logical_and()、torch.logical_or() 和 torch.logical_not() 方法来计算它。让我们一一讨论。

使用逻辑 AND 逐元素计算

torch.logical_and() - 此方法用于计算给定张量的元素逻辑与。此方法将非零值视为 True,将零值视为 False。以下语法用于计算逻辑 AND。

示例 1:

下面的程序是计算两个具有布尔值的一维张量的元素逻辑与。

Python3
# Import the required library
import torch
  
# create two tensors having boolean values
tens_1 = torch.tensor([True, True, False, False])
tens_2 = torch.tensor([True, False, True, False])
  
# display the above created tensors
print("Input Tensor 1: ", tens_1)
print("Input Tensor 2: ", tens_2)
  
# compute the logical AND of input1 and input2
tens = torch.logical_and(tens_1, tens_2)
  
# print result
print("\nAfter Compute Logical AND: ", tens)


Python3
# Import the required library
import torch
  
# create two tensors
tens_1 = torch.tensor([[10, 0], [0, 20]])
tens_2 = torch.tensor([[0, 30], [0, 40]])
  
# display the tensors
print("Input Tensor 1: \n", tens_1)
print("Input Tensor 2: \n", tens_2)
  
# compute the logical AND
tens = torch.logical_and(tens_1, tens_2)
  
# print result
print("After Compute Logical AND: \n", tens)


Python3
# Import the required library
import torch
  
# create two tensors
tens_1 = torch.tensor([[10, 0, 20, 0]])
tens_2 = torch.tensor([[0, 30, 40, 0]])
  
# display the tensors
print("\n Input Tensor 1: ", tens_1)
print("\n Input Tensor 2: ", tens_2)
  
# compute the logical OR
tens = torch.logical_or(tens_1, tens_2)
  
# print result
print("\n After Compute Logical OR: ", tens)


Python3
# Import the required library
import torch
  
# create two tensors
tens_1 = torch.tensor([[11, 0], [0, 12]])
tens_2 = torch.tensor([[0, 13], [0, 14]])
  
# display the tensors
print("\n Input Tensor 1: \n", tens_1)
print("\n Input Tensor 2: \n", tens_2)
  
# compute the logical OR
tens = torch.logical_or(tens_1, tens_2)
  
# print result
print("\n After Compute Logical OR: \n", tens)


Python3
# import required library
import torch
  
# create two tensors
tens_1 = torch.tensor([11, 0])
  
  
# display the tensors
print("\n Input Tensor 1: \n", tens_1)
  
# compute the logical NOT
tens = torch.logical_not(tens_1)
  
# display result
print("\n After Compute Logical NOT: \n", tens)


输出:

示例 2:

以下程序用于了解如何在两个 2D 张量上计算元素逻辑与。

Python3

# Import the required library
import torch
  
# create two tensors
tens_1 = torch.tensor([[10, 0], [0, 20]])
tens_2 = torch.tensor([[0, 30], [0, 40]])
  
# display the tensors
print("Input Tensor 1: \n", tens_1)
print("Input Tensor 2: \n", tens_2)
  
# compute the logical AND
tens = torch.logical_and(tens_1, tens_2)
  
# print result
print("After Compute Logical AND: \n", tens)

输出:

使用逻辑 OR 按元素计算

torch.logical_or() -此方法用于计算给定张量的元素逻辑或。此方法还将非零值视为 True,将零值视为 False。以下语法用于计算逻辑 OR。

示例 1:

以下程序将了解如何在两个 1D 张量上计算元素逻辑或。

Python3

# Import the required library
import torch
  
# create two tensors
tens_1 = torch.tensor([[10, 0, 20, 0]])
tens_2 = torch.tensor([[0, 30, 40, 0]])
  
# display the tensors
print("\n Input Tensor 1: ", tens_1)
print("\n Input Tensor 2: ", tens_2)
  
# compute the logical OR
tens = torch.logical_or(tens_1, tens_2)
  
# print result
print("\n After Compute Logical OR: ", tens)

输出:

示例 2:

以下程序用于了解如何在两个 2D 张量上计算元素逻辑或。

Python3

# Import the required library
import torch
  
# create two tensors
tens_1 = torch.tensor([[11, 0], [0, 12]])
tens_2 = torch.tensor([[0, 13], [0, 14]])
  
# display the tensors
print("\n Input Tensor 1: \n", tens_1)
print("\n Input Tensor 2: \n", tens_2)
  
# compute the logical OR
tens = torch.logical_or(tens_1, tens_2)
  
# print result
print("\n After Compute Logical OR: \n", tens)

输出:

用逻辑非计算元素

torch.logical_not() -此方法用于计算给定输入张量的元素逻辑非。此方法还将非零值视为 True,将零值视为 False。以下语法用于计算逻辑非。

下面的程序是为了理解如何计算张量的元素逻辑非。

Python3

# import required library
import torch
  
# create two tensors
tens_1 = torch.tensor([11, 0])
  
  
# display the tensors
print("\n Input Tensor 1: \n", tens_1)
  
# compute the logical NOT
tens = torch.logical_not(tens_1)
  
# display result
print("\n After Compute Logical NOT: \n", tens)

输出: