TopK result always sort when k==num_cols
Please make sure that this is a bug. As per our GitHub Policy, we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template
System information
- Have I written custom code (as opposed to using a stock example script provided in TensorFlow): No
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): CentOS Linux release 7.5.1804
- Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: No
- TensorFlow installed from (source or binary): - TensorFlow version (use command below): 2.1.0 using pip install
- Python version: - Bazel version (if compiling from source):NA
- GCC/Compiler version (if compiling from source): NA
- CUDA/cuDNN version: - GPU model and memory:NA
You can collect some of this information using our environment capture
script
You can also obtain the TensorFlow version with: 1. TF 1.0: python -c "import tensorflow as tf; print(tf.GIT_VERSION, tf.VERSION)" 2. TF 2.0: python -c "import tensorflow as tf; print(tf.version.GIT_VERSION, tf.version.VERSION)"
Describe the current behavior
top_k op always sort when k == input size
Describe the expected behavior No sort when sorted=False Standalone code to reproduce the issue Provide a reproducible test case that is the bare minimum necessary to generate the problem. If possible, please share a link to Colab/Jupyter/any notebook.
import tensorflow as tf
a = tf.constant([1, 2, 3, 4], dtype=tf.float32)
b = tf.math.top_k(a, k=4, sorted=False)
c = tf.math.top_k(a, k=4, sorted=True)
print(b)
print(c)
Other info / logs Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached.
TopKV2(values=<tf.Tensor: shape=(4,), dtype=float32, numpy=array([4., 3., 2., 1.], dtype=float32)>, indices=<tf.Tensor: shape=(4,), dtype=int32, numpy=array([3, 2, 1, 0], dtype=int32)>)
TopKV2(values=<tf.Tensor: shape=(4,), dtype=float32, numpy=array([4., 3., 2., 1.], dtype=float32)>, indices=<tf.Tensor: shape=(4,), dtype=int32, numpy=array([3, 2, 1, 0], dtype=int32)>)
The result show b and c are same with the result has been sorted.
I have tried on colab with TF version 2.1.0, 2.2.0-dev20200218 and was able to reproduce the issue.Please, find the gist here. Thanks!
@rmlarsen do you know who works on this?
I have tried on colab with TF version 2.2,2.3-rc1,nightly version(2.4.0-dev20200712) and was able to reproduce the issue.Please, find the gist here. Thanks!
able to reproduce even when k!=num_col. sorted is never effective.
import tensorflow as tf
a = tf.reshape(tf.range(100), (10, 10))
a = tf.random.normal((10, 10))
print(a)
print(tf.math.top_k(a, k=4, sorted=True))
print(tf.math.top_k(a, k=4, sorted=False))
(tf 2.4.1, Driver: 460.73.01, CUDA Version: 11.2 Quadro RTX 6000) Reproduced also on google colab.
Basically sorted=False does not work at all. We have to tf.sort the indices again.
Was able to reproduce your issue in Tf Nightly 2.6.0-dev20210524, please find the gist here. Thanks!
@rmlarsen this is still not working for 2.6.0-dev20210607
Was able to reproduce your issue in Tf 2.13.0-dev20230305, please find the gist here. Thanks!
Hi,
Thank you for opening this issue. Since this issue has been open for a long time, the code/debug information for this issue may not be relevant with the current state of the code base.
The Tensorflow team is constantly improving the framework by fixing bugs and adding new features. We suggest you try the latest TensorFlow version with the latest compatible hardware configuration which could potentially resolve the issue. If you are still facing the issue, please create a new GitHub issue with your latest findings, with all the debugging information which could help us investigate.
Please follow the release notes to stay up to date with the latest developments which are happening in the Tensorflow space.
This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you.
This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further.