Thursday, September 4, 2025
HomeLanguagesJavascriptTensorflow.js tf.real() Function

Tensorflow.js tf.real() Function

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS