JavaScript

Tensorflow.js – tf.mean()

“tf.mean() in tensorflow.js returns the total average of elements.”

Syntax:

tf.mean(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 the average across rows or columns.

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

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

Return

Return a Tensor with the average.

Example 1:

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

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

</body>
</html>

Output:

Working:

(34+56+78+90)/4 = 64.5

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 average 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.mean() </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 mean() on the above tensor
document.write("Total Average across columns:- "+tf.mean(values,0));
</script>

</body>
</html>

Output:

Working:

Tensor [[1,2], [3,4], [5,6 ], [7,8 ]]
=>
(1+3+5+7)/4 => 16/4  = 4
(2+4+6+8)/4 => 20/4  = 5

Example 3:

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

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

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

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

</body>
</html>

Output:

Working:

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

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

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 average 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.mean() </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 mean() on the above tensor
document.write("Total Average across rows and columns:- "+tf.mean(values));
</script>

</body>
</html>

Output:

Working:

Tensor [[34, 56], [78, 90], [1, 0], [3, 4]]
=>
(34+56+78+90+1+0+3+4)/8=33.25.

Conclusion

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