Python

PyTorch – isnan()

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

A tensor is a multidimensional array that is used to store 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.isnan()

isnan() in PyTorch returns True for the elements if the element is nan(not a number). Otherwise, it returns False.

It takes one parameter.

Syntax:

torch.isnan(tensor_object)

Parameter:

tensor_object is a tensor.

Return:

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

Representation:

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 these five are nan or not.

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

Output:

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

Working:

  1. 12 is not nan (False).
  2. 34 is not nan (False).
  3. 56 is not nan (False).
  4. 1 is not nan (False).
  5. nan is not a number (True).

Example 2:

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

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

Output:

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

Working:

  1. -nan is not a number (True).
  2. 34 is not nan (False).
  3. 56 is not nan (False).
  4. nan is not a number (True).
  5. inf is infinity. It is not nan (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 nan 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 nan")
print(torch.isnan(data1))

Output:

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

Working:

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

Work with CPU

If you want to run an isnan() 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 nan or not.

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

Output

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

Working:

  1. 12 is not nan (False).
  2. 34 is not nan (False).
  3. 56 is not nan (False).
  4. 1 is not nan (False).
  5. nan is not a number (True).

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 nan or not.

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

Output:

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

Working:

  1. -nan is not a number (True).
  2. 34 is not nan (False).
  3. 56 is not nan (False).
  4. nan is not a number (True).
  5. inf is infinity. It is not nan (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 nan 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 nan")
print(torch.isnan(data1))

Output:

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

Working:

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

Conclusion

In this PyTorch lesson, we discussed isnan(). It returns False for the elements if the element is not nan. Otherwise, it returns True.

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