Tensorflow sort changes values in output list to 0 when the tensor datatype is tf.float32, but not tf.float64

Viewed 66

Actions speak louder than words so here is the MWE:

import tensorflow as tf

n=17
a = tf.random.uniform(shape=[n], dtype=tf.float32)
print(a)
print(tf.sort(a))

When n<=16 it sorts the values just fine, but when n>16 it sorts the list and then turns the values at position 16 and higher to have value = -0. Example output:

tf.Tensor(
[0.41191268 0.48915362 0.65293264 0.6125376  0.00088847 0.03644979
 0.13768506 0.528106   0.27231824 0.4003389  0.5799836  0.83420205
 0.06494105 0.39109504 0.8135816  0.153288   0.07945895], shape=(17,), dtype=float32)
tf.Tensor(
[ 0.00088847  0.03644979  0.06494105  0.07945895  0.13768506  0.153288
  0.27231824  0.39109504  0.4003389   0.41191268  0.48915362  0.528106
  0.5799836   0.6125376   0.65293264  0.8135816  -0.        ], shape=(17,), dtype=float32)

But when I make the dtype tf.float64 there is no problem for seemingly arbitrary n:

tf.Tensor(
[0.91347295 0.60086058 0.0271204  0.83564393 0.49664206 0.96215479
 0.60472639 0.64395121 0.58394402 0.93489432 0.50379539 0.14087138
 0.51662724 0.29758834 0.5657154  0.08638131 0.47912787], shape=(17,), dtype=float64)
tf.Tensor(
[0.0271204  0.08638131 0.14087138 0.29758834 0.47912787 0.49664206
 0.50379539 0.51662724 0.5657154  0.58394402 0.60086058 0.60472639
 0.64395121 0.83564393 0.91347295 0.93489432 0.96215479], shape=(17,), dtype=float64)

Not sure if this is a bug or expected behavior. It does not depend on eager execution. I was noticing issues when using the tensorflowprobability percentile function where it was giving me -0.0 as the value, so I made my own percentile function and observed the same issue (which I suspect is due to this underlying issue with tf.sort). Numpy sorting works fine regardless of the data type, but I was trying to keep things within tensorflow.

Any reason why this might be happening or should I make a bug report?

Hardware: I am on an M1 Macbook Air using tensorflow 2.5.0

1 Answers

I also checked the same with tensorflow==2.5.0 as you mentioned and did not notice any issue.

import tensorflow as tf
print(tf.__version__)

Output:

2.5.0

and then

import tensorflow as tf

n=17
a = tf.random.uniform(shape=[n], dtype=tf.float32)
print(a)
print(tf.sort(a))

Output:

tf.Tensor(
[0.7946081  0.84397626 0.04671419 0.276353   0.8124876  0.66761124
 0.21016991 0.28140187 0.22393394 0.20382321 0.667526   0.1714747
 0.9672215  0.17870915 0.9914366  0.32059753 0.5422765 ], shape=(17,), dtype=float32)
tf.Tensor(
[0.04671419 0.1714747  0.17870915 0.20382321 0.21016991 0.22393394
 0.276353   0.28140187 0.32059753 0.5422765  0.667526   0.66761124
 0.7946081  0.8124876  0.84397626 0.9672215  0.9914366 ], shape=(17,), dtype=float32)

with n>16:

import tensorflow as tf

n=18
a = tf.random.uniform(shape=[n], dtype=tf.float32)
print(a)
print(tf.sort(a))

Output:

    tf.Tensor(
[0.1922586  0.6136733  0.7517139  0.3762852  0.52895963 0.7804493
 0.9869323  0.08194113 0.3963052  0.6049119  0.9553219  0.18031311
 0.58210933 0.92059183 0.4442644  0.91004515 0.4451145  0.8300687 ], shape=(18,), dtype=float32)
tf.Tensor(
[0.08194113 0.18031311 0.1922586  0.3762852  0.3963052  0.4442644
 0.4451145  0.52895963 0.58210933 0.6049119  0.6136733  0.7517139
 0.7804493  0.8300687  0.91004515 0.92059183 0.9553219  0.9869323 ], shape=(18,), dtype=float32)

Would you mind checking it back again and let us know if the issue still persists?

Related