rohlemax
rohlemax

Reputation: 31

tf.reduce_sum() unexpected result with uint8

Why does tf.reduce_sum() not work for uint8?

Consider this example:

>>> tf.reduce_sum(tf.ones((4, 10, 10), dtype=tf.uint8))
<tf.Tensor: shape=(), dtype=uint8, numpy=144>

>>> tf.reduce_sum(tf.ones((4, 10, 10), dtype=tf.uint16))
<tf.Tensor: shape=(), dtype=uint16, numpy=400>

Does someone know why that is?

The docs have not mentioned any incompatibility with uint8.

Upvotes: 2

Views: 232

Answers (1)

Kaveh
Kaveh

Reputation: 4970

uint8 stands for unsigned integer, which gets 8 bits in order to keep values.

By 8 bits, you can only save positive numbers (unsigned) in range [0, 255] (If it is int8 it can keep signed numbers in range [-127,+127]).

If you want to keep a value higher than 255, it only keeps the first 8 bits of that number, e.g. 256 in binary is 0000 0000 1, and the first 8 bits are 0000 0000. So, for 256 you will get 0 as the result:

>>> tf.reduce_sum(tf.ones((1, 255), dtype=tf.uint8))
    <tf.Tensor: shape=(), dtype=uint8, numpy=255>

>>> tf.reduce_sum(tf.ones((1, 256), dtype=tf.uint8))
    <tf.Tensor: shape=(), dtype=uint8, numpy=0>

In your case, the expected result is 400, but since uint8 cannot keep values higher than 255, it will get start at 0 when the sum gets to 256. So, you see result as 144 which is actually 400-256=144.

So, it's not ontf.reduce_sum(), it's on uint8, and take care of using any dtype.

Upvotes: 3

Related Questions