JavaScript

Tensorflow.js – tf.neg()

In tensorflow.js, if you want to convert the existing values present in a tensor to numeric values, the tf.neg() function is used. It converts each value in a tensor to negative by multiplying each value with -1.

Tf.neg() Function

The tf.neg() in Tensorflow.js converts all the values in a tensor to negative.

Syntax:

tf.neg(tensor_input)

Parameter:

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

Example 1:

Let’s create a one-dimensional tensor in js that has decimal values and convert them into negative 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.neg() </h2></center>
<script>

let values = tf.tensor1d([8.56,3.45,7.89,8.32]);
//actual tensor
document.write("Actual Tensor: ",values);

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

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

</body>
</html>

Output:

Working:

Tensor [8.5600004*-1, 3.45*-1, 7.8899999*-1, 8.3199997*-1]

If the values are already negative, the results are positive.

Example 2:

Let’s create a one-dimensional tensor in js that has negative decimal values and convert them into positive values with the neg() 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.neg() </h2></center>
<script>

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

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

document.write("Tensor with Negative Values:- "+tf.neg(values));
</script>

</body>
</html>

Output:

The positive values were returned with respect to the given negative values.

Working:

Tensor [-8.5600004*-1, -3.45*-1, -7.8899999*-1, -8.3199997*-1]

Example 3:

Let’s create a tensor that has two dimensions in js with 2 rows and 2 columns that has integer 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.neg() </h2></center>
<script>

let values = tf.tensor2d([-34,4,41,7],[2,2]);
//actual tensor
document.write("Actual Tensor: ",values);

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

document.write("Tensor with Negative Values:- "+tf.neg(values));
</script>

</body>
</html>

Output:

Working:

Tensor [[-34*-1, 4*-1], [41*-1 , 7*-1]]

Conclusion

In this Tensorflow.js tutorial, we learned how to get the negative values from a tensor using the tf.neg() function with three different examples. If the values are already negative, the results will be positive.

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