Syntax:
Parameter:
1. tensor_input is a tensor that has numeric elements.
It can be 1or 2 dimensional.
2. If the tensor is two-dimensional, then it is possible to specify the axis to get a cumulative product of values in rows or columns.
If axis=0, a cumulative product of values is returned across column-wise, and if axis=1, a cumulative product of values is returned across row-wise.
If the axis is not specified, then it will return the cumulative product across each column.
Return
Return a Tensor with the cumulative product of values.
Example 1:
Let’s create a one-dimensional tensor in js that has integer values and return the cumulative product.
<!-- CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<body>
<center><h2>Tensorflow.js - tf.cumprod() </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 cumprod() on the above tensor
document.write("Cumulative Product:- "+tf.cumprod(values));
</script>
</body>
</html>
Output:
Working:
1 2 3 4 | 34 34*56=1904 34*56*78=148512 34*56*78*90=13366080 |
Example 2:
Let’s create a tensor that has 2 dimensions with 3 rows and 2 columns that has integer values and return the cumulative product of values across columns.
<!-- CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<body>
<center><h2>Tensorflow.js - tf.cumprod() </h2></center>
<script>
let values = tf.tensor2d([1,2,3,4,5,6],[3,2]);
//actual tensor
document.write("Actual Tensor: ",values);
document.write("<br>");
document.write("<br>");
//apply cumprod() on the above tensor
document.write("Cumulative Product across columns:- "+tf.cumprod(values,0));
</script>
</body>
</html>
Output:
Working:
Column values:-
Column 1: [2,2*4,2*4*6]=>[2,8,48]
Example 3:
Let’s create a tensor that has 2 dimensions in js with 4 rows and 2 columns that has integer values and return the cumulative product of values across rows.
<!-- CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<body>
<center><h2>Tensorflow.js - tf.cumprod() </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>");
//apply cumprod() on the above tensor
document.write("Cumulative Product of values across rows:- "+tf.cumprod(values,1));
</script>
</body>
</html>
Output:
Working:
Row values:-
1 2 3 4 | 1,1*2 => [1, 2 ] 3,3*4 => [3, 12 ] 5,5*6 => [5, 30] 7,7*8 => [7, 56] |
Example 4:
Let’s create a tensor that has 2 dimensions in js with 4 rows and 2 columns that have integer values and return the cumulative product.
<!-- CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<body>
<center><h2>Tensorflow.js - tf.cumprod() </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>");
//apply cumprod() on the above tensor
document.write("Cumulative Product of values:- "+tf.cumprod(values));
</script>
</body>
</html>
Output:
Working:
Column values:-
Column 1: [2,2*4,2*4*6,2*4*6*8]=>[2,8,48,384]
Conclusion
In this Tensorflow.js tutorial, we have seen how to return the cumulative product of elements present in a tensor using the tf.cumprod() method. In a 2D tensor, if axis=0, a cumulative product of values is returned column-wise, and if axis=1, a cumulative product of values is returned across row-wise. By default, it will return the cumulative product across each column.