Skip to main content

Fixed: tf.reduce_mean Returns a Whole Number for an Integer Tensor

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

The average of the scores 7, 6, 8 and 6 is 6.75, but tf.reduce_mean returned 6, with no error. The scores were whole numbers, int32, and tf.reduce_mean gives its answer in the same type, so the fraction was dropped. Casting the scores to float32 first fixed it.

What I Was Doing​

I was averaging four quiz scores with tf.reduce_mean:

average.py
import tensorflow as tf

scores = tf.constant([7, 6, 8, 6])
print(tf.reduce_mean(scores))

I ran it in a uv project with TensorFlow 2.22.0:

Terminal
uv run python average.py

What I Expected​

6.75: the scores add up to 27, and 27 ÷ 4 is 6.75.

What I Got​

Output
tf.Tensor(6, shape=(), dtype=int32)

The terminal after uv run python average.py: tf.Tensor(6, shape=(), dtype=int32).

6, as an int32. The .75 was gone, and nothing said so.

What I Checked​

1. The type of the scores​

scores.dtype printed <dtype: 'int32'>. tf.constant makes whole numbers int32 unless told otherwise, and the result had the same type.

2. What the documentation says​

The tf.math.reduce_mean API page warns about it under "numpy compatibility": "Please note that np.mean has a dtype parameter that could be used to specify the output type. By default this is dtype=float64. On the other hand, tf.reduce_mean has an aggressive type inference from input_tensor". Its own example averages [1, 0, 1, 0] to 0 as an int32, and [1., 0., 1., 0.] to 0.5.

3. What happened to the fraction​

An int32 can't hold 6.75, so the result kept the whole part, 6, and dropped .75.

Why It Happens​

tf.reduce_mean returns its result in the type of its input. For whole-number types such as int32 or uint8, the average is a whole number too, and any fraction is lost.

The Fix​

Cast the scores to float32 before averaging:

average.py
import tensorflow as tf

scores = tf.constant([7, 6, 8, 6])
print(tf.reduce_mean(tf.cast(scores, tf.float32)))

tf.cast(scores, tf.float32) makes a float32 copy, so tf.reduce_mean works in decimals.

Another way: make the tensor with decimals​

If you create the tensor yourself, make it float32 from the start: tf.reduce_mean(tf.constant([7, 6, 8, 6], dtype=tf.float32)) returned 6.75.

After the Fix​

Terminal
uv run python average.py
Output
tf.Tensor(6.75, shape=(), dtype=float32)

The terminal after the fix: tf.Tensor(6.75, shape=(), dtype=float32).

6.75, as a float32. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Casting after averaging: tf.cast(tf.reduce_mean(scores), tf.float32)6.0: the fraction was already goneCast the scores, then average
Averaging a uint8 imageThe same problem: the values 138 and 139 averaged to 138 as uint8, and to 138.5 after a cast to float32Cast the image to float32 first

Troubleshooting​

SymptomLikely causeFix
tf.reduce_mean returns a whole numberThe tensor holds whole numbers (int32, uint8 and the like)tf.reduce_mean(tf.cast(tensor, tf.float32))
An image's average colour has no decimalsDecoded images are uint8Cast to float32 before averaging
tf.reduce_sum of a uint8 tensor is too smallThe sum wraps around past 255: 200 and 100 summed to 44Cast to a bigger type first. Fixed: 255 + 50 Gives 49 for a uint8 Tensor in TensorFlow explains the wrap-around

FAQ​

Does it round down?​

It drops the fraction, which isn't the same for negative numbers. The scores [-7, -6, -8, -6] average to -6.75, and tf.reduce_mean returned -6, not -7.

Why does dividing the sum give the right answer?​

tf.reduce_sum(scores) / 4 returned 6.75, as a float64. The sum is a whole number, 27, and TensorFlow's / turns whole-number tensors into decimals before dividing.

Conclusion​

tf.reduce_mean returned 6 instead of 6.75 because the scores were int32 and the average kept that type. tf.reduce_mean(tf.cast(scores, tf.float32)) returned 6.75.

Additional Resources​