JavaScript

Tensorflow.js – tf.sum()

“In tensorflow.js, if you want to return the sum of elements in a tensor, then you should know about the tf.sum() method.”

tf.sum()

tf.sum() in tensorflow.js returns the total sum of elements.

Syntax:

tf.sum(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 sum across rows or columns.

If axis=0, the total sum is returned column-wise, and if axis=1, the total sum is returned row-wise.

If the axis is not specified, then it will return the sum of all elements.

Return:

Return a Tensor with the total sum.

Example 1:

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

<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.sum() </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 sum() on the above tensor
document.write("Total Sum:- "+tf.sum(values));
</script>

</body>
</html>

Output:

Working:

34+56+78+90 = 258.

Example 2:

Let’s create a tensor that has 2 dimensions in js with 4 rows and 2 columns that has integer values and return sum across 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.sum() </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>");

//apply sum() on the above tensor
document.write("Total Sum across columns:- "+tf.sum(values,0));
</script>

</body>
</html>

Output:

Working:

Tensor [[34, 56], [78, 90], [1 , 0 ], [3 , 4 ]]
=>
34+78+1+0 = 116
56+90+0+4=150.

Example 3:

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

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


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

//apply sum() on the above tensor
document.write("Total Sum across rows:- "+tf.sum(values,1));
</script>

</body>
</html>

Output:

Working:

Tensor [[1, 0], [3, 4]]
=>
1+0 = 1
3+4=7.

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 total sum 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.sum() </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>");

//apply sum() on the above tensor
document.write("Total Sum across rows and columns:- "+tf.sum(values));
</script>

</body>
</html>

Output:

Working:

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

Conclusion

In this Tensorflow.js tutorial, we have seen how to return the total sum of elements present in a tensor using the tf.sum() method. In a 2D tensor, if axis=0, the total sum is returned column-wise, and if axis=1, the total sum is returned row-wise. By default, it will return the sum of all elements across 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