Python

# PyTorch – Isposinf()

We will check if the elements in a tensor are positive infinite or not using the isposinf() method in this PyTorch tutorial.

PyTorch is an open-source framework available with a Python programming language. Tensor is a multidimensional array that is used to store the data. 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 the data is a multi-dimensional array.

## Torch.isposinf()

Isposinf() in PyTorch returns True for the elements if the element is positive infinity. Otherwise, it returns False. It takes one parameter.

Syntax:

torch.isposinf(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 5 elements and check if these 5 elements are positive infinite 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 Positive Infinite")

print(torch.isposinf(data1))

Output:

Actual Tensor:

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

Check for Positive Infinite

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

Working:

1. Twelve (12) is not infinity, so it is finite (False)
2. Thirty-four (34) is not infinity, so it is finite (False)
3. Fifty-six (56) is not infinity, so it is finite (False)
4. One (1) is not infinity, so it is finite (False)
5. The inf is positive infinity (True)

## Example 2:

In this example, we will create a tensor with one dimension that has 5 elements and check if these 5 elements are positive infinite 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 Positive Infinite")

print(torch.isposinf(data1))

Output:

Actual Tensor:

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

Check for Positive Infinite

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

Working:

1. The -inf is negative infinity, so it is not positive infinite (False).
2. Thirty-four (34) is neither infinity nor nan, so it is Finite (False).
3. Fifty-six (56) is neither infinity nor nan, so it is Finite (False).
4. The nan is not a number, so it is not finite and not Infinity (False).
5. The inf is positive infinity (True).

## Example 3:

In this example, we will create a tensor with two dimensions that has 5 elements in each row and check if these 5 elements are positive infinite 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 Positive Infinite")

print(torch.isposinf(data1))

Output:

Actual Tensor:

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

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

Check for Positive Infinite

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

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

Working:

1. The -inf is negative infinity, so it is not positive infinite (False). The -inf is negative infinity, so it is not positive infinite (False).
2. Thirty-four (34) is neither infinity nor nan, so it is Finite (False). One-hundred (100) is neither infinity nor nan, so it is Finite (False).
3. Fifty-six (56) is neither infinity nor nan, so it is Finite (False). Negative four (-4) is neither infinity nor nan, so it is Finite (False).
4. The nan is not a number, so it is not infinite (False). The nan is not a number, so it is not infinite (False).
5. The inf is positive infinity (True). The inf is positive infinity (True).

## Work with CPU

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

When we create a tensor, 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 check if these 5 elements are positive infinite 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 Positive Infinite")

print(torch.isposinf(data1))

Output:

Actual Tensor:

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

Check for Positive Infinite

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

Working:

1. Twelve (12) is not infinity, so it is finite (False).
2. Thirty-four (34) is not infinity, so it is finite (False).
3. Fifty-six (56) is not infinity, so it is finite (False).
4. One (1) is not infinity, so it is finite (False).
5. The inf is positive infinity (True).

## Example 2:

In this example, we will create a tensor with one dimension that has 5 elements on the cpu and check if these 5 elements are positive infinite 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 Positive Infinite")

print(torch.isposinf(data1))

Output:

Actual Tensor:

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

Check for Positive Infinite

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

Working:

1. The -inf is negative infinity, so it is not positive infinite (False).
2. Thirty-four (34) is neither infinity nor nan, so it is Finite (False).
3. Fifty-six (56) is neither infinity nor nan, so it is Finite (False).
4. The nan is not a number, so it is not finite and not Infinity (False).
5. The inf is positive infinity (True).

## Example 3:

In this example, we will create a tensor with two dimensions that has 5 elements in each row and check if these 5 elements are positive infinite 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 Positive Infinite")

print(torch.isposinf(data1))

Output:

Actual Tensor:

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

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

Check for Positive Infinite

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

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

Working:

1. The -inf is negative infinity, so it is not positive infinite (False). The -inf is negative infinity, so it is not positive infinite (False).
2. Thirty-four (34) is neither infinity nor nan, so it is Finite (False). One-hundred (100) is neither infinity nor nan, so it is Finite (False).
3. Fifty-six (56) is neither infinity nor nan, so it is Finite (False). Negative four (-4) is neither infinity nor nan, so it is Finite (False).
4. The nan is not a number, so it is not infinite (False). The nan is not a number, so it is not infinite (False).
5. The inf is positive infinity (True). The inf is positive infinity (True).

## Conclusion

In this PyTorch lesson, we discussed about the isposinf() function. It returns False for the elements if the element is not positive infinity. Otherwise, it returns True.