Python

PyTorch – Isreal()

We will check if the elements in the tensor are real type or not using the isreal() 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.isreal()

Isreal() in PyTorch checks if the values in a tensor are real valued or not. If the value is real, it returns True. Otherwise, it returns False.

If the imaginary part is 0 in a complex value, it is considered as a real value. It takes one parameter.

Syntax:

torch.isreal(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 check whether the elements are real or not.

#first import the torch module

import torch

 

#create a 1D tensor

data1 = torch.tensor([1.34,5.67,8.90,4.56,7.43])

 

#display

print("Actual elements in the Tensor: ")

print(data1)

 

print("Is Real:? ")

print(torch.isreal(data1))

Output:

Actual elements in the Tensor:

tensor([1.3400, 5.6700, 8.9000, 4.5600, 7.4300])

Is Real:?

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

Here, all the elements in the tensor are real values since it returns True for all the values.

Example 2:

In this example, we will create a tensor with one dimension that has 5 elements and check whether the elements are real or not.

#first import the torch module

import torch

 

#create a 1D tensor

data1 = torch.tensor([1.34+4j,4+8j,3+0j,6+0j,45+89j])

 

#display

print("Actual elements in the Tensor: ")

print(data1)

 

print("Is Real:? ")

print(torch.isreal(data1))

Output:

Actual elements in the Tensor:

tensor([ 1.3400+4.j, 4.0000+8.j, 3.0000+0.j, 6.0000+0.j, 45.0000+89.j])

Is Real:?

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

Working:

  1. The 1.3400+4.j is a complex number and the imaginary part is 4 which is not equal to 0, so it is not real (False).
  2. The 4.0000+8.j is a complex number and the imaginary part is 8 which is not equal to 0, so it is not real (False).
  3. The 3.0000+0.j is a complex number and the imaginary part is 0 which is equal to 0, so it is real (True).
  4. The 6.0000+0.j is a complex number and the imaginary part is 0 which is equal to 0, so it is real (True).
  5. The 45.0000+89.j is a complex number and the imaginary part is 89 which is not equal to 0, so it is not real (False).

Work with CPU

If you want to run an isreal() 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 whether the elements are real or not.

#first import the torch module

import torch

 

#create a 1D tensor

data1 = torch.tensor([1.34,5.67,8.90,4.56,7.43])

 

#display

print("Actual elements in the Tensor: ")

print(data1)

 

print("Is Real:? ")

print(torch.isreal(data1))

Output:

Actual elements in the Tensor:

tensor([1.3400, 5.6700, 8.9000, 4.5600, 7.4300])

Is Real:?

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

Here, all the elements in the tensor are real values since it returns True for all the values.

Example 2:

In this example, we will create a tensor with one dimension that has 5 elements on the cpu and check whether the elements are real or not.

#first import the torch module

import torch

 

#create a 1D tensor

data1 = torch.tensor([1.34+4j,4+8j,3+0j,6+0j,45+89j])

 

#display

print("Actual elements in the Tensor: ")

print(data1)

 

print("Is Real:? ")

print(torch.isreal(data1))

Output:

Actual elements in the Tensor:

tensor([ 1.3400+4.j, 4.0000+8.j, 3.0000+0.j, 6.0000+0.j, 45.0000+89.j])

Is Real:?

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

Working:

  1. The1.3400+4.j is a complex number and the imaginary part is 4 which is not equal to 0, so it is not real (False).
  2. The 4.0000+8.j is a complex number and the imaginary part is 8 which is not equal to 0, so it is not real (False).
  3. The 3.0000+0.j is a complex number and the imaginary part is 0 which is equal to 0, so it is real (True).
  4. The 6.0000+0.j is a complex number and the imaginary part is 0 which is equal to 0, so it is real (True).
  5. The 45.0000+89.j is a complex number and the imaginary part is 89 which is not equal to 0, so it is not real (False).

Conclusion

In this PyTorch lesson, we discussed about the isreal() function. It checks if the elements in a tensor are real values or not. If it is real, it returns True, otherwise it returns False. We learned the two different examples and also worked these examples on a cpu machine.

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