From 507867d866a279ba173e9a75c4c831cadeb63f6c Mon Sep 17 00:00:00 2001 From: Yiyan66 Date: Mon, 30 Dec 2019 08:52:18 +0000 Subject: [PATCH 1/6] add op --- python/mxnet/ndarray/numpy/_op.py | 123 ++++++++++++++++- python/mxnet/numpy/multiarray.py | 128 +++++++++++++++++- python/mxnet/numpy_dispatch_protocol.py | 2 + python/mxnet/symbol/numpy/_symbol.py | 82 ++++++++++- .../unittest/test_numpy_interoperability.py | 14 ++ tests/python/unittest/test_numpy_op.py | 54 ++++++++ 6 files changed, 390 insertions(+), 13 deletions(-) diff --git a/python/mxnet/ndarray/numpy/_op.py b/python/mxnet/ndarray/numpy/_op.py index 0fcfe8a6e54d..2a6f93694a7b 100644 --- a/python/mxnet/ndarray/numpy/_op.py +++ b/python/mxnet/ndarray/numpy/_op.py @@ -37,10 +37,11 @@ 'logspace', 'expand_dims', 'tile', 'arange', 'array_split', 'split', 'vsplit', 'concatenate', 'append', 'stack', 'vstack', 'column_stack', 'dstack', 'average', 'mean', 'maximum', 'minimum', 'swapaxes', 'clip', 'argmax', 'argmin', 'std', 'var', 'indices', 'copysign', 'ravel', 'unravel_index', 'hanning', 'hamming', - 'blackman', 'flip', 'around', 'round', 'hypot', 'bitwise_xor', 'bitwise_or', 'rad2deg', 'deg2rad', - 'unique', 'lcm', 'tril', 'identity', 'take', 'ldexp', 'vdot', 'inner', 'outer', 'equal', 'not_equal', - 'greater', 'less', 'greater_equal', 'less_equal', 'hsplit', 'rot90', 'einsum', 'true_divide', 'nonzero', - 'shares_memory', 'may_share_memory', 'diff', 'resize', 'nan_to_num', 'where', 'bincount'] + 'blackman', 'flip', 'flipud', 'fliplr', 'around', 'round', 'hypot', 'bitwise_xor', 'bitwise_or', + 'rad2deg', 'deg2rad', 'unique', 'lcm', 'tril', 'identity', 'take', 'ldexp', 'vdot', 'inner', 'outer', + 'equal', 'not_equal', 'greater', 'less', 'greater_equal', 'less_equal', 'hsplit', 'rot90', 'einsum', + 'true_divide', 'nonzero', 'shares_memory', 'may_share_memory', 'diff', 'resize', 'nan_to_num', 'where', + 'bincount'] @set_module('mxnet.ndarray.numpy') @@ -4813,6 +4814,120 @@ def flip(m, axis=None, out=None): raise TypeError('type {} not supported'.format(str(type(m)))) +@set_module('mxnet.ndarray.numpy') +def flipud(m): + r""" + flipud(*args, **kwargs) + + Flip array in the up/down direction. + + Flip the entries in each column in the up/down direction. + Rows are preserved, but appear in a different order than before. + + Parameters + ---------- + m : array_like + Input array. + + Returns + ------- + out : array_like + A view of `m` with the rows reversed. Since a view is + returned, this operation is :math:`\mathcal O(1)`. + + See Also + -------- + fliplr : Flip array in the left/right direction. + rot90 : Rotate array counterclockwise. + + Notes + ----- + Equivalent to ``m[::-1,...]``. + Does not require the array to be two-dimensional. + + Examples + -------- + >>> A = np.diag(np.array([1.0, 2, 3])) + >>> A + array([[1., 0., 0.], + [0., 2., 0.], + [0., 0., 3.]]) + >>> np.flipud(A) + array([[0., 0., 3.], + [0., 2., 0.], + [1., 0., 0.]]) + + >>> A = np.random.randn(2,3,5) + >>> np.all(np.flipud(A) == A[::-1,...]) + array(True) + + >>> np.flipud(np.array([1,2])) + array([2., 1.]) + """ + from ...numpy import ndarray + if isinstance(m, numeric_types): + return _np.flip(m, 0) + elif isinstance(m, ndarray): + return _npi.flip(m, 0) + else: + raise TypeError('type {} not supported'.format(str(type(m)))) + + +@set_module('mxnet.ndarray.numpy') +def fliplr(m): + r""" + fliplr(*args, **kwargs) + + Flip array in the left/right direction. + + Flip the entries in each row in the left/right direction. + Columns are preserved, but appear in a different order than before. + + Parameters + ---------- + m : array_like + Input array, must be at least 2-D. + + Returns + ------- + f : ndarray + A view of `m` with the columns reversed. Since a view + is returned, this operation is :math:`\mathcal O(1)`. + + See Also + -------- + flipud : Flip array in the up/down direction. + rot90 : Rotate array counterclockwise. + + Notes + ----- + Equivalent to m[:,::-1]. Requires the array to be at least 2-D. + + Examples + -------- + >>> A = np.diag(np.array([1.,2.,3.])) + >>> A + array([[1., 0., 0.], + [0., 2., 0.], + [0., 0., 3.]]) + >>> np.fliplr(A) + array([[0., 0., 1.], + [0., 2., 0.], + [3., 0., 0.]]) + + >>> A = np.random.randn(2,3,5) + >>> np.all(np.fliplr(A) == A[:,::-1,...]) + array(True) + """ + from ...numpy import ndarray + if isinstance(m, numeric_types): + return _np.flip(m, 1) + elif isinstance(m, ndarray): + return _npi.flip(m, 1) + else: + raise TypeError('type {} not supported'.format(str(type(m)))) + + @set_module('mxnet.ndarray.numpy') def around(x, decimals=0, out=None, **kwargs): r""" diff --git a/python/mxnet/numpy/multiarray.py b/python/mxnet/numpy/multiarray.py index 8993b03eab08..68c0b8172ef3 100644 --- a/python/mxnet/numpy/multiarray.py +++ b/python/mxnet/numpy/multiarray.py @@ -56,11 +56,11 @@ 'tensordot', 'eye', 'linspace', 'logspace', 'expand_dims', 'tile', 'arange', 'array_split', 'split', 'vsplit', 'concatenate', 'stack', 'vstack', 'column_stack', 'dstack', 'average', 'mean', 'maximum', 'minimum', 'swapaxes', 'clip', 'argmax', 'argmin', 'std', 'var', 'indices', 'copysign', - 'ravel', 'unravel_index', 'hanning', 'hamming', 'blackman', 'flip', 'around', 'round', 'arctan2', - 'hypot', 'bitwise_xor', 'bitwise_or', 'rad2deg', 'deg2rad', 'unique', 'lcm', 'tril', 'identity', - 'take', 'ldexp', 'vdot', 'inner', 'outer', 'equal', 'not_equal', 'greater', 'less', 'greater_equal', - 'less_equal', 'hsplit', 'rot90', 'einsum', 'true_divide', 'nonzero', 'shares_memory', - 'may_share_memory', 'diff', 'resize', 'nan_to_num', 'where', 'bincount'] + 'ravel', 'unravel_index', 'hanning', 'hamming', 'blackman', 'flip', 'flipud', 'fliplr', 'around', + 'round', 'arctan2', 'hypot', 'bitwise_xor', 'bitwise_or', 'rad2deg', 'deg2rad', 'unique', 'lcm', + 'tril', 'identity', 'take', 'ldexp', 'vdot', 'inner', 'outer', 'equal', 'not_equal', 'greater', + 'less', 'greater_equal', 'less_equal', 'hsplit', 'rot90', 'einsum', 'true_divide', 'nonzero', + 'shares_memory', 'may_share_memory', 'diff', 'resize', 'nan_to_num', 'where', 'bincount'] # Return code for dispatching indexing function call _NDARRAY_UNSUPPORTED_INDEXING = -1 @@ -1470,6 +1470,22 @@ def flip(self, *args, **kwargs): """ raise AttributeError('mxnet.numpy.ndarray object has no attribute flip') + def flipud(self, *args, **kwargs): + """Convenience fluent method for :py:func:`flipud`. + + The arguments are the same as for :py:func:`flipud`, with + this array as data. + """ + raise AttributeError('mxnet.numpy.ndarray object has no attribute flipud') + + def flip(self, *args, **kwargs): + """Convenience fluent method for :py:func:`fliplr`. + + The arguments are the same as for :py:func:`fliplr`, with + this array as data. + """ + raise AttributeError('mxnet.numpy.ndarray object has no attribute fliplr') + def depth_to_space(self, *args, **kwargs): """Convenience fluent method for :py:func:`depth_to_space`. @@ -6513,6 +6529,108 @@ def flip(m, axis=None, out=None): return _mx_nd_np.flip(m, axis, out=out) +@set_module('mxnet.numpy') +def flipud(m): + r""" + flipud(*args, **kwargs) + + Flip array in the up/down direction. + + Flip the entries in each column in the up/down direction. + Rows are preserved, but appear in a different order than before. + + Parameters + ---------- + m : array_like + Input array. + + Returns + ------- + out : array_like + A view of `m` with the rows reversed. Since a view is + returned, this operation is :math:`\mathcal O(1)`. + + See Also + -------- + fliplr : Flip array in the left/right direction. + rot90 : Rotate array counterclockwise. + + Notes + ----- + Equivalent to ``m[::-1,...]``. + Does not require the array to be two-dimensional. + + Examples + -------- + >>> A = np.diag(np.array([1.0, 2, 3])) + >>> A + array([[1., 0., 0.], + [0., 2., 0.], + [0., 0., 3.]]) + >>> np.flipud(A) + array([[0., 0., 3.], + [0., 2., 0.], + [1., 0., 0.]]) + + >>> A = np.random.randn(2,3,5) + >>> np.all(np.flipud(A) == A[::-1,...]) + array(True) + + >>> np.flipud(np.array([1,2])) + array([2., 1.]) + """ + return _mx_nd_np.flip(m, 0) + + +@set_module('mxnet.numpy') +def fliplr(m): + r""" + fliplr(*args, **kwargs) + + Flip array in the left/right direction. + + Flip the entries in each row in the left/right direction. + Columns are preserved, but appear in a different order than before. + + Parameters + ---------- + m : array_like + Input array, must be at least 2-D. + + Returns + ------- + f : ndarray + A view of `m` with the columns reversed. Since a view + is returned, this operation is :math:`\mathcal O(1)`. + + See Also + -------- + flipud : Flip array in the up/down direction. + rot90 : Rotate array counterclockwise. + + Notes + ----- + Equivalent to m[:,::-1]. Requires the array to be at least 2-D. + + Examples + -------- + >>> A = np.diag([1.,2.,3.]) + >>> A + array([[1., 0., 0.], + [0., 2., 0.], + [0., 0., 3.]]) + >>> np.fliplr(A) + array([[0., 0., 1.], + [0., 2., 0.], + [3., 0., 0.]]) + + >>> A = np.random.randn(2,3,5) + >>> np.all(np.fliplr(A) == A[:,::-1,...]) + array(True) + """ + return _mx_nd_np.flip(m, 1) + + @set_module('mxnet.numpy') def around(x, decimals=0, out=None, **kwargs): r""" diff --git a/python/mxnet/numpy_dispatch_protocol.py b/python/mxnet/numpy_dispatch_protocol.py index db1e2684d7a4..56a5dd5ac813 100644 --- a/python/mxnet/numpy_dispatch_protocol.py +++ b/python/mxnet/numpy_dispatch_protocol.py @@ -104,6 +104,8 @@ def _run_with_array_ufunc_proto(*args, **kwargs): 'expand_dims', 'fix', 'flip', + 'flipud', + 'fliplr', 'inner', 'max', 'amax', diff --git a/python/mxnet/symbol/numpy/_symbol.py b/python/mxnet/symbol/numpy/_symbol.py index 0340a5d2220f..7b3937f0c830 100644 --- a/python/mxnet/symbol/numpy/_symbol.py +++ b/python/mxnet/symbol/numpy/_symbol.py @@ -45,10 +45,10 @@ 'logspace', 'expand_dims', 'tile', 'arange', 'array_split', 'split', 'vsplit', 'concatenate', 'append', 'stack', 'vstack', 'column_stack', 'dstack', 'average', 'mean', 'maximum', 'minimum', 'swapaxes', 'clip', 'argmax', 'argmin', 'std', 'var', 'indices', 'copysign', 'ravel', 'unravel_index', 'hanning', 'hamming', - 'blackman', 'flip', 'around', 'round', 'hypot', 'bitwise_xor', 'bitwise_or', 'rad2deg', 'deg2rad', - 'unique', 'lcm', 'tril', 'identity', 'take', 'ldexp', 'vdot', 'inner', 'outer', 'equal', 'not_equal', - 'greater', 'less', 'greater_equal', 'less_equal', 'hsplit', 'rot90', 'einsum', 'true_divide', - 'shares_memory', 'may_share_memory', 'diff', 'resize', 'nan_to_num', 'where', 'bincount'] + 'blackman', 'flip', 'flipud', 'fliplr', 'around', 'round', 'hypot', 'bitwise_xor', 'bitwise_or', + 'rad2deg', 'deg2rad', 'unique', 'lcm', 'tril', 'identity', 'take', 'ldexp', 'vdot', 'inner', 'outer', + 'equal', 'not_equal', 'greater', 'less', 'greater_equal', 'less_equal', 'hsplit', 'rot90', 'einsum', + 'true_divide', 'shares_memory', 'may_share_memory', 'diff', 'resize', 'nan_to_num', 'where', 'bincount'] @set_module('mxnet.symbol.numpy') @@ -585,6 +585,22 @@ def flip(self, *args, **kwargs): """ raise AttributeError('_Symbol object has no attribute flip') + def flipud(self, *args, **kwargs): + """Convenience fluent method for :py:func:`flipud`. + + The arguments are the same as for :py:func:`flipud`, with + this array as data. + """ + raise AttributeError('_Symbol object has no attribute flipud') + + def fliplr(self, *args, **kwargs): + """Convenience fluent method for :py:func:`fliplr`. + + The arguments are the same as for :py:func:`fliplr`, with + this array as data. + """ + raise AttributeError('_Symbol object has no attribute fliplr') + def depth_to_space(self, *args, **kwargs): """Convenience fluent method for :py:func:`depth_to_space`. @@ -4581,6 +4597,64 @@ def flip(m, axis=None, out=None): raise TypeError('type {} not supported'.format(str(type(m)))) +@set_module('mxnet.symbol.numpy') +def flipud(m): + r""" + flipud(*args, **kwargs) + + Flip array in the up/down direction. + + Flip the entries in each column in the up/down direction. + Rows are preserved, but appear in a different order than before. + + Parameters + ---------- + m : array_like + Input array. + + Returns + ------- + out : array_like + A view of `m` with the rows reversed. Since a view is + returned, this operation is :math:`\mathcal O(1)`. + """ + if isinstance(m, numeric_types): + return _np.flip(m, 0) + elif isinstance(m, _Symbol): + return _npi.flip(m,0) + else: + raise TypeError('type {} not supported'.format(str(type(m)))) + + +@set_module('mxnet.symbol.numpy') +def fliplr(m): + r""" + fliplr(*args, **kwargs) + + Flip array in the left/right direction. + + Flip the entries in each row in the left/right direction. + Columns are preserved, but appear in a different order than before. + + Parameters + ---------- + m : array_like + Input array, must be at least 2-D. + + Returns + ------- + f : ndarray + A view of `m` with the columns reversed. Since a view + is returned, this operation is :math:`\mathcal O(1)`. + """ + if isinstance(m, numeric_types): + return _np.flip(m, 1) + elif isinstance(m, _Symbol): + return _npi.flip(m,1) + else: + raise TypeError('type {} not supported'.format(str(type(m)))) + + @set_module('mxnet.symbol.numpy') def around(x, decimals=0, out=None, **kwargs): r""" diff --git a/tests/python/unittest/test_numpy_interoperability.py b/tests/python/unittest/test_numpy_interoperability.py index d2064789c761..d940422408f4 100644 --- a/tests/python/unittest/test_numpy_interoperability.py +++ b/tests/python/unittest/test_numpy_interoperability.py @@ -787,6 +787,18 @@ def _add_workload_flip(): OpArgMngr.add_workload('flip', np.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]]), (1, 2)) +def _add_workload_flipud(): + OpArgMngr.add_workload('flipud', np.random.normal(size=(4, 4))) + OpArgMngr.add_workload('flipud', np.array([[0, 1, 2], [3, 4, 5]])) + OpArgMngr.add_workload('flipud', np.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]])) + + +def _add_workload_fliplr(): + OpArgMngr.add_workload('fliplr', np.random.normal(size=(4, 4))) + OpArgMngr.add_workload('fliplr', np.array([[0, 1, 2], [3, 4, 5]])) + OpArgMngr.add_workload('fliplr', np.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]])) + + def _add_workload_max(array_pool): OpArgMngr.add_workload('max', array_pool['4x1']) @@ -1606,6 +1618,8 @@ def _prepare_workloads(): _add_workload_expand_dims() _add_workload_fix() _add_workload_flip() + _add_workload_flipud() + _add_workload_fliplr() _add_workload_max(array_pool) _add_workload_amax(array_pool) _add_workload_min(array_pool) diff --git a/tests/python/unittest/test_numpy_op.py b/tests/python/unittest/test_numpy_op.py index 7e8c34456723..fe32352b90be 100644 --- a/tests/python/unittest/test_numpy_op.py +++ b/tests/python/unittest/test_numpy_op.py @@ -4710,6 +4710,60 @@ def hybrid_forward(self, F, x): assert_almost_equal(mx_out.asnumpy(), np_out, rtol=rtol, atol=atol) +@with_seed() +@use_np +def test_np_flipud_fliplr(): + class TestFlipud(HybridBlock): + def __init__(self): + super(TestFlipud, self).__init__() + + def hybrid_forward(self, F, x): + return F.np.flipud(x) + + class TestFliplr(HybridBlock): + def __init__(self): + super(TestFliplr, self).__init__() + + def hybrid_forward(self, F, x): + return F.np.fliplr(x) + + shapes = [(1, 2, 3), (1, 0)] + types = ['int32', 'int64', 'float16', 'float32', 'float64'] + for func in ['flipud', 'fliplr']: + for hybridize in [True, False]: + for oneType in types: + rtol, atol=1e-3, 1e-5 + for shape in shapes: + if func == 'flipud': + test_flip = TestFlipud() + else: + test_flip = TestFliplr() + if hybridize: + test_flip.hybridize() + x = rand_ndarray(shape, dtype=oneType).as_np_ndarray() + x.attach_grad() + if func == 'flipud': + np_out = _np.flipud(x.asnumpy()) + else: + np_out = _np.fliplr(x.asnumpy()) + with mx.autograd.record(): + mx_out = test_flip(x) + assert mx_out.shape == np_out.shape + assert_almost_equal(mx_out.asnumpy(), np_out, rtol=rtol, atol=atol) + mx_out.backward() + np_backward = _np.ones(np_out.shape) + assert_almost_equal(x.grad.asnumpy(), np_backward, rtol=rtol, atol=atol) + + # Test imperative once again + if func == 'flipud': + mx_out = np.flipud(x) + np_out = _np.flipud(x.asnumpy()) + else: + mx_out = np.fliplr(x) + np_out = _np.fliplr(x.asnumpy()) + assert_almost_equal(mx_out.asnumpy(), np_out, rtol=rtol, atol=atol) + + @with_seed() @use_np def test_np_around(): From d9e0750dab648e3db27eb3ba8cb69ba6efd7154f Mon Sep 17 00:00:00 2001 From: Yiyan66 Date: Mon, 30 Dec 2019 08:58:46 +0000 Subject: [PATCH 2/6] change something --- python/mxnet/numpy/multiarray.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/mxnet/numpy/multiarray.py b/python/mxnet/numpy/multiarray.py index 68c0b8172ef3..af5f83685514 100644 --- a/python/mxnet/numpy/multiarray.py +++ b/python/mxnet/numpy/multiarray.py @@ -1478,7 +1478,7 @@ def flipud(self, *args, **kwargs): """ raise AttributeError('mxnet.numpy.ndarray object has no attribute flipud') - def flip(self, *args, **kwargs): + def fliplr(self, *args, **kwargs): """Convenience fluent method for :py:func:`fliplr`. The arguments are the same as for :py:func:`fliplr`, with From 8dcb3dda52533e2d0f4c436c75f4a42bd72fc2d3 Mon Sep 17 00:00:00 2001 From: Yiyan66 Date: Mon, 30 Dec 2019 09:35:53 +0000 Subject: [PATCH 3/6] change py --- python/mxnet/ndarray/numpy/_op.py | 2 ++ python/mxnet/numpy/multiarray.py | 18 ++---------------- python/mxnet/symbol/numpy/_symbol.py | 22 ++++------------------ 3 files changed, 8 insertions(+), 34 deletions(-) diff --git a/python/mxnet/ndarray/numpy/_op.py b/python/mxnet/ndarray/numpy/_op.py index 2a6f93694a7b..47ccb1c295a5 100644 --- a/python/mxnet/ndarray/numpy/_op.py +++ b/python/mxnet/ndarray/numpy/_op.py @@ -4920,6 +4920,8 @@ def fliplr(m): array(True) """ from ...numpy import ndarray + if len(_np.shape(m.asnumpy())) < 2: + raise ValueError("Input must be >= 2-d.") if isinstance(m, numeric_types): return _np.flip(m, 1) elif isinstance(m, ndarray): diff --git a/python/mxnet/numpy/multiarray.py b/python/mxnet/numpy/multiarray.py index af5f83685514..5ee08bd450ba 100644 --- a/python/mxnet/numpy/multiarray.py +++ b/python/mxnet/numpy/multiarray.py @@ -1470,22 +1470,6 @@ def flip(self, *args, **kwargs): """ raise AttributeError('mxnet.numpy.ndarray object has no attribute flip') - def flipud(self, *args, **kwargs): - """Convenience fluent method for :py:func:`flipud`. - - The arguments are the same as for :py:func:`flipud`, with - this array as data. - """ - raise AttributeError('mxnet.numpy.ndarray object has no attribute flipud') - - def fliplr(self, *args, **kwargs): - """Convenience fluent method for :py:func:`fliplr`. - - The arguments are the same as for :py:func:`fliplr`, with - this array as data. - """ - raise AttributeError('mxnet.numpy.ndarray object has no attribute fliplr') - def depth_to_space(self, *args, **kwargs): """Convenience fluent method for :py:func:`depth_to_space`. @@ -6628,6 +6612,8 @@ def fliplr(m): >>> np.all(np.fliplr(A) == A[:,::-1,...]) array(True) """ + if len(_np.shape(m.asnumpy())) < 2: + raise ValueError("Input must be >= 2-d.") return _mx_nd_np.flip(m, 1) diff --git a/python/mxnet/symbol/numpy/_symbol.py b/python/mxnet/symbol/numpy/_symbol.py index 7b3937f0c830..6bb99e3ec287 100644 --- a/python/mxnet/symbol/numpy/_symbol.py +++ b/python/mxnet/symbol/numpy/_symbol.py @@ -585,22 +585,6 @@ def flip(self, *args, **kwargs): """ raise AttributeError('_Symbol object has no attribute flip') - def flipud(self, *args, **kwargs): - """Convenience fluent method for :py:func:`flipud`. - - The arguments are the same as for :py:func:`flipud`, with - this array as data. - """ - raise AttributeError('_Symbol object has no attribute flipud') - - def fliplr(self, *args, **kwargs): - """Convenience fluent method for :py:func:`fliplr`. - - The arguments are the same as for :py:func:`fliplr`, with - this array as data. - """ - raise AttributeError('_Symbol object has no attribute fliplr') - def depth_to_space(self, *args, **kwargs): """Convenience fluent method for :py:func:`depth_to_space`. @@ -4621,7 +4605,7 @@ def flipud(m): if isinstance(m, numeric_types): return _np.flip(m, 0) elif isinstance(m, _Symbol): - return _npi.flip(m,0) + return _npi.flip(m, 0) else: raise TypeError('type {} not supported'.format(str(type(m)))) @@ -4647,10 +4631,12 @@ def fliplr(m): A view of `m` with the columns reversed. Since a view is returned, this operation is :math:`\mathcal O(1)`. """ + if len(_np.shape(m.asnumpy())) < 2: + raise ValueError("Input must be >= 2-d.") if isinstance(m, numeric_types): return _np.flip(m, 1) elif isinstance(m, _Symbol): - return _npi.flip(m,1) + return _npi.flip(m, 1) else: raise TypeError('type {} not supported'.format(str(type(m)))) From 0215d9641cd2051b1a12d3763c3818079b45f471 Mon Sep 17 00:00:00 2001 From: Yiyan66 Date: Mon, 30 Dec 2019 11:44:15 +0000 Subject: [PATCH 4/6] raise error --- python/mxnet/ndarray/numpy/_op.py | 2 -- python/mxnet/numpy/multiarray.py | 2 -- python/mxnet/symbol/numpy/_symbol.py | 2 -- src/operator/numpy/np_matrix_op-inl.h | 2 +- 4 files changed, 1 insertion(+), 7 deletions(-) diff --git a/python/mxnet/ndarray/numpy/_op.py b/python/mxnet/ndarray/numpy/_op.py index 47ccb1c295a5..2a6f93694a7b 100644 --- a/python/mxnet/ndarray/numpy/_op.py +++ b/python/mxnet/ndarray/numpy/_op.py @@ -4920,8 +4920,6 @@ def fliplr(m): array(True) """ from ...numpy import ndarray - if len(_np.shape(m.asnumpy())) < 2: - raise ValueError("Input must be >= 2-d.") if isinstance(m, numeric_types): return _np.flip(m, 1) elif isinstance(m, ndarray): diff --git a/python/mxnet/numpy/multiarray.py b/python/mxnet/numpy/multiarray.py index 5ee08bd450ba..edb61bdfb752 100644 --- a/python/mxnet/numpy/multiarray.py +++ b/python/mxnet/numpy/multiarray.py @@ -6612,8 +6612,6 @@ def fliplr(m): >>> np.all(np.fliplr(A) == A[:,::-1,...]) array(True) """ - if len(_np.shape(m.asnumpy())) < 2: - raise ValueError("Input must be >= 2-d.") return _mx_nd_np.flip(m, 1) diff --git a/python/mxnet/symbol/numpy/_symbol.py b/python/mxnet/symbol/numpy/_symbol.py index 6bb99e3ec287..21ed2f25fc1a 100644 --- a/python/mxnet/symbol/numpy/_symbol.py +++ b/python/mxnet/symbol/numpy/_symbol.py @@ -4631,8 +4631,6 @@ def fliplr(m): A view of `m` with the columns reversed. Since a view is returned, this operation is :math:`\mathcal O(1)`. """ - if len(_np.shape(m.asnumpy())) < 2: - raise ValueError("Input must be >= 2-d.") if isinstance(m, numeric_types): return _np.flip(m, 1) elif isinstance(m, _Symbol): diff --git a/src/operator/numpy/np_matrix_op-inl.h b/src/operator/numpy/np_matrix_op-inl.h index 3abf5627f20f..ccc3dd2a0876 100644 --- a/src/operator/numpy/np_matrix_op-inl.h +++ b/src/operator/numpy/np_matrix_op-inl.h @@ -498,7 +498,7 @@ void NumpyFlipForward(const nnvm::NodeAttrs& attrs, std::vector trailing_(axistemp.ndim()); index_t flip_index = 0; for (int axis : axistemp) { - CHECK_LT(axis, ishape.ndim()); + CHECK_LT(axis, ishape.ndim()) << "Input must be >= 2-d"; stride_[flip_index] = ishape[axis]; trailing_[flip_index] = 1; for (int i2 = axis + 1; i2 < ishape.ndim(); ++i2) { From a3ad2464720cc381834477e8f9930a52877da480 Mon Sep 17 00:00:00 2001 From: Yiyan66 Date: Tue, 31 Dec 2019 04:05:13 +0000 Subject: [PATCH 5/6] change error --- python/mxnet/ndarray/numpy/_op.py | 32 +++++++++------------------ python/mxnet/numpy/multiarray.py | 12 +++++----- python/mxnet/symbol/numpy/_symbol.py | 14 ++---------- src/operator/numpy/np_matrix_op-inl.h | 2 +- 4 files changed, 19 insertions(+), 41 deletions(-) diff --git a/python/mxnet/ndarray/numpy/_op.py b/python/mxnet/ndarray/numpy/_op.py index 2a6f93694a7b..e226c30ff20b 100644 --- a/python/mxnet/ndarray/numpy/_op.py +++ b/python/mxnet/ndarray/numpy/_op.py @@ -4850,12 +4850,12 @@ def flipud(m): >>> A = np.diag(np.array([1.0, 2, 3])) >>> A array([[1., 0., 0.], - [0., 2., 0.], - [0., 0., 3.]]) + [0., 2., 0.], + [0., 0., 3.]]) >>> np.flipud(A) array([[0., 0., 3.], - [0., 2., 0.], - [1., 0., 0.]]) + [0., 2., 0.], + [1., 0., 0.]]) >>> A = np.random.randn(2,3,5) >>> np.all(np.flipud(A) == A[::-1,...]) @@ -4864,13 +4864,7 @@ def flipud(m): >>> np.flipud(np.array([1,2])) array([2., 1.]) """ - from ...numpy import ndarray - if isinstance(m, numeric_types): - return _np.flip(m, 0) - elif isinstance(m, ndarray): - return _npi.flip(m, 0) - else: - raise TypeError('type {} not supported'.format(str(type(m)))) + return flip(m, 0) @set_module('mxnet.ndarray.numpy') @@ -4908,24 +4902,18 @@ def fliplr(m): >>> A = np.diag(np.array([1.,2.,3.])) >>> A array([[1., 0., 0.], - [0., 2., 0.], - [0., 0., 3.]]) + [0., 2., 0.], + [0., 0., 3.]]) >>> np.fliplr(A) array([[0., 0., 1.], - [0., 2., 0.], - [3., 0., 0.]]) + [0., 2., 0.], + [3., 0., 0.]]) >>> A = np.random.randn(2,3,5) >>> np.all(np.fliplr(A) == A[:,::-1,...]) array(True) """ - from ...numpy import ndarray - if isinstance(m, numeric_types): - return _np.flip(m, 1) - elif isinstance(m, ndarray): - return _npi.flip(m, 1) - else: - raise TypeError('type {} not supported'.format(str(type(m)))) + return flip(m, 1) @set_module('mxnet.ndarray.numpy') diff --git a/python/mxnet/numpy/multiarray.py b/python/mxnet/numpy/multiarray.py index edb61bdfb752..847f106575a8 100644 --- a/python/mxnet/numpy/multiarray.py +++ b/python/mxnet/numpy/multiarray.py @@ -6549,12 +6549,12 @@ def flipud(m): >>> A = np.diag(np.array([1.0, 2, 3])) >>> A array([[1., 0., 0.], - [0., 2., 0.], - [0., 0., 3.]]) + [0., 2., 0.], + [0., 0., 3.]]) >>> np.flipud(A) array([[0., 0., 3.], - [0., 2., 0.], - [1., 0., 0.]]) + [0., 2., 0.], + [1., 0., 0.]]) >>> A = np.random.randn(2,3,5) >>> np.all(np.flipud(A) == A[::-1,...]) @@ -6563,7 +6563,7 @@ def flipud(m): >>> np.flipud(np.array([1,2])) array([2., 1.]) """ - return _mx_nd_np.flip(m, 0) + return flip(m, 0) @set_module('mxnet.numpy') @@ -6612,7 +6612,7 @@ def fliplr(m): >>> np.all(np.fliplr(A) == A[:,::-1,...]) array(True) """ - return _mx_nd_np.flip(m, 1) + return flip(m, 1) @set_module('mxnet.numpy') diff --git a/python/mxnet/symbol/numpy/_symbol.py b/python/mxnet/symbol/numpy/_symbol.py index 21ed2f25fc1a..d3ed84ae0e49 100644 --- a/python/mxnet/symbol/numpy/_symbol.py +++ b/python/mxnet/symbol/numpy/_symbol.py @@ -4602,12 +4602,7 @@ def flipud(m): A view of `m` with the rows reversed. Since a view is returned, this operation is :math:`\mathcal O(1)`. """ - if isinstance(m, numeric_types): - return _np.flip(m, 0) - elif isinstance(m, _Symbol): - return _npi.flip(m, 0) - else: - raise TypeError('type {} not supported'.format(str(type(m)))) + return flip(m, 0) @set_module('mxnet.symbol.numpy') @@ -4631,12 +4626,7 @@ def fliplr(m): A view of `m` with the columns reversed. Since a view is returned, this operation is :math:`\mathcal O(1)`. """ - if isinstance(m, numeric_types): - return _np.flip(m, 1) - elif isinstance(m, _Symbol): - return _npi.flip(m, 1) - else: - raise TypeError('type {} not supported'.format(str(type(m)))) + return flip(m, 1) @set_module('mxnet.symbol.numpy') diff --git a/src/operator/numpy/np_matrix_op-inl.h b/src/operator/numpy/np_matrix_op-inl.h index ccc3dd2a0876..b81f2ffee481 100644 --- a/src/operator/numpy/np_matrix_op-inl.h +++ b/src/operator/numpy/np_matrix_op-inl.h @@ -498,7 +498,7 @@ void NumpyFlipForward(const nnvm::NodeAttrs& attrs, std::vector trailing_(axistemp.ndim()); index_t flip_index = 0; for (int axis : axistemp) { - CHECK_LT(axis, ishape.ndim()) << "Input must be >= 2-d"; + CHECK_LT(axis, ishape.ndim()) << "Input must be >= " << axis + 1 << "-d"; stride_[flip_index] = ishape[axis]; trailing_[flip_index] = 1; for (int i2 = axis + 1; i2 < ishape.ndim(); ++i2) { From 0eb9f682b1fe87e40b905c246f85fffd814da3ba Mon Sep 17 00:00:00 2001 From: Yiyan66 Date: Tue, 31 Dec 2019 04:20:33 +0000 Subject: [PATCH 6/6] change error info --- src/operator/numpy/np_matrix_op-inl.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/operator/numpy/np_matrix_op-inl.h b/src/operator/numpy/np_matrix_op-inl.h index b81f2ffee481..2007a49ca662 100644 --- a/src/operator/numpy/np_matrix_op-inl.h +++ b/src/operator/numpy/np_matrix_op-inl.h @@ -498,7 +498,8 @@ void NumpyFlipForward(const nnvm::NodeAttrs& attrs, std::vector trailing_(axistemp.ndim()); index_t flip_index = 0; for (int axis : axistemp) { - CHECK_LT(axis, ishape.ndim()) << "Input must be >= " << axis + 1 << "-d"; + CHECK_LT(axis, ishape.ndim()) << "axis " << axis + << " is out of bounds for array of dimension " << ishape.ndim() << std::endl; stride_[flip_index] = ishape[axis]; trailing_[flip_index] = 1; for (int i2 = axis + 1; i2 < ishape.ndim(); ++i2) {