Python

PyTorch – any()

“In this PyTorch tutorial, we will check if any of the elements in the tensor evaluate to True using the any() method.

PyTorch is an open-source framework available with a Python programming language.

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.any()

torch.any() in PyTorch returns True if at least one element in a tensor is not equal to 0 or False. If all the values in a tensor are equal to 0 or False, it will return False.

It takes one parameter.

Syntax:

torch.isreal(tensor_object)

Parameter:

tensor_object is a tensor.

Example 1

In this example, we will create a tensor with one dimension with 5 elements and apply any() function to check the functionality.

#first import the torch module

import torch

 

#create a 1D tensor

data1 = torch.tensor([2,3,0,3,4])

 

#display

print("Actual elements in the Tensor: ")

print(data1)

 

print("Any of the elements in a tensor not equal to 0? ")

#any() in pytorch

print(torch.any(data1))

Output:

Actual elements in the Tensor:

tensor([2, 3, 0, 3, 4])

Are any of the elements in a tensor not equal to 0?

tensor(True)

Here, we can find 2,3,3 and 4 non-zero elements present in the tensor. So any() returned True.

Example 2

In this example, we will create a tensor with one dimension that has 5 elements and apply any() function to check the functionality.

#first import the torch module

import torch

 

#create a 1D tensor

data1 = torch.tensor([0,0,0,0,0])

 

#display

print("Actual elements in the Tensor: ")

print(data1)

 

print("Any of the elements in a tensor not equal to 0? ")

#any() in pytorch

print(torch.any(data1))

Output:

Actual elements in the Tensor:

tensor([0, 0, 0, 0, 0])

Are any of the elements in a tensor not equal to 0?

tensor(False)

Here, we can find all zero elements in the tensor. So any() returned False.

Example 3

In this example, we will create a tensor with one dimension that has 5 boolean elements and apply any() function to check the functionality.

#first import the torch module

import torch

 

#create a 1D tensor

data1 = torch.tensor([True,False,False,False,False])

 

#display

print("Actual elements in the Tensor: ")

print(data1)

 

print("Any of the elements in a tensor not equal to False? ")

#any() in pytorch

print(torch.any(data1))

Output:

Actual elements in the Tensor:

tensor([ True, False, False, False, False])

Are any of the elements in a tensor not equal to False?

tensor(True)

Here, we can find at least one True in the tensor. So the any() returned True.

Work With CPU

If you want to run any() function on the CPU, then we have to create a tensor with a cpu() function. This will run on a CPU machine.

When we are creating a tensor, at this time, we can use the cpu() function.

Syntax:

torch.tensor(data).cpu()

Example 1

In this example, we will create a tensor with one dimension that has 5 elements on the cpu and apply any() function to check the functionality.

#first import the torch module

import torch

 

#create a 1D tensor

data1 = torch.tensor([2,3,0,3,4]).cpu()

 

#display

print("Actual elements in the Tensor: ")

print(data1)

 

print("Any of the elements in a tensor not equal to 0? ")

#any() in pytorch

print(torch.any(data1))

Output:

Actual elements in the Tensor:

tensor([2, 3, 0, 3, 4])

Are any of the elements in a tensor not equal to 0?

tensor(True)

Here, we can find 2,3,3 and 4 non-zero elements present in the tensor. So any() returned True.

Example 2

In this example, we will create a tensor with one dimension that has 5 elements on the cpu and apply any() function to check the functionality.

#first import the torch module

import torch

 

#create a 1D tensor

data1 = torch.tensor([0,0,0,0,0]).cpu()

 

#display

print("Actual elements in the Tensor: ")

print(data1)

 

print("Any of the elements in a tensor not equal to 0? ")

#any() in pytorch

print(torch.any(data1))

Output:

Actual elements in the Tensor:

tensor([0, 0, 0, 0, 0])

Are any of the elements in a tensor not equal to 0?

tensor(False)

Here, we can find all zero elements in the tensor. So any() returned False.

Example 3

In this example, we will create a tensor with one dimension that has 5 boolean elements on the cpu and apply any() function to check the functionality.

#first import the torch module

import torch

 

#create a 1D tensor

data1 = torch.tensor([True,False,False,False,False]).cpu()

 

#display

print("Actual elements in the Tensor: ")

print(data1)

 

print("Any of the elements in a tensor not equal to False? ")

#any() in pytorch

print(torch.any(data1))

Output:

Actual elements in the Tensor:

tensor([ True, False, False, False, False])

Are any of the elements in a tensor not equal to False?

tensor(True)

Here, we can find at least one True in the tensor. So the any() returned True.

Conclusion

In this PyTorch lesson, we discussed any() function. It returns True if at least one element in a tensor is not equal to 0 or False. If all the values in a tensor are equal to 0 or False, it will return False. We saw 3 different examples and also worked on these examples on a cpu machine.

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