Logical OR in PyTorch
PyTorch is an open-source framework available with a Python programming language. We can process the data in PyTorch in the form of a Tensor.
A tensor is a multidimensional array that is used to store the data. So for using a Tensor, we have to import the torch module.
To create a tensor, the method used is tensor()”
Syntax:
Where data is a multi-dimensional array.
torch.logical_or()
torch.logical_or() in PyTorch is performed on two tensor objects. It will perform an element-wise comparison and return True; either of the elements is True or greater than 0 and return False if both the elements are False or 0. It takes two tensors as parameters.
Syntax:
Parameters:
- tensor_object1 is the first tensor
- tensor_object2 is the second tensor
Example 1
In this example, we will create two one-dimensional tensors – data1 and data2 with 5 boolean values each and perform logical_or().
import torch
#create a 1D tensor - data1 with 5 boolean values
data1 = torch.tensor([False,True, True, True,False])
#create a 1D tensor - data2 with 5 boolean values
data2 = torch.tensor([False,False, True, False,True])
#display
print("First Tensor: ",data1)
print("Second Tensor: ",data2)
#logical_or on data1 and data2
print("Logical OR on above two tensors: ",torch.logical_or(data1,data2))
Output:
Second Tensor: tensor([False, False, True, False, True])
Logical OR on above two tensors: tensor([False, True, True, True, True])
Working:
- logical_or(False ,False) – False
- logical_or(True , False) – True
- logical_or(True , True) – True
- logical_or(True , False) – True
- logical_or(False , True) – True
Example 2
In this example, we will create two-dimensional tensors – data1 and data2 with 5 boolean values each in a row and perform logical_or().
import torch
#create a 2D tensor - data1 with 5 boolean values in each row
data1 = torch.tensor([[False,True, True, True,False],[False,True, True, True,False]])
#create a 2D tensor - data2 with 5 boolean values in each row
data2 = torch.tensor([[False,False, True, False,True],[False,False, True, False,True]])
#display
print("First Tensor: ",data1)
print("Second Tensor: ",data2)
#logical_or on data1 and data2
print("Logical OR on above two tensors: ",torch.logical_or(data1,data2))
Output:
[False, True, True, True, False]])
Second Tensor: tensor([[False, False, True, False, True],
[False, False, True, False, True]])
Logical OR on above two tensors: tensor([[False, True, True, True, True],[False, True, True, True, True]])
Example 3
In this example, we will create two-dimensional tensors – data1 and data2 with 5 numeric values each in a row and perform logical_or().
import torch
#create a 2D tensor - data1 with 5 numeric values in each row
data1 = torch.tensor([[23,45,67,0,0],[12,21,34,56,78]])
#create a 2D tensor - data2 with 5 numeric values in each row
data2 = torch.tensor([[0,0,55,78,23],[10,20,44,56,0]])
#display
print("First Tensor: ",data1)
print("Second Tensor: ",data2)
#logical_or on data1 and data2
print("Logical OR on above two tensors: ",torch.logical_or(data1,data2))
Output:
[12, 21, 34, 56, 78]])
Second Tensor: tensor([[ 0, 0, 55, 78, 23],
[10, 20, 44, 56, 0]])
Logical OR on above two tensors: tensor([[True, True, True, True, True],
[True, True, True, True, True]])
Working:
- logical_or(23 ,0) – True, logical_or(12 ,10) – True
- logical_or(45 , 0) – True, logical_or(21 ,20) – True
- logical_or(67 , 55) – True, logical_or(34 ,44) – True
- logical_or(0 , 78) – True, logical_or(56 ,56) – True
- logical_or(0 , 23) – True, logical_or(78 ,0) – True
Example 4
In this example, we will create two-dimensional tensors – data1 and data2 with 5 numeric & logical values each in a row and perform logical_or().
Here it considers True as 1 and false as 0.
import torch
#create a 2D tensor - data1 with 5 numeric & logical values in each row
data1 = torch.tensor([[23,45,67,0,0],[False, True, True, True, False]])
#create a 2D tensor - data2 with 5 numeric & logical values in each row
data2 = torch.tensor([[0,0,55,78,23],[False, True, True, True, False]])
#display
print("First Tensor: ",data1)
print("Second Tensor: ",data2)
#logical_or on data1 and data2
print("Logical OR on above two tensors: ",torch.logical_or(data1,data2))
Output:
[ 0, 1, 1, 1, 0]])
Second Tensor: tensor([[ 0, 0, 55, 78, 23],
[ 0, 1, 1, 1, 0]])
Logical OR on above two tensors: tensor([[ True, True, True, True, True],
[False, True, True, True, False]])
Working:
- logical_or(23 ,0) – True, logical_or(0,0) – False
- logical_or(45 , 0) – True, logical_or(1 ,1) – True
- logical_or(67 , 55) – True, logical_or(1 ,1) – True
- logical_or(0 , 78) – True, logical_or(1 ,1) – True
- logical_or(0 , 23) – True, logical_or(0 ,0) – False
Conclusion
In this PyTorch lesson, we discussed how to perform logical OR operation with a torch.logical_or() method. It will perform an element-wise comparison and return True; either of the elements is True or greater than 0 and return False if both the elements are False or 0. We saw the functionality of logical and numerical data.
Source: linuxhint.com