MindSpore Numpy package contains a set of Numpy-like interfaces, which allows developers to build models on MindSpore with similar syntax of Numpy.
MindSpore Numpy operators can be classified into four functional modules: `array generation`, `array operation`, `logic operation` and `math operation`.
Common imported modules in corresponding API examples are as follows:
MindSpore numpy provides a consistent programming experience with native numpy by assembling the low-level operators. Compared with MindSpore's function and ops interfaces, it is easier for user to understand and use. However, please notice that to be more compatible with native numpy, the performance of some MindSpore numpy interfaces may be weaker than the corresponding function/ops interfaces. Users can choose which to use as needed.
The following code implements the operation of averaging all the elements of `input_x`:
..code-block:: python
input_x = np.arange(6).astype('float32')
output = np.mean(input_x)
print(output)
The result is as follows:
..code-block::
2.5
- Exponential arithmetic
The following code implements the operation of the natural constant `e` to the power of `input_x`:
..code-block:: python
input_x = np.arange(5).astype('float32')
output = np.exp(input_x)
print(output)
The result is as follows:
..code-block::
[ 1. 2.7182817 7.389056 20.085537 54.59815 ]
..msplatformautosummary::
:toctree:numpy
:nosignatures:
:template:classtemplate_inherited.rst
mindspore.numpy.absolute
mindspore.numpy.add
mindspore.numpy.amax
mindspore.numpy.amin
mindspore.numpy.arccos
mindspore.numpy.arccosh
mindspore.numpy.arcsin
mindspore.numpy.arcsinh
mindspore.numpy.arctan
mindspore.numpy.arctan2
mindspore.numpy.arctanh
mindspore.numpy.argmax
mindspore.numpy.argmin
mindspore.numpy.around
mindspore.numpy.average
mindspore.numpy.bincount
mindspore.numpy.bitwise_and
mindspore.numpy.bitwise_or
mindspore.numpy.bitwise_xor
mindspore.numpy.cbrt
mindspore.numpy.ceil
mindspore.numpy.clip
mindspore.numpy.convolve
mindspore.numpy.copysign
mindspore.numpy.corrcoef
mindspore.numpy.correlate
mindspore.numpy.cos
mindspore.numpy.cosh
mindspore.numpy.count_nonzero
mindspore.numpy.cov
mindspore.numpy.cross
mindspore.numpy.cumprod
mindspore.numpy.cumsum
mindspore.numpy.deg2rad
mindspore.numpy.diff
mindspore.numpy.digitize
mindspore.numpy.divide
mindspore.numpy.divmod
mindspore.numpy.dot
mindspore.numpy.ediff1d
mindspore.numpy.exp
mindspore.numpy.exp2
mindspore.numpy.expm1
mindspore.numpy.fix
mindspore.numpy.float_power
mindspore.numpy.floor
mindspore.numpy.floor_divide
mindspore.numpy.fmod
mindspore.numpy.gcd
mindspore.numpy.gradient
mindspore.numpy.heaviside
mindspore.numpy.histogram
mindspore.numpy.histogram2d
mindspore.numpy.histogramdd
mindspore.numpy.hypot
mindspore.numpy.inner
mindspore.numpy.interp
mindspore.numpy.invert
mindspore.numpy.kron
mindspore.numpy.lcm
mindspore.numpy.log
mindspore.numpy.log10
mindspore.numpy.log1p
mindspore.numpy.log2
mindspore.numpy.logaddexp
mindspore.numpy.logaddexp2
mindspore.numpy.matmul
mindspore.numpy.matrix_power
mindspore.numpy.maximum
mindspore.numpy.mean
mindspore.numpy.minimum
mindspore.numpy.multi_dot
mindspore.numpy.multiply
mindspore.numpy.nancumsum
mindspore.numpy.nanmax
mindspore.numpy.nanmean
mindspore.numpy.nanmin
mindspore.numpy.nanstd
mindspore.numpy.nansum
mindspore.numpy.nanvar
mindspore.numpy.negative
mindspore.numpy.norm
mindspore.numpy.outer
mindspore.numpy.polyadd
mindspore.numpy.polyder
mindspore.numpy.polyint
mindspore.numpy.polymul
mindspore.numpy.polysub
mindspore.numpy.polyval
mindspore.numpy.positive
mindspore.numpy.power
mindspore.numpy.promote_types
mindspore.numpy.ptp
mindspore.numpy.rad2deg
mindspore.numpy.radians
mindspore.numpy.ravel_multi_index
mindspore.numpy.reciprocal
mindspore.numpy.remainder
mindspore.numpy.result_type
mindspore.numpy.rint
mindspore.numpy.searchsorted
mindspore.numpy.sign
mindspore.numpy.sin
mindspore.numpy.sinh
mindspore.numpy.sqrt
mindspore.numpy.square
mindspore.numpy.std
mindspore.numpy.subtract
mindspore.numpy.sum
mindspore.numpy.tan
mindspore.numpy.tanh
mindspore.numpy.tensordot
mindspore.numpy.trapz
mindspore.numpy.true_divide
mindspore.numpy.trunc
mindspore.numpy.unwrap
mindspore.numpy.var
Interact With MindSpore Functions
---------------------------------
Since `mindspore.numpy` directly wraps MindSpore tensors and operators, it has all the advantages and properties of MindSpore. In this section, we will briefly introduce how to employ MindSpore execution management and automatic differentiation in `mindspore.numpy` coding scenarios. These include:
In this function, MindSpore dispatches each computing kernel to device separately. However, with the help of `jit` decorator, we can compile all operations into a single static computing graph.
Most functions in `mindspore.numpy` can run in Graph Mode and PyNative Mode, and can run on CPU, GPU and Ascend. Like MindSpore, users can manage the execution mode using `mindspore.set_context`:
For more details, see `API mindspore.set_context <https://www.mindspore.cn/docs/en/master/api_python/mindspore/mindspore.set_context.html#mindspore.set_context>`_ .