Skip to main content

How to Calculate the Average of a Tensor with tf.reduce_mean

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

The average brightness of a photo, the average error of a model, the average temperature of a week: TensorFlow computes each with tf.reduce_mean. This guide averages a small table of sleep hours in TensorFlow 2.22.0, overall, per person and per night, and shows the one trap: whole numbers give a whole-number average.

Syntax​

Syntax
tf.reduce_mean(<tensor>, axis=<axis>)
PartWhat it means
<tensor>The values to average
axis=<axis>Optional. The axis to average along. Leave it out to average every value; for a table, axis=1 gives one average per row and axis=0 one per column

The tf.math.reduce_mean API page says it "Computes the mean of elements across dimensions of a tensor", and warns about the type of the result: "tf.reduce_mean has an aggressive type inference from input_tensor". tf.reduce_mean is the same function under a shorter name.

The Example​

Hours of sleep for two people over three nights: one row per person, one column per night.

Night 1Night 2Night 3
Person 17.56.08.0
Person 25.57.06.5

Prerequisites​

  • A uv project with TensorFlow. This guide uses TensorFlow 2.22.0 on Python 3.14.10, with uv 0.13.10.

Step 1: Write the Script​

averages.py
import tensorflow as tf

# Hours of sleep for two people (rows) over three nights (columns)
sleep = tf.constant([[7.5, 6.0, 8.0], [5.5, 7.0, 6.5]])

print(tf.reduce_mean(sleep))
print(tf.reduce_mean(sleep, axis=1))
print(tf.reduce_mean(sleep, axis=0))

The averages.py script in a macOS-style code window: a sleep table of two people by three nights, then tf.reduce_mean over the whole table, with axis=1 and with axis=0.

Three averages: of all six nights, of each person (along each row, axis=1), and of each night (down each column, axis=0).

Step 2: Run It​

Terminal
uv run python averages.py
Output
tf.Tensor(6.75, shape=(), dtype=float32)
tf.Tensor([7.1666665 6.3333335], shape=(2,), dtype=float32)
tf.Tensor([6.5 6.5 7.25], shape=(3,), dtype=float32)

The terminal after uv run python averages.py: 6.75 for the whole table, 7.1666665 and 6.3333335 per person with axis=1, and 6.5, 6.5 and 7.25 per night with axis=0.

On average the two slept 6.75 hours. Person 1 averaged 7.1666665 hours and person 2 6.3333335, one value per row. The nights averaged 6.5, 6.5 and 7.25, one value per column. The odd last digits are normal: float32 can't store 7⅙ exactly, so it keeps the nearest value it can, which prints as 7.1666665. The command works unchanged in PowerShell.

The first run in a new environment is slow, while TensorFlow loads for the first time.

Common Mistakes​

MistakeWhat happensDo this instead
Averaging whole numberstf.reduce_mean(tf.constant([7, 6, 8, 6])) returned tf.Tensor(6, shape=(), dtype=int32). The real average is 6.75, but an int32 tensor gets an int32 averageCast to float32 first: tf.reduce_mean(tf.cast(..., tf.float32)) returned 6.75. Fixed: tf.reduce_mean Returns a Whole Number for an Integer Tensor has the details

FAQ​

Can I average over two axes at once?​

Yes, pass a list: axis=[0, 1] averages over both axes of the table, which returned 6.75, the same as no axis at all. For a colour photo, axis=[0, 1] averages over its height and width and leaves one average per colour.

Is tf.reduce_mean the same as NumPy's np.mean?​

The API page calls it "Equivalent to np.mean", with one difference: "np.mean has a dtype parameter that could be used to specify the output type. By default this is dtype=float64." tf.reduce_mean keeps the tensor's own type instead.

Conclusion​

tf.reduce_mean averaged the sleep table to 6.75 hours, per person with axis=1 and per night with axis=0. The result keeps the tensor's type, so cast whole numbers to float32 before averaging them.

Additional Resources​