JavaScript

Tensorflow.js – tf.cumsum()

“tf.cumsum() in tensorflow.js returns the cumulative sum of elements present in a tensor.”

Syntax:

tf.cumsum(tensor_input,axis)

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, the cumulative sum of values is returned column-wise, and if axis=1, the cumulative sum of values is returned row-wise.

If the axis is not specified, then it will return the cumulative sum across each column.

Return

Return a Tensor with the cumulative sum of values.

Example 1:

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

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>
<center><h2>Tensorflow.js - tf.cumsum() </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 cumcum() on the above tensor
document.write("Cumulative Sum:- "+tf.cumsum(values));
</script>

</body>
</html>

Output:

Working:

1
2
3
4
34
34+56=90
34+56+78=168
34+56+78+90=258

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 sum of values across columns.

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>
<center><h2>Tensorflow.js - tf.cumsum() </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 cumsum() on the above tensor
document.write("Cumulative sum across columns:- "+tf.cumsum(values,0));
</script>

</body>
</html>

Output:

Working:

Tensor [[1, 2], [3, 4], [5, 6]]

Column values:-

Column 1: [1,1+3,1+3+5]=>[1,4,9]
Column 1: [2,2+4,2+4+6]=>[2,6,12]

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 sum of values across rows.

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>
<center><h2>Tensorflow.js - tf.cumsum() </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 cumsum() on the above tensor
document.write("Cumulative Sum of values across rows:- "+tf.cumsum(values,1));
</script>

</body>
</html>

Output:

Working:

Tensor [[1, 2], [3, 4], [5, 6], [7, 8]]

Row values:-

1
2
3
4
1,1+2 => [1, 3 ]
3,3+4 => [3, 7 ]
5,5+6 => [5, 11]
7,7+8 => [7, 15]

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 sum.

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>
<center><h2>Tensorflow.js - tf.cumsum() </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 cumsum() on the above tensor
document.write("Cumulative Sum of values:- "+tf.cumsum(values));
</script>

</body>
</html>

Output:

Working:

Tensor [[1, 2], [3, 4], [5, 6], [7, 8]]

Column values:-

Column 1: [1,1+3,1+3+5,1+3+5+7]=>[1,4,9,16]
Column 1: [2,2+4,2+4+6,2+4+6+8]=>[2,6,12,20]

Conclusion

In this Tensorflow.js tutorial, we have seen how to return the cumulative sum of elements present in a tensor using the tf.cumsum() method. In a 2D tensor, If axis=0, the cumulative sum of values is returned column-wise, and if axis=1, the cumulative sum of values is returned across row-wise. By default, it will return the cumulative sum across each column.

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