Logical AND 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_and()
torch.logical_and() in PyTorch is performed on two tensor objects. It will perform an element-wise comparison and return True if both the elements are True or greater than 0 and return False if either of the elements is 0 or False. 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_and().
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_and on data1 and data2
print("Logical AND on above two tensors: ",torch.logical_and(data1,data2))
Output:
Second Tensor: tensor([False, False, True, False, True])
Logical AND on above two tensors: tensor([False, False, True, False, False])
Working:
- logical_and(False ,False) – False
- logical_and(True , False) – False
- logical_and(True , True) – False
- logical_and(True , False) – True
- logical_and(False , True) – False
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_and().
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_and on data1 and data2
print("Logical AND on above two tensors: ",torch.logical_and(data1,data2))
Output:
[False, True, True, True, False]])
Second Tensor: tensor([[False, False, True, False, True],
[False, False, True, False, True]])
Logical AND on above two tensors: tensor([[False, False, True, False, False],[False, False, True, False, False]])
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_and().
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_and on data1 and data2
print("Logical AND on above two tensors: ",torch.logical_and(data1,data2))
Output:
[12, 21, 34, 56, 78]])
Second Tensor: tensor([[ 0, 0, 55, 78, 23],
[10, 20, 44, 56, 0]])
Logical AND on above two tensors: tensor([[False, False, True, False, False],[ True, True, True, True, False]])
Working:
- logical_and(23 ,0) – False, logical_and(12 ,10) – True
- logical_and(45 , 0) – False, logical_and(21 ,20) – True
- logical_and(67 , 55) – False, logical_and(34 ,44) – True
- logical_and(0 , 78) – True, logical_and(56 ,56) – True
- logical_and(0 , 23) – False, logical_and(78 ,0) – False
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_and().
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_and on data1 and data2
print("Logical AND on above two tensors: ",torch.logical_and(data1,data2))
Output:
[ 0, 1, 1, 1, 0]])
Second Tensor: tensor([[ 0, 0, 55, 78, 23],
[ 0, 1, 1, 1, 0]])
Logical AND on above two tensors: tensor([[False, False, True, False, False],
[False, True, True, True, False]])
Working:
- logical_and(23 ,0) – False, logical_and(0,0) – False
- logical_and(45 , 0) – False, logical_and(1 ,1) – True
- logical_and(67 , 55) – False, logical_and(1 ,1) – True
- logical_and(0 , 78) – True, logical_and(1 ,1) – True
- logical_and(0 , 23) – False, logical_and(0 ,0) – False
Conclusion
In this PyTorch lesson, we discussed how to perform logical AND operation with a torch.logical_and() method. It will perform an element-wise comparison and return True if both the elements are True or greater than 0 and return False if either of the elements is 0 or False. We saw the functionality of logical and numerical data.
Source: linuxhint.com