You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2020/01/15 14:52:55 UTC

[GitHub] [incubator-mxnet] ZheyuYe opened a new issue #17327: np.ndarray indexing after hybridize

ZheyuYe opened a new issue #17327: np.ndarray indexing after hybridize
URL: https://github.com/apache/incubator-mxnet/issues/17327
 
 
   ## Description
   Indexing is an important feature of numpy that supports complex positioning and value operations as [Numpy | Indexing](https://www.geeksforgeeks.org/numpy-indexing/). The mxnet deepnumpy only supply the basic indexing operation as
   ```
   gathered_data = sequence[indices_x, indices_y]
   ```
   whereas the above operation will fail after hybridize, see [here](https://github.com/apache/incubator-mxnet/pull/16621). Currently, the more effective method is only [mx.npx.pick](https://numpy.mxnet.io/api/deepnumpy/generated/mxnet.npx.pick.html), but it only supports one-dimensional selection instead of providing gather_nd operation( for nd.ndarray) can select array elements multidimensionally.
   
   The temporary solution would be adding the gather_nd operation for mx.np.ndarray, and the hybridized indexing opertaion could be added in future work.
   @sxjscience @haojin2 

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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] sxjscience commented on issue #17327: np.ndarray indexing after hybridize

Posted by GitBox <gi...@apache.org>.
sxjscience commented on issue #17327: np.ndarray indexing after hybridize
URL: https://github.com/apache/incubator-mxnet/issues/17327#issuecomment-574875177
 
 
   @ZheyuYe In terms of `mx.npx.pick` we can support multiple-dimensional selection as long as we are selecting the leading ones:
   
   ```python
   import mxnet as mx
   import mxnet.numpy as np
   mx.npx.set_np()
   a = mx.np.random.uniform(-1, 1, (10, 10, 5))
   b = np.random.randint(0, 5, (10,)).broadcast_to((10, 10))
   mx.npx.pick(a, b) 
   ```
   
   Also, would you provide a reproducible code snippet (like above) to mention which type of indexing operation is missing?
   
   
   

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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] ZheyuYe edited a comment on issue #17327: np.ndarray indexing after hybridize

Posted by GitBox <gi...@apache.org>.
ZheyuYe edited a comment on issue #17327: np.ndarray indexing after hybridize
URL: https://github.com/apache/incubator-mxnet/issues/17327#issuecomment-575180346
 
 
   @sxjscience It would be great if hybridized indexing could be supported as the last line in the code snippet.
   
   ```
   import numpy as np
   import mxnet as mx
   import numpy.testing as npt
   
   # new implementation in deep numpy
   mx.npx.set_np()
   sequence = mx.np.array(np.random.normal(0, 1, (8, 32, 768)), dtype=np.float32)
   # pick_ids: [batch_size, picked_index]
   pick_ids = mx.np.random.randint(0, 31, (8,2), dtype=np.int32)
   
   idx_arange = mx.npx.arange_like(pick_ids.reshape((-1, )), axis=0)
   batch_idx = mx.np.floor(idx_arange / 2).astype(np.int32)
   
   encoded = sequence[batch_idx, pick_ids.reshape((-1,))]
   ```
   I was aimed to pick the items from the sequence with shape (8, 2, 768) whereas the `mn.npx.pick` can not handle it. Under the deep numpy enviorment, I used the basic indexing as numpy operation as `encoded = sequence[batch_idx, pick_ids.reshape((-1,))]` which would fail after hybridize() raising the below **Error**
   
   > IndexError: Only integer, slice, or tuple of these types are supported! Received key=(<_Symbol albertmodel0_floor0>, <_Symbol albertmodel0_reshape4>)
   
   The full testing code can be found in [here](https://gist.github.com/ZheyuYe/63862dfea57dea95ea32ac8b51741c5e)

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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] ZheyuYe commented on issue #17327: np.ndarray indexing after hybridize

Posted by GitBox <gi...@apache.org>.
ZheyuYe commented on issue #17327: np.ndarray indexing after hybridize
URL: https://github.com/apache/incubator-mxnet/issues/17327#issuecomment-575180346
 
 
   @sxjscience 
   
   ```
   import numpy as np
   import mxnet as mx
   import numpy.testing as npt
   
   sequence = mx.nd.array(np.random.normal(0, 1, (8, 32, 768)), dtype=np.float32)
   # pick_ids: [batch_size, picked_index]
   pick_ids = mx.nd.random.randint(0, 31, (8,2), dtype=np.int32)
   
   # original approach with nd.ndarray
   idx_arange = mx.nd.contrib.arange_like(pick_ids.reshape((-1, )), axis=0)
   batch_idx = mx.nd.broadcast_div(idx_arange, mx.nd.array([2],dtype=np.int32))
   # batch_idx_1d = [0,0,1,1,2,2,...]
   # pick_ids_1d = [1,2,4,0,3,4,2,3,5...]
   batch_idx_1d = batch_idx.reshape((1, -1))
   pick_ids_1d = pick_ids.reshape((1, -1))
   position_idx = mx.nd.concat(batch_idx_1d, pick_ids_1d, dim=0)
   encoded = mx.nd.gather_nd(sequence, position_idx)
   ground_truth = encoded.reshape_like(pick_ids, lhs_begin=-2, lhs_end=-1, rhs_begin=0)
   
   # new implementation in deep numpy
   mx.npx.set_np()
   sequence = sequence.as_np_ndarray()
   pick_ids = pick_ids.as_np_ndarray()
   
   idx_arange = mx.npx.arange_like(pick_ids.reshape((-1, )), axis=0)
   batch_idx = mx.np.floor(idx_arange / 2).astype(np.int32)
   
   encoded = sequence[batch_idx, pick_ids.reshape((-1,))]
   np_out = mx.npx.reshape_like(encoded, pick_ids, lhs_begin=-2, lhs_end=-1, rhs_begin=0)
   
   npt.assert_allclose(ground_truth.asnumpy(), np_out.asnumpy(), rtol=1e-5, atol=1e-5)
   ```
   I was aimed to pick the items from the sequence with shape (8, 2, 768) whereas the `mn.npx.pick` can not handle it. Under the deep numpy enviorment, I used the basic indexing as numpy operation as `encoded = sequence[batch_idx, pick_ids.reshape((-1,))]` which would fail after hybridize() raising the below **Error**
   
   > IndexError: Only integer, slice, or tuple of these types are supported! Received key=(<_Symbol albertmodel0_floor0>, <_Symbol albertmodel0_reshape4>)

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


With regards,
Apache Git Services