JavaScript

Tensorflow.js – tf.islnf()

While you are training a model in machine learning using the Tensorflow.js, you may overcome with Infinite values in your dataset.

If you include these values in your dataset, you didn’t get the training accuracy for the model. It is necessary to remove the Infinites from your data. First, we have to check that all the elements present in the dataset are not Infinites. Then, only you have to proceed to train and work on the model.

To check if the data contains Infinite values or not, we use the tf.isinf() function.

Tf.isinf() Function

The tf.isinf() is used to check if the element is Infinity or Infinity. It returns the Boolean values. If the value is -Infinity or Infinity, it returns true. Otherwise, it returns false.

Syntax:

tf.isinf(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 positive and negative Infinities and apply the isInf() function.

<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.isInf() </h2></center>
<script>

 let values = tf.tensor1d([Infinity,-Infinity]);
//actual tensor
document.write("Actual Tensor: ",values);

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

//apply isInf() on the above tensor
document.write("Infinity?:- "+tf.isInf(values));
</script>

</body>
</html>

 
Output:


We can see that it returns true for the Infinity values (both positive and negative).

Example 2:

Let’s create a one-dimensional tensor in js that has 0, null, NaN, and undefined values and apply the isInf() function.

<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.isInf() </h2></center>
<script>

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

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

//apply isInf() on the above tensor
document.write("Infinity?:- "+tf.isInf(values));
</script>

</body>
</html>

 
Output:


Since they are not related to Infinite values, false is returned.

Example 3:

Let’s create a tensor that has two dimensions in js with 2 rows and 2 columns that has decimal values with Infinites and check for Infinites.

<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.isInf() </h2></center>
<script>

let values = tf.tensor2d([[Infinity,4.56],[-Infinity,7.89]]);
//actual tensor
document.write("Actual Tensor: ",values);

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

//apply isInf() on the above tensor
document.write("Infinity?:- "+tf.isInf(values));
</script>

</body>
</html>

 
Output:


There are two Infinities present in the previous tensor. Hence, for those values, true is returned.

Conclusion

In this Tensorflow.js tutorial, we learned how to check the Infinite values in a tensor using the tf.isInf() function with three different examples. In the JavaScript, we can create an Infinite value using Infinity or -Infinity. The null, 0, undefined, and NaN doesn’t come under the Infinite values.

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