Open In App

Tensorflow.js tf.complex() Function

Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment. It helps developers to develop ML models in JavaScript, and use ML directly in the browser or in Node.js.

The tf.complex() function is used to convert two real numbers to a complex number.



Syntax:

tf.complex(real, imag)

Parameter:



Return Value: It returns a tf.Tensor.

Note: The real parameter is the tf.Tensor represents the real part of the complex number and the imag parameter is the tf.Tensor represents the imaginary part of the complex number.
For example, 

Example 1:




// The complex function containing the
// real and imag Typedarray
const complex = tf.complex ([5,3],[1,3]);
  
// Printing the tensor
complex.print();

Output:

Tensor
   [5 + 1j, 3 + 3j]

Example 2:




// The complex function containing the
// real and imag Typedarray
const complex = tf.complex(
    tf.tensor1d([4.75, 5.75]),
    tf.tensor1d([2.25, 3.25])
);
  
// Printing the tensor
complex.print();

Output:

Tensor
   [4.75 + 2.25j, 5.75 + 3.25j]

Reference: https://js.tensorflow.org/api/latest/#complex

Article Tags :