Python

# PyTorch – all()

“In this PyTorch tutorial, we will check if the elements in the tensor evaluate to True using the all() 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.all()

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

It takes one parameter.

Syntax:

torch.all(tensor_object)

Parameter:

tensor_object is a tensor.

## Example 1

In this example, we will create a tensor with one dimension that has 5 elements and apply all() 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("Do all elements in a tensor are not equal to 0? ")

#all() in pytorch

print(torch.all(data1))

Output:

Actual elements in the Tensor:

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

Do all elements in a tensor are not equal to 0?

tensor(False)

Here, we can find 0 in the 4th position. So all() returned False.

## Example 2

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

#first import the torch module

import torch

#create a 1D tensor

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

#display

print("Actual elements in the Tensor: ")

print(data1)

print("Do all elements in a tensor are not equal to 0? ")

#all() in pytorch

print(torch.all(data1))

Output:

Actual elements in the Tensor:

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

Do all elements in a tensor are not equal to 0?

tensor(True)

Here, we can’t find 0 in the tensor. So all() returned True.

## Example 3

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

#first import the torch module

import torch

#create a 1D tensor

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

#display

print("Actual elements in the Tensor: ")

print(data1)

print("Do all elements in a tensor are not equal to False? ")

#all() in pytorch

print(torch.all(data1))

Output:

Actual elements in the Tensor:

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

Do all elements in a tensor are not equal to False?

tensor(False)

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

## Work With CPU

If you want to run an all() 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 all() 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("Do all elements in a tensor are not equal to 0? ")

#all() in pytorch

print(torch.all(data1))

Output:

Actual elements in the Tensor:

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

Do all elements in a tensor are not equal to 0?

tensor(False)

Here, we can find 0 in the 4th position. So all() returned False.

## Example 2

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

#first import the torch module

import torch

#create a 1D tensor

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

#display

print("Actual elements in the Tensor: ")

print(data1)

print("Do all elements in a tensor are not equal to 0? ")

#all() in pytorch

print(torch.all(data1))

Output:

Actual elements in the Tensor:

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

Do all elements in a tensor are not equal to 0?

tensor(True)

Here, we can’t find 0 in the tensor. So all() returned True.

## Example 3

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

#first import the torch module

import torch

#create a 1D tensor

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

#display

print("Actual elements in the Tensor: ")

print(data1)

print("Do all elements in a tensor are not equal to False? ")

#all() in pytorch

print(torch.all(data1))

Output:

Actual elements in the Tensor:

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

Do all elements in a tensor are not equal to False?

tensor(False)

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

## Conclusion

In this PyTorch lesson, we discussed the all() function. It returns True if the values in a tensor are not equal to 0 or False. If any of 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.