
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Compute Elementwise Logical AND, OR, and NOT of Tensors in PyTorch
To compute elementwise logical AND of given input tensors we apply torch.logical_and(). It takes two input tensors and computes the logical AND element wise. The zeros in the tensors are treated as False and non-zeros as True. The input tensors may be of any dimension.
The torch.logical_or() function computes elementwise logical OR of the given input tensors. It also takes two input tensors and outputs a tensor with True or False. As same in logical AND zeros are treated as False and non-zeros are treated as True.The input tensors may be of any dimension.
To compute the elementwise NOT of a given input tensor we apply torch.logical_not() metod. This method takes a single input tensor and returns a tensor with a logical NOT of each element. Same as above, zeros are False and non-zeros as True.
Syntax
torch.logical_and(input1, input2) torch.logical_or(input1, input2) torch.logical_not(input)
Steps
Import the required library. In all the following examples, the required Python library is torch. Make sure you have already installed it.
import torch
Define torch tensors and print them.
input1 = torch.tensor([4, -2, 3, 0], dtype=torch.int8) input2 = torch.tensor([0, 1, -7, 2], dtype=torch.int8)
Compute logical AND, OR or NOT using above defined syntax.
output = torch.logical_and(input1, input2)
Print the computed tensor.
print("Logical AND:
", output)
Now let's take a couple of examples to demonstrate how to compute elementwise Logical AND, OR and NOT.
Example 1
In this Python program, we compute the element-wise logical AND.
# Python 3 program to compute element-wise # logical AND of the given input tensors # Import the required library import torch # define two tensors input1 = torch.tensor([4, -2, 3, 0], dtype=torch.int8) input2 = torch.tensor([0, 1, -7, 2], dtype=torch.int8) # display the above defined tensors print("Input Tensor 1:
", input1) print("Input Tensor 2:
", input2) # compute the logical AND of input1 and input2 output = torch.logical_and(input1, input2) # print above computed logical AND tensor print("Logical 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 logical AND of input1 and input2 output = torch.logical_and(input1, input2) # print above computed Logical AND tensor print("Logical AND:
", output)
Output
Input Tensor 1: tensor([ 4, -2, 3, 0], dtype=torch.int8) Input Tensor 2: tensor([ 0, 1, -7, 2], dtype=torch.int8) Logical AND: tensor([False, True, True, False]) ................................. Input Tensor 1: tensor([ True, True, False, False]) Input Tensor 2: tensor([False, True, False, True]) Logical AND: tensor([False, True, False, False])
Example 2
In this program, we compute the element-wise logical OR.
# Python 3 program to compute element-wise # logical OR of the given input tensors # Import the required library import torch # define two tensors input1 = torch.tensor([4, -2, 3, 0], dtype=torch.int8) input2 = torch.tensor([0, 1, -7, 0], dtype=torch.int8) # display the above defined tensors print("Input Tensor 1:
", input1) print("Input Tensor 2:
", input2) # compute the logical OR of input1 and input2 output = torch.logical_or(input1, input2) # print above computed logical OR tensor print("Logical 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 logical OR of input1 and input2 output = torch.logical_or(input1, input2) # print above computed Logical OR tensor print("Logical OR:
", output)
Output
Input Tensor 1: tensor([ 4, -2, 3, 0], dtype=torch.int8) Input Tensor 2: tensor([ 0, 1, -7, 0], dtype=torch.int8) Logical OR: tensor([ True, True, True, False]) ................................. Input Tensor 1: tensor([ True, True, False, False]) Input Tensor 2: tensor([False, True, False, True]) Logical OR: tensor([ True, True, False, True])
Example 3
In this program, we compute the element-wise logical NOT.
# Python program to compute logical NOT of a given input tensor # Import the required library import torch # define input tensors input1 = torch.tensor([11, -21, 0], dtype=torch.int8) # display the above defined tensors print("Input Tensor 1:
", input1) # compute the logical NOT output1 = torch.logical_not(input1) # print above computed logical NOT tensor print("Logical NOT:
", output1) # define input tensors input2 = torch.tensor([False, True]) # display the above defined tensors print("Input Tensor 2:
", input2) # compute the logical NOT output2 = torch.logical_not(input2) # print above computed logical NOT tensor print("Logical NOT:
", output2)
Output
Input Tensor 1: tensor([ 11, -21, 0], dtype=torch.int8) Logical NOT: tensor([False, False, True]) Input Tensor 2: tensor([False, True]) Logical NOT: tensor([ True, False])