JavaScript

Tensorflow.js – tf.round()

In this article, we will see how to round-off the values in tensorflow.js framework in the JavaScript. The advantages of rounding is we can represent the values in an integer format without any decimal points.

In Machine Learning, it is very simple to train a machine learning model if the input contains the integer values. It is very important to get only the integers from the decimal values and achieve the transformation (scaling) using this method. Each decimal value scales to an integer.

Tf.round() Function

The tf.exp() in Tensorflow.js returns the exponential values in a tensor. Mathematically, “e” is raised to the power of “x”.

Syntax:

tf.round(tensor_input)

Parameter:

The tensor_input is a tensor that has numbers.
It can be one or two-dimensional.

Example 1:

Let’s create a one-dimensional tensor in js that has some values and return the Rounded 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.round() </h2></center>
<script>

let values = tf.tensor1d([0.45,0.98,0.12,9.03,-0.343]);

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

document.write("<br>");
document.write("<br>");

//apply exp() on the above tensor
document.write("Tensor with Rounded Values: "+tf.round(values));
</script>

</body>
</html>

Output:

  1. 0.45 Rounds to 0
  2. 0.98 rounds to 1
  3. 0.12 rounds to 0
  4. 9.02 rounds to 9 and
  5. -0.343 rounds to 0

Example 2:

Let’s create a tensor that has two dimensions in js with 2 rows and 2 columns that has 0, null, Nan, and undefined values and return the Rounded 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.round() </h2></center>
<script>

let values = tf.tensor2d([[0,null],[undefined,NaN]]);
//actual tensor
document.write("Actual Tensor: ",values);

document.write("<br>");
document.write("<br>");

document.write("Tensor with Rounded Values: "+tf.round(values));
</script>

</body>
</html>

Output:

We can notice that for the values 0, null, NaN, and undefined, the rounded values is 0.

Example 3:

In this case, we will consider the Integer values. Let’s create a tensor that has two dimensions in js with 2 rows and 2 columns and return the Rounded 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.round() </h2></center>
<script>

let values = tf.tensor2d([[23,45],[11,22]]);
//actual tensor
document.write("Actual Tensor: ",values);

document.write("<br>");
document.write("<br>");

document.write("Tensor with Rounded Values: "+tf.round(values));
</script>

</body>
</html>

Output:

The rounded values is the same as the integers.

Conclusion

In this Tensorflow.js tutorial, we learned how to return the nearest values using the tf.round() function. If the input element is in 0, null, undefined, or NaN, it returns 0. For the integer values, it returns integer. We learned the three different examples on tf.round() function.

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