Python

Logical AND in PyTorch

“In this PyTorch tutorial, we will see how to perform a logical AND operation on a tensor using logocal_and().

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:

torch.tensor(data)

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:

torch.logical_and(tensor_object1,tensor_object2)

Parameters:

  1. tensor_object1 is the first tensor
  2. 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 module

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:

First Tensor: tensor([False, True, True, True, False])

Second Tensor: tensor([False, False, True, False, True])

Logical AND on above two tensors: tensor([False, False, True, False, False])

Working:

  1. logical_and(False ,False) – False
  2. logical_and(True , False) – False
  3. logical_and(True , True) – False
  4. logical_and(True , False) – True
  5. 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 module

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:

First Tensor: tensor([[False, True, True, True, False],

[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 module

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:

First Tensor: tensor([[23, 45, 67, 0, 0],

[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:

  1. logical_and(23 ,0) – False, logical_and(12 ,10) – True
  2. logical_and(45 , 0) – False, logical_and(21 ,20) – True
  3. logical_and(67 , 55) – False, logical_and(34 ,44) – True
  4. logical_and(0 , 78) – True, logical_and(56 ,56) – True
  5. 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 module

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:

First Tensor: tensor([[23, 45, 67, 0, 0],

[ 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:

  1. logical_and(23 ,0) – False, logical_and(0,0) – False
  2. logical_and(45 , 0) – False, logical_and(1 ,1) – True
  3. logical_and(67 , 55) – False, logical_and(1 ,1) – True
  4. logical_and(0 , 78) – True, logical_and(1 ,1) – True
  5. 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.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain