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:**

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:**

**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.

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:**

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.

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:

Is Real:?

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

**Working:**

- 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).
- 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).
- 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).
- 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).
- 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:**

**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.

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:**

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.

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:**

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:**

- 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).
- 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).
- 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).
- 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).
- 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.