JavaScript

Tensorflow.js – tf.prod()

In tensorflow.js, if you want to return the product of elements in a tensor, you should know about the tf.prod() method.

Tf.prod() Function

The tf.prod() in Tensorflow.js returns the total product of elements.

Syntax:

tf.prod(tensor_input,axis)

Parameter:

1. The tensor_input is a tensor that has numeric elements.

It can be one or two-dimensional.

2. If the tensor is two-dimensional, it is possible to specify the axis to get a product across the rows or columns.
If axis=0, the total product is returned across the column wise. If the axis=1, the total product is returned across the row wise.

If the axis is not specified, it returns the product of all elements.

Return:

Returns a Tensor with the product.

Example 1:

Let’s create a one-dimensional tensor in js that has integer values and return the product.

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

let values = tf.tensor1d([34,56,78,90]);
//actual tensor
document.write("Actual Tensor: ",values);

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

//apply exp() on the above tensor
document.write("Total Product:- "+tf.prod(values));
</script>

</body>
</html>

Output:

Working:

34*56*78*90 = 13366080.

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

Example 2:

Let’s create a tensor that has two dimensions in js with 4 rows and 2 columns that has integer values and return the product across the columns.

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

let values = tf.tensor2d([1,2,3,4,5,6,7,8],[4,2]);
//actual tensor
document.write("Actual Tensor: ",values);

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

document.write("Total Product across columns:- "+tf.prod(values,0));
</script>

</body>
</html>

Output:


Working:

Tensor [[1,2], [3,4], [5,6 ], [7,8 ]]
=>
1*3*5*7 = 105
2*4*6*8=384

Example 3:

Let’s create a tensor that has two dimensions in js with 1 row and 2 columns that has integer values and return the product across the rows.

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

let values = tf.tensor2d([[1],[3]]);
//actual tensor
document.write("Actual Tensor: ",values);

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

document.write("Total Product across rows:- "+tf.prod(values,1));
</script>

</body>
</html>

Output:

Working:

Tensor [[1], [3]]
=>
1
3.

Since there is only one element in each row, it returns itself.

Example 4:

Let’s create a tensor that has two dimensions in js with 4 rows and 2 columns that have integer values and return the total product in all rows and columns.

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

let values = tf.tensor2d([34,56,78,90,1,0,3,4],[4,2]);
//actual tensor
document.write("Actual Tensor: ",values);

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

document.write("Total Product across rows:- "+tf.prod(values,1));
</script>

</body>
</html>

Output:

Working:

Tensor [[34, 56], [78, 90], [1 , 0 ], [3 , 4 ]]
=>
34*56*78*90*1*0*3*4=0..

Conclusion

In this Tensorflow.js tutorial, we learned how to return the total product of elements present in a tensor using the tf.prod() method. In a 2D tensor, if axis=0, total product is returned across the column wise. If axis=1, the total product is returned across the row wise. By default, it returns the product of all elements across the rows and columns.

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