Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
fix several numpy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
haojin2 committed Nov 19, 2019
1 parent bcb46b5 commit 37001e0
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 50 deletions.
44 changes: 40 additions & 4 deletions python/mxnet/ndarray/numpy/_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from . import _internal as _npi
from ..ndarray import NDArray

__all__ = ['zeros', 'ones', 'full', 'add', 'subtract', 'multiply', 'divide', 'mod', 'remainder', 'power',
__all__ = ['shape', 'zeros', 'ones', 'full', 'add', 'subtract', 'multiply', 'divide', 'mod', 'remainder', 'power',
'arctan2', 'sin', 'cos', 'tan', 'sinh', 'cosh', 'tanh', 'log10', 'sqrt', 'cbrt', 'abs',
'absolute', 'exp', 'expm1', 'arcsin', 'arccos', 'arctan', 'sign', 'log', 'degrees', 'log2',
'log1p', 'rint', 'radians', 'reciprocal', 'square', 'negative', 'fix', 'ceil', 'floor',
Expand All @@ -43,7 +43,41 @@


@set_module('mxnet.ndarray.numpy')
def zeros(shape, dtype=_np.float32, order='C', ctx=None):
def shape(a):
"""
Return the shape of an array.
Parameters
----------
a : array_like
Input array.
Returns
-------
shape : tuple of ints
The elements of the shape tuple give the lengths of the
corresponding array dimensions.
See Also
--------
ndarray.shape : Equivalent array method.
Examples
--------
>>> np.shape(np.eye(3))
(3, 3)
>>> np.shape([[1, 2]])
(1, 2)
>>> np.shape([0])
(1,)
>>> np.shape(0)
()
"""
return a.shape


@set_module('mxnet.ndarray.numpy')
def zeros(shape, dtype=_np.float32, order='C', ctx=None): # pylint: disable=redefined-outer-name
"""Return a new array of given shape and type, filled with zeros.
This function currently only supports storing multi-dimensional data
in row-major (C-style).
Expand Down Expand Up @@ -77,7 +111,7 @@ def zeros(shape, dtype=_np.float32, order='C', ctx=None):


@set_module('mxnet.ndarray.numpy')
def ones(shape, dtype=_np.float32, order='C', ctx=None):
def ones(shape, dtype=_np.float32, order='C', ctx=None): # pylint: disable=redefined-outer-name
"""Return a new array of given shape and type, filled with ones.
This function currently only supports storing multi-dimensional data
in row-major (C-style).
Expand Down Expand Up @@ -110,8 +144,9 @@ def ones(shape, dtype=_np.float32, order='C', ctx=None):
return _npi.ones(shape=shape, ctx=ctx, dtype=dtype)


# pylint: disable=too-many-arguments, redefined-outer-name
@set_module('mxnet.ndarray.numpy')
def full(shape, fill_value, dtype=None, order='C', ctx=None, out=None): # pylint: disable=too-many-arguments
def full(shape, fill_value, dtype=None, order='C', ctx=None, out=None):
"""
Return a new array of given shape and type, filled with `fill_value`.
Parameters
Expand Down Expand Up @@ -163,6 +198,7 @@ def full(shape, fill_value, dtype=None, order='C', ctx=None, out=None): # pylin
ctx = current_context()
dtype = _np.float32 if dtype is None else dtype
return _npi.full(shape=shape, value=fill_value, ctx=ctx, dtype=dtype, out=out)
# pylint: enable=too-many-arguments, redefined-outer-name


@set_module('mxnet.ndarray.numpy')
Expand Down
73 changes: 57 additions & 16 deletions python/mxnet/numpy/multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from ..ndarray.numpy import _internal as _npi
from ..ndarray.ndarray import _storage_type

__all__ = ['ndarray', 'empty', 'array', 'zeros', 'ones', 'full', 'add', 'subtract', 'multiply', 'divide',
__all__ = ['ndarray', 'empty', 'array', 'shape', 'zeros', 'ones', 'full', 'add', 'subtract', 'multiply', 'divide',
'mod', 'remainder', 'power', 'arctan2', 'sin', 'cos', 'tan', 'sinh', 'cosh', 'tanh', 'log10',
'sqrt', 'cbrt', 'abs', 'absolute', 'exp', 'expm1', 'arcsin', 'arccos', 'arctan', 'sign', 'log',
'degrees', 'log2', 'log1p', 'rint', 'radians', 'reciprocal', 'square', 'negative',
Expand All @@ -67,7 +67,7 @@

# This function is copied from ndarray.py since pylint
# keeps giving false alarm error of undefined-all-variable
def _new_alloc_handle(shape, ctx, delay_alloc, dtype=mx_real_t):
def _new_alloc_handle(shape, ctx, delay_alloc, dtype=mx_real_t): # pylint: disable=redefined-outer-name
"""Return a new handle with specified shape and context.
Empty handle is only used to hold results.
Expand All @@ -89,7 +89,7 @@ def _new_alloc_handle(shape, ctx, delay_alloc, dtype=mx_real_t):
return hdl


def _reshape_view(a, *shape):
def _reshape_view(a, *shape): # pylint: disable=redefined-outer-name
"""Returns a **view** of this array with a new shape without altering any data.
Parameters
Expand Down Expand Up @@ -462,7 +462,7 @@ def __getitem__(self, key):
"""
# handling possible boolean indexing first
ndim = self.ndim
shape = self.shape
shape = self.shape # pylint: disable=redefined-outer-name

if isinstance(key, list):
try:
Expand Down Expand Up @@ -804,7 +804,7 @@ def __int__(self):

def __len__(self):
"""Number of elements along the first axis."""
shape = self.shape
shape = self.shape # pylint: disable=redefined-outer-name
if len(shape) == 0:
raise TypeError('len() of unsized object')
return self.shape[0]
Expand Down Expand Up @@ -1165,7 +1165,7 @@ def reshape_like(self, *args, **kwargs):
"""
raise AttributeError('mxnet.numpy.ndarray object has no attribute reshape_like')

def reshape_view(self, *shape, **kwargs):
def reshape_view(self, *shape, **kwargs): # pylint: disable=redefined-outer-name
"""Returns a **view** of this array with a new shape without altering any data.
Inheritated from NDArray.reshape.
"""
Expand Down Expand Up @@ -1519,13 +1519,15 @@ def mean(self, axis=None, dtype=None, out=None, keepdims=False): # pylint: disa
"""Returns the average of the array elements along given axis."""
return mean(self, axis=axis, dtype=dtype, out=out, keepdims=keepdims)

def std(self, axis=None, dtype=None, out=None, ddof=0, keepdims=False): # pylint: disable=arguments-differ
# pylint: disable=too-many-arguments, arguments-differ
def std(self, axis=None, dtype=None, out=None, ddof=0, keepdims=False):
"""Returns the standard deviation of the array elements along given axis."""
return std(self, axis=axis, dtype=dtype, ddof=ddof, keepdims=keepdims, out=out)

def var(self, axis=None, dtype=None, out=None, ddof=0, keepdims=False): # pylint: disable=arguments-differ
def var(self, axis=None, dtype=None, out=None, ddof=0, keepdims=False):
"""Returns the variance of the array elements, along given axis."""
return var(self, axis=axis, dtype=dtype, out=out, ddof=ddof, keepdims=keepdims)
# pylint: enable=too-many-arguments, arguments-differ

def cumsum(self, axis=None, dtype=None, out=None):
"""Return the cumulative sum of the elements along the given axis."""
Expand Down Expand Up @@ -1854,7 +1856,7 @@ def squeeze(self, axis=None): # pylint: disable=arguments-differ
"""Remove single-dimensional entries from the shape of a."""
return _mx_np_op.squeeze(self, axis=axis)

def broadcast_to(self, shape):
def broadcast_to(self, shape): # pylint: disable=redefined-outer-name
return _mx_np_op.broadcast_to(self, shape)

def broadcast_like(self, other):
Expand Down Expand Up @@ -1916,7 +1918,7 @@ def tostype(self, stype):


@set_module('mxnet.numpy')
def empty(shape, dtype=_np.float32, order='C', ctx=None):
def empty(shape, dtype=_np.float32, order='C', ctx=None): # pylint: disable=redefined-outer-name
"""Return a new array of given shape and type, without initializing entries.
Parameters
Expand Down Expand Up @@ -2020,7 +2022,41 @@ def array(object, dtype=None, ctx=None):


@set_module('mxnet.numpy')
def zeros(shape, dtype=_np.float32, order='C', ctx=None):
def shape(a):
"""
Return the shape of an array.
Parameters
----------
a : array_like
Input array.
Returns
-------
shape : tuple of ints
The elements of the shape tuple give the lengths of the
corresponding array dimensions.
See Also
--------
ndarray.shape : Equivalent array method.
Examples
--------
>>> np.shape(np.eye(3))
(3, 3)
>>> np.shape([[1, 2]])
(1, 2)
>>> np.shape([0])
(1,)
>>> np.shape(0)
()
"""
return _mx_nd_np.shape(a)


@set_module('mxnet.numpy')
def zeros(shape, dtype=_np.float32, order='C', ctx=None): # pylint: disable=redefined-outer-name
"""Return a new array of given shape and type, filled with zeros.
This function currently only supports storing multi-dimensional data
in row-major (C-style).
Expand Down Expand Up @@ -2061,7 +2097,7 @@ def zeros(shape, dtype=_np.float32, order='C', ctx=None):


@set_module('mxnet.numpy')
def ones(shape, dtype=_np.float32, order='C', ctx=None):
def ones(shape, dtype=_np.float32, order='C', ctx=None): # pylint: disable=redefined-outer-name
"""Return a new array of given shape and type, filled with ones.
This function currently only supports storing multi-dimensional data
in row-major (C-style).
Expand Down Expand Up @@ -2106,8 +2142,9 @@ def ones(shape, dtype=_np.float32, order='C', ctx=None):
return _mx_nd_np.ones(shape, dtype, order, ctx)


# pylint: disable=too-many-arguments, redefined-outer-name
@set_module('mxnet.numpy')
def full(shape, fill_value, dtype=None, order='C', ctx=None, out=None): # pylint: disable=too-many-arguments
def full(shape, fill_value, dtype=None, order='C', ctx=None, out=None):
"""
Return a new array of given shape and type, filled with `fill_value`.
Expand Down Expand Up @@ -2160,6 +2197,7 @@ def full(shape, fill_value, dtype=None, order='C', ctx=None, out=None): # pylin
[2, 2]], dtype=int32)
"""
return _mx_nd_np.full(shape, fill_value, order=order, ctx=ctx, dtype=dtype, out=out)
# pylint: enable=too-many-arguments, redefined-outer-name


@set_module('mxnet.numpy')
Expand Down Expand Up @@ -4224,7 +4262,7 @@ def tensordot(a, b, axes=2):


@set_module('mxnet.numpy')
def histogram(a, bins=10, range=None, normed=None, weights=None, density=None): # pylint-disable=too-many-arguments
def histogram(a, bins=10, range=None, normed=None, weights=None, density=None): # pylint: disable=too-many-arguments
"""
Compute the histogram of a set of data.
Expand Down Expand Up @@ -4383,6 +4421,7 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis
return _mx_nd_np.linspace(start, stop, num, endpoint, retstep, dtype, axis, ctx)


# pylint: disable=too-many-arguments
@set_module('mxnet.numpy')
def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0, ctx=None):
r"""Return numbers spaced evenly on a log scale.
Expand Down Expand Up @@ -4457,6 +4496,7 @@ def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0,
array([ 100. , 215.44347, 464.15887, 1000. ], ctx=gpu(0))
"""
return _mx_nd_np.logspace(start, stop, num, endpoint, base, dtype, axis, ctx=ctx)
# pylint: enable=too-many-arguments


@set_module('mxnet.numpy')
Expand Down Expand Up @@ -5421,8 +5461,9 @@ def mean(a, axis=None, dtype=None, out=None, keepdims=False): # pylint: disable
return _npi.mean(a, axis=axis, dtype=dtype, keepdims=keepdims, out=out)



@set_module('mxnet.numpy')
def std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False):
def std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False): # pylint: disable=too-many-arguments
"""
Compute the standard deviation along the specified axis.
Returns the standard deviation, a measure of the spread of a distribution,
Expand Down Expand Up @@ -5489,7 +5530,7 @@ def std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False):


@set_module('mxnet.numpy')
def var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False):
def var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False): # pylint: disable=too-many-arguments
"""
Compute the variance along the specified axis.
Returns the variance of the array elements, a measure of the spread of a
Expand Down
29 changes: 19 additions & 10 deletions python/mxnet/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
'subok': True,
}

_set_np_shape_logged = False
_set_np_array_logged = False


def makedirs(d):
"""Create directories recursively if they don't exist. os.makedirs(exist_ok=True) is not
Expand Down Expand Up @@ -87,13 +90,16 @@ def set_np_shape(active):
>>> print(mx.is_np_shape())
True
"""
global _set_np_shape_logged
if active:
import logging
logging.info('NumPy-shape semantics has been activated in your code. '
'This is required for creating and manipulating scalar and zero-size '
'tensors, which were not supported in MXNet before, as in the official '
'NumPy library. Please DO NOT manually deactivate this semantics while '
'using `mxnet.numpy` and `mxnet.numpy_extension` modules.')
if not _set_np_shape_logged:
import logging
logging.info('NumPy-shape semantics has been activated in your code. '
'This is required for creating and manipulating scalar and zero-size '
'tensors, which were not supported in MXNet before, as in the official '
'NumPy library. Please DO NOT manually deactivate this semantics while '
'using `mxnet.numpy` and `mxnet.numpy_extension` modules.')
_set_np_shape_logged = True
elif is_np_array():
raise ValueError('Deactivating NumPy shape semantics while NumPy array semantics is still'
' active is not allowed. Please consider calling `npx.reset_np()` to'
Expand Down Expand Up @@ -678,11 +684,14 @@ def _set_np_array(active):
-------
A bool value indicating the previous state of NumPy array semantics.
"""
global _set_np_array_logged
if active:
import logging
logging.info('NumPy array semantics has been activated in your code. This allows you'
' to use operators from MXNet NumPy and NumPy Extension modules as well'
' as MXNet NumPy `ndarray`s.')
if not _set_np_array_logged:
import logging
logging.info('NumPy array semantics has been activated in your code. This allows you'
' to use operators from MXNet NumPy and NumPy Extension modules as well'
' as MXNet NumPy `ndarray`s.')
_set_np_array_logged = True
cur_state = is_np_array()
_NumpyArrayScope._current.value = _NumpyArrayScope(active)
return cur_state
Expand Down
32 changes: 16 additions & 16 deletions tests/python/unittest/test_numpy_interoperability.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,24 @@ def get_mat(n):
A = np.array([[1, 2], [3, 4], [5, 6]])
vals = (100 * np.arange(5)).astype('l')
vals_c = (100 * np.array(get_mat(5)) + 1).astype('l')
vals_f = _np.array((100 * get_mat(5) + 1), order ='F', dtype ='l')
vals_f = _np.array((100 * get_mat(5) + 1), order='F', dtype='l')
vals_f = np.array(vals_f)

