Open In App

Tensorflow.js tf.real() Function

Improve
Improve
Like Article
Like
Save
Share
Report

Tensorflow.js is an open-source library that is being developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The .real() function is used to find a tensor for the stated tensor input, which is of type float and is the real section of each element that is considered in input as a complex number.

Syntax :  

tf.real( input )

Parameters:  

  • input: It is the tensor input, which takes a complex number as an input.

Return Value: It returns the real section of a complex or a real tensor input.

Example 1: In this example, we are defining an input tensor of float type and then printing the real part of it. For creating an input tensor we are utilizing the .complex() method and in order to print the output we are using the .print() method.  

Javascript




// Defining input tensor
const y = tf.complex([-9.25, 12.25], [6.66, 8.66]);
  
// Calling real() function and
// printing the output
tf.real(y).print();


Output:

Tensor
    [-9.25, 12.25]

Example 2: In this example, we are using null, character, as well as integer type values as tensor input.

Javascript




// Defining input tensor
const y = tf.complex([null, 'a'], [5, 'r']);
  
// Calling real() function
var z = tf.real(y);
  
// Prints output
z.print();


Output:

Tensor
    [0, NaN]

In the above example, the null value returns zero and the character type value returns NaN.

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


Last Updated : 20 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads