如何在 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