JavaScript

# Tensorflow.js – tf.isFinite()

## What Are Finite Values?

If you are working on a project using Machine Learning with Tensorflow.js library, if you only want to collect the Finite values from your dataset, you have to check for the Finite values first.

Finite values are the values which are not Infinite. Simply, we can say that except for -Infinity and Infinity, all are Finite.

So, to check if the data contains Finite values or not, we can use the tf.isFinite() function.

## Tf.isFinite() Function

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

Syntax:

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

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

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

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

</body>
</html>

Output:

We can see that false is returned 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 isFinite() 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.isFinite() </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 isFinite() on the above tensor
document.write("Finite?:- "+tf.isFinite(values));
</script>

</body>
</html>

Output:

Since they are related to the Finite values, it returns true.

## 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 Finites.

<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.isFinite() </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 isFinite() on the above tensor
document.write("Finite?:- "+tf.isFinite(values));
</script>

</body>
</html>

Output:

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

## Conclusion

In this Tensorflow.js tutorial, we learned how to check the Finite values in a tensor using the tf.isFinite() function with three different examples. There are only two Infinite values in the JavaScript: Infinity and -Infinity. Null, 0, undefined, and NaN come under the Finite 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