JavaScript

Tensorflow.js – tf.real()

When you are working with the complex numbers in tensorflow.js, in some cases, you need only real numbers from the complex numbers. Is it possible to get it? The answer is Yes.

Let’s see how to return the real number part from the given set of complex numbers.
As we know, the complex number is the form a+ib, where a refers to the real part and b refers to the imaginary part.

Tensorflow.js is a framework in Javascript which is used to run the Machine Learning models in the browser directly. Using this library, we can train and test the model and achieve the accuracy of the model.

In tensorflow.js, we can create a data using tensor. It can hold multiple elements separated by comma. We will run the Tensorflow.js framework inside the HTML Tags. It is very important to use the Content Delivery Network Link inside the script tag. This framework can be implemented inside the tag. This script tag can be placed inside the <head> or <body> tag.

Structure:

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

It is possible to create the multiple imaginary numbers in a tensor using the tf.complex() method.

Syntax:

tf.complex([real_parts],[imaginary_parts])

Note:

It takes two arrays as parameters. The first array takes the real part and the second array takes the imaginary parts.

Parameters:

Tf.real() Function

Tensorflow.js supports the tf.real() method that can return only the real number part from the complex number in a tensor. It takes only one parameter, i.e. tensor, that has complex numbers.

Syntax:

tf.real(complex_tensor)

Parameter:

The complex_tensor is a tensor that has complex numbers.

Without any delay, let’s create a tensor that has 5 complex numbers and return the real numbers from them.

Example 1:

Here, we will create a complex tensor that has numeric values.

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>
<center><h1>Linux Hint</h1></center>
<center><h2>Tensorflow.js - tf.real() </h2></center>
<script>
//create a tensor that store 5 complex numbers
let complex_numbers = tf.complex([34,56,78,45,0], [12,34,56,89,66]);

//actual tensor
document.write("Actual Tensor: ",values);

document.write("<br>");
//return only real part from the above complex_numbers
document.write("Tensor with real numbers: "+tf.real(complex_numbers));

</script>

</body>
</html>

Output:

We can observe that only the real numbers were returned from the complex numbers:

34 + 12j =>34
56 + 34j =>56
78 + 56j =>78
45 + 89j =>45
0 + 66j =>0

Example 2:

Here, we will create a complex tensor that has null, undefined, and NaN values.

It considers the null value as 0 and the undefined and NaN values as NaN(Not a Number).

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>
<center><h1>Linux Hint</h1></center>
<center><h2>Tensorflow.js - tf.real() </h2></center>
<script>
//create a tensor that store 3 complex numbers with null,NaN and undefined
let complex_numbers = tf.complex([null,undefined,NaN,], [null,undefined,NaN]);

//actual tensor
document.write("Actual Tensor: ",complex_numbers);

document.write("<br>");
//return only real part from the above complex_numbers
document.write("Tensor with real numbers: "+tf.real(complex_numbers));
</script>

</body>
</html>

Output:

Working:

Tensor [[1,2], [3,4], [5,6 ], [7,8 ]]
=>
1*3*5*7 = 105
2*4*6*8=384

We can observe that the null value is taken as 0 while the NaN and undefined values are taken as NaN. Finally, they were returned.

Conclusion

In this Tensorflow.js tutorial, we learned how to return the real part from the complex number using the tf.real() function. If the tensor has null, undefined, or NaN values, it considers the null value as 0 and the undefined and NaN values as NaN. Make sure that the CDN Link is provided inside the script tag.

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