如何在 PyTorch 中计算给定输入张量的按位与、或和非?
要计算给定输入张量的**按位与**,我们应用**torch.bitwise_and()**。输入张量必须为整数或布尔类型。对于**bool**张量,它计算**逻辑**与。
要计算给定输入张量的**按位非**,我们应用**torch.bitwise_not()**方法。输入张量必须为整数或布尔类型。对于**bool**张量,它计算**逻辑或**。
要计算给定输入张量的**按位非**,我们应用**torch.bitwise_not()**方法。输入张量必须为整数或布尔类型。对于**bool**张量,它计算**逻辑非**。
语法
torch.bitwise_and(input1, input2) torch.bitwise_or(input1, input2) torch.bitwise_not(input)
步骤
导入所需的库。在以下所有示例中,所需的 Python 库为**torch**。请确保您已安装它。
import torch
定义 torch 张量并打印它们。
input1 = torch.tensor([4, -2, 3, 0], dtype=torch.int8) input2 = torch.tensor([0, 1, -7, 2], dtype=torch.int8)
使用上面定义的语法计算按位与、或或非。
output = torch.bitwise_and(input1, input2)
打印计算出的张量。
print("Bitwise AND:
", output)示例 1
在以下 Python 程序中,我们计算给定输入张量的按位与。
# Python 3 program to compute bitwise AND of the given input tensors
# Import the required library
import torch
# define two tensors
input1 = torch.tensor([11, -21, 3], dtype=torch.int8)
input2 = torch.tensor([-2, 0, 3], dtype=torch.int8)
# display the above defined tensors
print("Input Tensor 1:
", input1)
print("Input Tensor 2:
", input2)
# compute the bitwise AND of input1 and input2
output = torch.bitwise_and(input1, input2)
# print above computed bitwise and tensor
print("Bitwise AND:
", output)
print(".................................")
# define two tensors
input1 = torch.tensor([True, True, False, False])
input2 = torch.tensor([False, True, False, True])
# display the above defined tensors
print("Input Tensor 1:
", input1)
print("Input Tensor 2:
", input2)
# compute the bitwise AND of input1 and input2
output = torch.bitwise_and(input1, input2)
# print above computed bitwise and tensor
print("Bitwise AND:
", output)输出
Input Tensor 1: tensor([ 11, -21, 3], dtype=torch.int8) Input Tensor 2: tensor([-2, 0, 3], dtype=torch.int8) Bitwise AND: tensor([10, 0, 3], dtype=torch.int8) ................................. Input Tensor 1: tensor([ True, True, False, False]) Input Tensor 2: tensor([False, True, False, True]) Bitwise AND: tensor([False, True, False, False])
示例 2
在此 Python 程序中,我们计算给定输入张量的按位或。
# Python 3 program to compute bitwise OR of the given input tensors
# Import the required library
import torch
# define two tensors
input1 = torch.tensor([11, -21, 3], dtype=torch.int8)
input2 = torch.tensor([-2, 0, 3], dtype=torch.int8)
# display the above defined tensors
print("Input Tensor 1:
", input1)
print("Input Tensor 2:
", input2)
# compute the bitwise AND of input1 and input2
output = torch.bitwise_or(input1, input2)
# print above computed bitwise and tensor
print("Bitwise OR:
", output)
print(".................................")
# define two tensors
input1 = torch.tensor([True, True, False, False])
input2 = torch.tensor([False, True, False, True])
# display the above defined tensors
print("Input Tensor 1:
", input1)
print("Input Tensor 2:
", input2)
# compute the bitwise AND of input1 and input2
output = torch.bitwise_or(input1, input2)
# print above computed bitwise and tensor
print("Bitwise OR:
", output)输出
Input Tensor 1: tensor([ 11, -21, 3], dtype=torch.int8) Input Tensor 2: tensor([-2, 0, 3], dtype=torch.int8) Bitwise OR: tensor([ -1, -21, 3], dtype=torch.int8) ................................. Input Tensor 1: tensor([ True, True, False, False]) Input Tensor 2: tensor([False, True, False, True]) Bitwise OR: tensor([ True, True, False, True])
示例 3
在此 Python 程序中,我们计算给定输入张量的按位非。
# Python 3 program to compute bitwise NOT of a given input tensor
# Import the required library
import torch
# define input tensors
input1 = torch.tensor([11, -21, 3], dtype=torch.int8)
# display the above defined tensors
print("Input Tensor 1:
", input1)
# compute the bitwise NOT
output1 = torch.bitwise_not(input1)
# print above computed bitwise NOT tensor
print("Bitwise NOT:
", output1)
# define input tensors
input2 = torch.tensor([False, True])
# display the above defined tensors
print("Input Tensor 2:
", input2)
# compute the bitwise NOT
output2 = torch.bitwise_not(input2)
# print above computed bitwise NOT tensor
print("Bitwise NOT:
", output2)输出
Input Tensor 1: tensor([ 11, -21, 3], dtype=torch.int8) Bitwise NOT: tensor([-12, 20, -4], dtype=torch.int8) Input Tensor 2: tensor([False, True]) Bitwise NOT: tensor([ True, False])
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP