Search code examples
python-3.xnumpy-ndarraymxnet

mxnet import nd or np to use arrays


I'm starting to study mxnet and gluon, but I'm getting somewhat confused about the usage of np/nd arrays.

  • As suggested on the gluon website, I installed mxnet and gluon by running:

    pip install --upgrade mxnet gluoncv
    

    which installs mxnet version 1.5.1.post0. In this case, to use arrays I need:

    from mxnet import ndarray as nd
    
  • On the other hand, I found a deep learning book based on mxnet, where they have you install a later mxnet version by:

    pip install mxnet==1.6.0b20190915
    

    and in this case, you can either import ndarray or directly np, so:

    from mxnet import ndarray as nd
    from mxnet import np
    

both work (while, with mxnet 1.5.1, from mxnet import np fails).

Why is it possible to import np in the new version, if we had nd already? Are there any differences between arrays created from nd or np?
It seems that I can use mxnet features (such as attach_grad()) in both cases...for example, the following works:

from mxnet import np
array = np.array([1,2,3)
array.attach_grad()

Thanks!


Solution

  • Here is the RFC explaining the motivation of introducing mx.np module. To give a couple of highlighted differences between mx.np and mx.nd modules:

    1. mx.np module adopts the official NumPy operator API, which has evolved for almost twenty years, to provide an easy transitioning experience for users coming from the NumPy world to deep learning, while the development of mx.nd module did not follow a well-established spec to define operator signatures, which sometimes caused confusions, e.g., dim vs axis.
    2. Operators registered in mx.np have highly compatible behaviors with the official NumPy operators, while mx.nd operators do not share such aspects. For example, zero-dim/scalar, zero-size, boolean tensors, and boolean indexing, are supported in mx.np, but not in mx.nd.

    You can consider mx.np as an enhanced version of mx.nd in terms of usability, functionality, and performance. mx.nd will be gradually deprecated in the future releases.