Python

PyTorch – isfinite()

PyTorch is an open-source framework for the Python programming language.

A tensor is a multidimensional array that is used to store the data. So to use 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.isfinite()

isfinite() in PyTorch returns True if the element is not infinity (for example, they should not be nan, positive infinity, and negative infinity). Otherwise, it returns False. It takes one parameter.

Syntax:
torch.isfinite(tensor_object)

Parameter:
tensor_object is a tensor.

Return:
It will return a boolean tensor with respect to the actual tensor.

Representation:

Positive Infinity  - float('inf')
Negative Infinity - float('-inf')
Not a number - float('nan’)

Example 1:
In this example, we will create a tensor with one dimension that has five elements and check if those five are finite or not.

#import torch module
import torch
 
#create a tensor
data1 = torch.tensor([12,34,56,1, float('inf')])
 
#display
print("Actual Tensor: ")
print(data1)

print("Check for Finite")
print(torch.isfinite(data1))

Output:

Actual Tensor:
tensor([12., 34., 56.,  1., inf])
Check for Finite
tensor([ True,  True,  True,  True, False])

Working:

  1. 12 is neither infinity nor nan, so it is Finite (True).
  2. 34 is neither infinity nor nan, so it is Finite (True).
  3. 56 is neither infinity nor nan, so it is Finite (True).
  4. 1 is neither infinity nor nan, so it is Finite (True).
  5. inf is infinity so it is not finite (False).

Example 2:
In this example, we will create a tensor with one dimension that has five elements and check if these five are finite or not.

#import torch module
import torch
 
#create a tensor
data1 = torch.tensor([float('-inf'),34,56,float('nan'), float('inf')])
 
#display
print("Actual Tensor: ")
print(data1)

print("Check for Finite")
print(torch.isfinite(data1))

Output:

Actual Tensor:
tensor([-inf, 34., 56., nan, inf])
Check for Finite
tensor([False,  True,  True, False, False])

Working:

  1. -inf is negative infinity, so it is not Finite (False).
  2. 34 is neither infinity nor nan, so it is Finite (True).
  3. 56 is neither infinity nor nan, so it is Finite (True).
  4. nan is not a number, so it is not finite (False).
  5. inf is infinity, so it is not finite (False).

Example 3:
In this example, we will create a tensor with two dimensions that has five elements in each row and check if these five are finite or not.

#import torch module
import torch
 
#create a 2D tensor
data1=torch.tensor([[float('-inf'),34,56,float('nan'), float('inf')],[float('-inf'),100,-4,float('nan'), float('inf')]])
#display
print("Actual Tensor: ")
print(data1)

print("Check for Finite")
print(torch.isfinite(data1))

Output:

Actual Tensor:
tensor([[-inf,  34.,  56.,  nan,  inf],
        [-inf, 100.,  -4.,  nan,  inf]])
Check for Finite
tensor([[False,  True,  True, False, False],
        [False,  True,  True, False, False]])

Working:

  1. -inf is negative infinity, so it is not finite (False) for both.
  2. 34 is neither infinity nor nan, so it is Finite (True). 100 is neither infinity nor nan, so it is Finite (True).
  3. 56 is neither infinity nor nan, so it is Finite (True). -4 is neither infinity nor nan, so it is Finite (True).
  4. nan is not a number, so it is not finite (False) for both.
  5. inf is infinity, so it is not finite (False) for both.

Work with CPU

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

At this time, when we are creating a tensor, 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 five elements on the CPU and check if these five are finite or not.

#import torch module
import torch
 
#create a tensor
data1 = torch.tensor([12,34,56,1, float(‘inf’)]).cpu()
 
#display
print(“Actual Tensor: “)
print(data1)

print(“Check for Finite”)
print(torch.isfinite(data1))

Output:

Actual Tensor:
tensor([12., 34., 56.,  1., inf])
Check for Finite
tensor([ True,  True,  True,  True, False])

Working:

  1. 12 is neither infinity nor nan, so it is Finite (True).
  2. 34 is neither infinity nor nan, so it is Finite (True).
  3. 56 is neither infinity nor nan, so it is Finite (True).
  4. 1 is neither infinity nor nan, so it is Finite (True).
  5. inf is infinity, so it is not finite (False).

Example 2:
In this example, we will create a tensor with one dimension that has five elements on the CPU and check if these five are finite or not.

#import torch module
import torch
 
#create a tensor
data1 = torch.tensor([float(‘-inf’),34,56,float(‘nan’), float(‘inf’)]).cpu()  
 
#display
print(“Actual Tensor: “)
print(data1)

print(“Check for Finite”)
print(torch.isfinite(data1))

Output:

Actual Tensor:
tensor([-inf, 34., 56., nan, inf])
Check for Finite
tensor([False,  True,  True, False, False])

Working:

  1. -inf is negative infinity, so it is not finite (False).
  2. 34 is neither infinity nor nan, so it is Finite (True).
  3. 56 is neither infinity nor nan, so it is Finite (True).
  4. nan is not a number, so it is not finite (False).
  5. inf is infinity, so it is not finite (False).

Example 3:
In this example, we will create a tensor with two dimensions that has five elements on the CPU in each row and check if these five are finite or not.

#import torch module
import torch
 
#create a 2D tensor
data1=torch.tensor([[float(‘-inf’),34,56,float(‘nan’), float(‘inf’)],[float(‘-inf’),100,-4,float(‘nan’), float(‘inf’)]]).cpu()  
#display
print(“Actual Tensor: “)
print(data1)

print(“Check for Finite”)
print(torch.isfinite(data1))

Output:

Actual Tensor:
tensor([[-inf,  34.,  56.,  nan,  inf],
        [-inf, 100.,  -4.,  nan,  inf]])
Check for Finite
tensor([[False,  True,  True, False, False],
        [False,  True,  True, False, False]])

Working:

  1. -inf is negative infinity, so it is not finite (False) for both.
  2. 34 is neither infinity nor nan, so it is Finite (True). 100 is neither infinity nor nan, so it is Finite (True).
  3. 56 is neither infinity nor nan, so it is Finite (True). -4 is neither infinity nor nan, so it is Finite (True).
  4. nan is not a number, so it is not finite (False) for both.
  5. inf is infinity, so it is not finite (False) for both.

Conclusion

In this PyTorch lesson, we discussed isfinite(). It returns True for the elements if the element is not infinity or nan. Otherwise, it returns False. The elements that come under the infinite category are -inf and inf.

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