leezu opened a new issue #11271: nd.topk regression with nan values
URL: https://github.com/apache/incubator-mxnet/issues/11271
 
 
   mxnet master contains a regression in the nd.topk operator compared to mxnet 
v1.2. Likely https://github.com/apache/incubator-mxnet/pull/10997 is at fault 
(but I have not performed an bisection to proof that).
   
   Consider the following:
   
   Behavior in mxnet master:
   ```
   In [1]: a = [np.nan] * 4 + list(range(2500))
       ...: a = mx.nd.array(a)
       ...: print(a)
       ...: for k in range(3,10):
       ...:     print(mx.nd.topk(a, k=k))
       ...:
       ...:
   [  nan   nan   nan ... 2497. 2498. 2499.]
   <NDArray 2504 @cpu(0)>
   
   [2. 0. 1.]
   <NDArray 3 @cpu(0)>
   
   [2. 0. 1. 3.]
   <NDArray 4 @cpu(0)>
   
   [2. 4. 0. 1. 3.]
   <NDArray 5 @cpu(0)>
   
   [5. 4. 2. 0. 1. 3.]
   <NDArray 6 @cpu(0)>
   
   [2. 6. 5. 4. 0. 1. 3.]
   <NDArray 7 @cpu(0)>
   
   [ 2.  1. 10.  9.  8.  6.  0.  3.]
   <NDArray 8 @cpu(0)>
   
   [ 2.  1. 11. 10.  9.  6.  8.  0.  3.]
   <NDArray 9 @cpu(0)>
   ```
   
   Behavior in mxnet v1.2:
   ```
   In [1]: In [1]: a = [np.nan] * 4 + list(range(2500))
      ...:     ...: a = mx.nd.array(a)
      ...:     ...: print(a)
      ...:     ...: for k in range(3,10):
      ...:     ...:     print(mx.nd.topk(a, k=k))
      ...:
   
   [  nan   nan   nan ... 2497. 2498. 2499.]
   <NDArray 2504 @cpu(0)>
   
   [0. 1. 2.]
   <NDArray 3 @cpu(0)>
   
   [0. 1. 2. 3.]
   <NDArray 4 @cpu(0)>
   
   [0.000e+00 1.000e+00 2.000e+00 3.000e+00 2.503e+03]
   <NDArray 5 @cpu(0)>
   
   [0.000e+00 1.000e+00 2.000e+00 3.000e+00 2.503e+03 2.502e+03]
   <NDArray 6 @cpu(0)>
   
   [0.000e+00 1.000e+00 2.000e+00 3.000e+00 2.503e+03 2.502e+03 2.501e+03]
   <NDArray 7 @cpu(0)>
   
   [0.000e+00 1.000e+00 2.000e+00 3.000e+00 2.503e+03 2.502e+03 2.501e+03
    2.500e+03]
   <NDArray 8 @cpu(0)>
   
   [0.000e+00 1.000e+00 2.000e+00 3.000e+00 2.503e+03 2.502e+03 2.501e+03
    2.500e+03 2.499e+03]
   <NDArray 9 @cpu(0)>
   ```
   
   Notice how the result is correct with mxnet version 1.2 but wrong on the 
master version. As a sidenote, even with mxnet version 1.2 the behavior of 
nd.topk is inconsistent with nd.max. The latter ignores 'nan' whereas the 
former considers it to be the maximum element.
   
   ```
   n [9]: mx.nd.argmax(mx.nd.array([np.nan, 1]), axis=0)
   Out[9]:
   
   [1.]
   <NDArray 1 @cpu(0)>
   
   In [10]: mx.nd.topk(mx.nd.array([np.nan, 1]), axis=0)
   Out[10]:
   
   [0.]
   <NDArray 1 @cpu(0)>
   
   
   ``` 
   
   Arguably there was never a guarantee that nd.topk works under the presence 
of nan values, but legacy code may rely on this behavior and can break in 
unexpected ways.
   
   @asmushetzel

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to