OpArgMngr.add_workload('diag', A, k= 2)
OpArgMngr.add_workload('diag', A, k= 1)
OpArgMngr.add_workload('diag', A, k= 0)
OpArgMngr.add_workload('diag', A, k= -1)
OpArgMngr.add_workload('diag', A, k= -2)
OpArgMngr.add_workload('diag', A, k= -3)
OpArgMngr.add_workload('diag', vals, k= 0)
OpArgMngr.add_workload('diag', vals, k= 2)
OpArgMngr.add_workload('diag', vals, k= -2)
OpArgMngr.add_workload('diag', vals_c, k= 0)
OpArgMngr.add_workload('diag', vals_c, k= 2)
OpArgMngr.add_workload('diag', vals_c, k= -2)
OpArgMngr.add_workload('diag', vals_f, k= 0)
OpArgMngr.add_workload('diag', vals_f, k= 2)
OpArgMngr.add_workload('diag', vals_f, k= -2)
OpArgMngr.add_workload('diag', A, k=2)
OpArgMngr.add_workload('diag', A, k=1)
OpArgMngr.add_workload('diag', A, k=0)
OpArgMngr.add_workload('diag', A, k=-1)
OpArgMngr.add_workload('diag', A, k=-2)
OpArgMngr.add_workload('diag', A, k=-3)
OpArgMngr.add_workload('diag', vals, k=0)
OpArgMngr.add_workload('diag', vals, k=2)
OpArgMngr.add_workload('diag', vals, k=-2)
OpArgMngr.add_workload('diag', vals_c, k=0)
OpArgMngr.add_workload('diag', vals_c, k=2)
OpArgMngr.add_workload('diag', vals_c, k=-2)
OpArgMngr.add_workload('diag', vals_f, k=0)
OpArgMngr.add_workload('diag', vals_f, k=2)
OpArgMngr.add_workload('diag', vals_f, k=-2)


def _add_workload_concatenate(array_pool):
Expand Down
Loading

0 comments on commit 37001e0

Please sign in to comment.