Compute element-wise with logical OR
torch.logical_or() – This method is used to compute the element-wise logical OR of the given tensor. This method also treated the non-zero values as True and zero values as False. The following syntax is used to compute logical OR.
Syntax: torch.logical_or(input, other)
Parameters
- input – This is our input tensor
- other – This tensor is to compute OR with input tensor.
Return –returns a tensor with values we get after computing the logical OR.
Example 1:
The following program is to know how to compute element-wise logical OR on two 1D tensors.
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) |
Output:
Example 2:
The following program is to understand how to compute element-wise logical OR on two 2D tensors.
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) |
Output:
Compute element-wise logical AND, OR and NOT of tensors in PyTorch
In this article, we are going to see how to compute element-wise logical AND, OR, and NOT of given tensors in PyTorch. We can compute this by using the torch.logical_and(), torch.logical_or(), and torch.logical_not() methods. Let’s discuss all of them one by one.