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 2018/01/18 19:38:54 UTC

[GitHub] aidan-plenert-macdonald commented on issue #9380: nd.stack causes abort for differently sized arrays

aidan-plenert-macdonald commented on issue #9380: nd.stack causes abort for differently sized arrays
URL: https://github.com/apache/incubator-mxnet/issues/9380#issuecomment-358758416
 
 
   @eric-haibin-lin So this wasn't so much about it doing the right thing as simply not killing Python. This error shouldn't be fatal. Not Numpy's behavior,
   
   ```
   $ python
   Python 3.6.3 (default, Oct  4 2017, 06:09:15) 
   [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin
   Type "help", "copyright", "credits" or "license" for more information.
   >>> import numpy as np
   >>> a = np.arange(20).reshape((20, 2))
   >>> a = np.arange(20).reshape((10, 2))
   >>> b = np.arange(10).reshape((10, 1))
   >>> np.stack([a, b])
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
     File "/usr/local/lib/python3.6/site-packages/numpy/core/shape_base.py", line 354, in stack
       raise ValueError('all input arrays must have the same shape')
   ValueError: all input arrays must have the same shape
   >>> np.hstack([a, b])
   array([[ 0,  1,  0],
          [ 2,  3,  1],
          [ 4,  5,  2],
          [ 6,  7,  3],
          [ 8,  9,  4],
          [10, 11,  5],
          [12, 13,  6],
          [14, 15,  7],
          [16, 17,  8],
          [18, 19,  9]])
   ```
   
   Numpy doesn't kill the session when it throws the error. But beyond that, MXNet lacks an `nd.hstack` function, so it would be nice if `nd.stack` could handle stacking of different shapes.
   
   For example, pseudocode outlining the shape assertions.
   ```python
   def stack(A, B, axis=0):
       for i, (s0, s1) in enumerate(zip(A.shape, B.shape)):
           if axis != i:
               assert A.shape[i] == B.shape[i]
       # Actually do the stacking
       C = actual_stacking(A, B, axis=axis)
       for i, (s0, s1) in enumerate(zip(A.shape, B.shape, C.shape)):
           if axis != i:
               assert A.shape[i] == B.shape[i]
               assert A.shape[i] == C.shape[i]
          else:
               assert C.shape[i] == A.shape[i] + B.shape[i]
       return C
   ```

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services