mxnet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] szha commented on issue #13998: expand_dims() makes copy instead of simply reshaping
Date Fri, 01 Feb 2019 06:06:43 GMT
szha commented on issue #13998: expand_dims() makes copy instead of simply reshaping
URL: https://github.com/apache/incubator-mxnet/issues/13998#issuecomment-459617256
 
 
   @stephenrawls you're right that `expand_dims` and `squeeze` for NDArray should behave consistently
with `reshape`. Currently, `reshape` is done throught [a special C API](https://github.com/apache/incubator-mxnet/blob/master/python/mxnet/ndarray/ndarray.py#L1065-L1069)
which returns a modified view of the data, while the `expand_dims` and `squeeze` are making
a copy. For symbol (and thus the hybridized version), since in-place identity is possible
it should not matter.
   ```
   In [1]: import mxnet as mx
   
   In [2]: a = mx.nd.arange(6)
   
   In [3]: b = a.reshape(2, 3)
   
   In [4]: c = a.expand_dims(1)
   
   In [5]: a[1] = 7
   
   In [6]: b
   Out[6]:
   
   [[0. 7. 2.]
    [3. 4. 5.]]
   <NDArray 2x3 @cpu(0)>
   
   In [7]: c
   Out[7]:
   
   [[0.]
    [1.]
    [2.]
    [3.]
    [4.]
    [5.]]
   <NDArray 6x1 @cpu(0)>
   ```
   
   It's straightforward to let `expand_dims`/`squeeze` return a view too (e.g. by simply calling
`.shape` and `reshape` in `expand_dims` and `squeeze` in python/mxnet/ndarray/ndarray.py).

----------------------------------------------------------------
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

Mime
View raw message