tf reduce_mean featured image

tf reduce_mean : Find mean of tensors in Tensorflow

Popular Domains for just 99 Cents at Namecheap!

Tensorflow is mostly used for building deep learning models. There are many functions that allow you to manipulate data to make the best predictive model. The TensorFlow reduce_mean is one of them. In this entire tutorial, you will know the implementation of TensorFlow tf.reduce_mean with various examples.

Examples on tf reduce_mean

In this section, you will know all the examples of the TensorFlow reduce_mean() function. The reuduce_mean function calculates the mean of elements across dimensions of a tensor.

Please note that I am doing all the coding demonstrations on Jupyter Notebook. For better understanding do the example on your Jupyter Notebook.

Example 1: Applying tf.reduce_mean on Single Dimension

In this example, firstly I will create a sample tensor of a single dimension and then calculate the mean of all the elements present in the tensor.

Just execute the below lines of code and see the output.

import tensorflow as tf
tensor = tf.constant([10,20,30,40])
mean = tf.reduce_mean(tensor)
print(mean)

Here I am creating a sample tensor using the tf.constant() method and lastly applying reduce_mean() on it. You will get the following output when you will run the code.

Output

Mean of tensor of single dimension
Mean of the tensor of a single dimension

 

Example 2: Apply reduce_mean on Multi Dimension

In this example, I will create a multi-dimensional tensor and apply the reduce_mean on it. There are two ways you can find mean on multi-dimensional tensor. The one is finding mean row-wise and the other is finding mean columnwise. I will show you the examples of both.

Finding mean row-wise

For finding the mean for all the elements row-wise you have to pass the axis value as 0.

Execute the below lines of code to calculate the mean.

import tensorflow as tf
tensor = tf.constant([[10,20,30,40],[50,60,70,80]])
mean = tf.reduce_mean(tensor,axis=0)
print(mean)

Output

Mean of tensor of Multi dimensional row-wise
Mean of the tensor of Multidimensional row-wise

Finding mean column-wise

In the same way, you can find mean of tensor column-wise by passing the value of the axis to 1. Run the below lines of code and see the output.

import tensorflow as tf
tensor = tf.constant([[10,20,30,40],[50,60,70,80]])
mean = tf.reduce_mean(tensor,axis=1)
print(mean)

Output

Mean of tensor of Multi dimensional column-wise
Mean of the tensor of Multidimensional column-wise

If you directly apply the tf.reduce_mean() without passing the axis value. Then it will find the mean of the entire elements present in the tensor.

Conclusion

The mean of tensors will generally be used while building your deep learning model. These are examples of how to use the TensorFlow reduce_mean() function. I hope you have liked this tutorial. If you have any queries then you can contact us for more help.

Source:

Tensorflow Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner