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 multidimensional array.

## torch.isinf()

In PyTorch, isinf() returns True for the elements if the element is infinity (positive infinity or negative infinity). Otherwise, it returns False.

It takes one parameter.

**Syntax:**

torch.isinf(tensor_object)

**Parameter:**

tensor_object is a tensor.

**Return:**

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

**Representation:**

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

import torch

#create a tensor

data1 = torch.tensor([12,34,56,1, float('inf')])

#display

print("Actual Tensor: ")

print(data1)

print("Check for Infinite")

print(torch.isinf(data1))

**Output:**

tensor([12., 34., 56., 1., inf])

Check for Infinite

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

**Working:**

- 12 is not infinity, so it is finite (False).
- 34 is not infinity, so it is finite (False).
- 56 is not infinity, so it is finite (False).
- 1 is not infinity, so it is finite (False)).
- inf is infinity, so it is infinite (True).

**Example 2:**

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

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 Infinite")

print(torch.isinf(data1))

**Output:**

tensor([-inf, 34., 56., nan, inf])

Check for Infinite

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

**Working:**

- -inf is negative infinity, so it is infinite (True).
- 34 is neither infinity nor nan, so it is Finite (False).
- 56 is neither infinity nor nan, so it is Finite (False).
- nan is not a number, so it is not finite and not Infinity (False).
- inf is infinity, so it is infinite (True).

**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 infinite or not.

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 Infinite")

print(torch.isinf(data1))

**Output:**

tensor([[-inf, 34., 56., nan, inf],

[-inf, 100., -4., nan, inf]])

Check for Infinite

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

[ True, False, False, False, True]])

**Working:**

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

## Work with CPU

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

import torch

#create a tensor

data1 = torch.tensor([12,34,56,1, float('inf')]).cpu()

#display

print("Actual Tensor: ")

print(data1)

print("Check for Infinite")

print(torch.isinf(data1))

**Output:**

Actual Tensor:

tensor([12., 34., 56., 1., inf])

Check for Infinite

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

**Working:**

- 12 is not infinity, so it is finite (False).
- 34 is not infinity, so it is finite (False).
- 56 is not infinity, so it is finite (False).
- 1 is not infinity, so it is finite (False).
- inf is infinity, so it is infinite (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 infinite or not.

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 Infinite")

print(torch.isinf(data1))

**Output:**

tensor([-inf, 34., 56., nan, inf])

Check for Infinite

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

**Working:**

- -inf is negative infinity, so it is infinite (True).
- 34 is neither infinity nor nan, so it is Finite (False).
- 56 is neither infinity nor nan, so it is Finite (False).
- nan is not a number, so it is not finite and not Infinity (False).
- inf is infinity, so it is infinite (True).

**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 infinite or not.

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 Infinite")

print(torch.isinf(data1))

**Output:**

tensor([[-inf, 34., 56., nan, inf],

[-inf, 100., -4., nan, inf]])

Check for Infinite

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

[ True, False, False, False, True]])

**Working:**

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

## Conclusion

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