JavaScript

Tensorflow.js – tf.abs()

Machine Learning with Tensorflow.js is one of the best options to implement. In this case, we will see negative values. To overcome these values, or to get accurate results, we will convert them into positive values. So how will they be converted?

Tensorflow.js is a framework in Javascript that supports the tf.abs() function that converts all negative values to positive present in a tensor.

Introduction

A tensor in tensorflow.js acts as an array that stores elements in single or multiple dimensions.

If we want to create a tensor with one dimension, tf.tensor1d() is used.

Syntax:

tf.tensor1d([elements])

Parameter:

It takes an array that has elements separated by a comma.

To create a tensor with two dimensions (rows and columns), tf.tensor2d() is used.

Syntax:

tf.tensor2d([[elements],.........,[elements]])

tf.abs()

tf.abs() is used to return positive values from a given tensor.

So, it takes only one parameter: IE tensor that has numbers.

Syntax:

tf.abs(tensor_input)

Parameter:

tensor_input is a tensor that has numbers.

It can be 1 or 2-dimensional.

Let’s explore different examples of this method.

Example 1:

Here, we will create a one-dimensional tensor that has some negative values.

<html>

<!-- CDN Link that delivers the Tensorflow.js framework -->

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>

<center><h1>Tensorflow.js - tf.abs() </h1></center>

<script>

//create a one dimensional tensor with 5 elements

let values = tf.tensor1d([-45,-12,5,95,-67]);

//actual tensor

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

document.write("<br>");

//apply abs() on the above tensor

document.write("Final Tensor: "+tf.abs(values));

</script>

</body>

</html>

Output:

There are three negative values present in the above tensor.

-45, -12, and -67 are converted to positive but 5 and 95 remain the same.

Example 2:

Here, we will create a two-dimensional tensor with 3 rows and 2 columns that have some negative values.

<html>

<!-- CDN Link that delivers the Tensorflow.js framework -->

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>

<center><h1>Tensorflow.js - tf.abs() </h1></center>

<script>

//create a two dimensional tensor with 3 rows and 2 columns

let values = tf.tensor2d([[-45,-12],[34,56],[67,-43]]);

//actual tensor

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

document.write("<br>");

//apply abs() on the above tensor

document.write("Final Tensor: "+tf.abs(values));

</script>

</body>

</html>

Output:

There are three negative values present in the above tensor.

-45, -12, and -43 are converted to positive but 34, 56, and 67 remain the same.

Example 3:

In this case, we will consider the decimal values.

Here, we will create a two-dimensional tensor with 3 rows and 2 columns that has some negative values.

<html>

<!-- CDN Link that delivers the Tensorflow.js framework -->

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>

<center><h1>Tensorflow.js - tf.abs() </h1></center>

<script>

//create a two dimensional tensor with 3 rows and 2 columns

let values = tf.tensor2d([[-0.56,-0.45],[4.56,-9.45],[4.56,8.90]]);

//actual tensor

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

document.write("<br>");

//apply abs() on the above tensor

document.write("Final Tensor: "+tf.abs(values));

</script>

</body>

</html>

Output:

There are three negative values present in the above tensor.

-0.56, -0.45, and -9.4499998 are converted to positive but 4.5599999,4.5599999 and 8.8999996 remain the same.

Conclusion

In this Tensorflow.js tutorial, we saw how to convert negative values to positive using the tf.abs() function present in one /two dimensional tensors with three examples. Make sure that CDN Link is provided inside the script tag.

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