As we know, 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 <script> 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 multiple imaginary numbers in a tensor using the tf.complex() method.

**Syntax:**

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

**Parameters:**

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

**Note:**

The elements in the two arrays must be equal. Otherwise, an error is thrown.

## Tf.imag() Function

Tensorflow.js supports the tf.imag() method that returns only the imaginary number from the complex number in a tensor. It takes only one parameter, i.e. tensor, that has complex numbers.

**Syntax:**

**tf.imag(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 imaginary numbers from them.

## Example 1:

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

<!-- CDN Link that delivers the Tensorflow.js framework -->

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

<body>

<center><h1>Tensorflow.js - tf.imag() </h1></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: ",complex_numbers);

document.write("<br>");

//return only imaginary part from the above complex_numbers

document.write("Tensor with imaginary numbers:"+tf.imag(complex_numbers));

</script>

</body>

</html>

**Output:**

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

56 + 34j =>34

78 + 56j =>56

45 + 89j =>89

0 + 66j =>66

**Example 2:**

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

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

<!-- CDN Link that delivers the Tensorflow.js framework -->

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

<body>

<center><h1>Tensorflow.js - tf.imag() </h1></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 complex part from the above complex_numbers

document.write("Tensor with complex numbers: "+tf.imag(complex_numbers));

</script>

</body>

</html>

**Output:**

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 imaginary part from the complex number using the tf.imag() 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.