From 90a18e0d272a73cc215ada0b356d72dff7d38d9c Mon Sep 17 00:00:00 2001 From: kshitij12345 Date: Sun, 28 Jul 2019 15:04:06 +0530 Subject: [PATCH 1/6] add magic method abs to ndarray --- python/mxnet/ndarray/ndarray.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/python/mxnet/ndarray/ndarray.py b/python/mxnet/ndarray/ndarray.py index 53c563854511..9ee1ad2cd09f 100644 --- a/python/mxnet/ndarray/ndarray.py +++ b/python/mxnet/ndarray/ndarray.py @@ -205,6 +205,10 @@ def _to_shared_mem(self): self.handle, ctypes.byref(shared_pid), ctypes.byref(shared_id))) return shared_pid.value, shared_id.value, self.shape, self.dtype + def __abs__(self): + """x.__abs__() <=> abs(x) <=> x.abs() <=> mx.nd.abs(x, y)""" + return self.abs() + def __add__(self, other): """x.__add__(y) <=> x+y <=> mx.nd.add(x, y) """ return add(self, other) From 2552054b67f5a4e2b2e2d1bcba6c3bb40b8a93c2 Mon Sep 17 00:00:00 2001 From: kshitij12345 Date: Sun, 28 Jul 2019 15:09:11 +0530 Subject: [PATCH 2/6] add relevant tests --- tests/python/unittest/test_ndarray.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/python/unittest/test_ndarray.py b/tests/python/unittest/test_ndarray.py index 56db1ebd640d..0f154bd67a1a 100644 --- a/tests/python/unittest/test_ndarray.py +++ b/tests/python/unittest/test_ndarray.py @@ -172,6 +172,15 @@ def test_ndarray_negate(): assert_almost_equal(npy, arr.asnumpy()) +@with_seed() +def test_ndarray_magic_abs(): + for dim in range(1, 7): + shape = rand_shape_nd(dim) + npy = np.random.uniform(-10, 10, shape) + arr = mx.nd.array(npy) + assert_almost_equal(abs(arr).asnumpy(), arr.abs().asnumpy()) + + @with_seed() def test_ndarray_reshape(): tensor = (mx.nd.arange(30) + 1).reshape(2, 3, 5) From d962df3e652afc24c132633a12fc56e3d566bd5b Mon Sep 17 00:00:00 2001 From: kshitij12345 Date: Mon, 29 Jul 2019 21:10:48 +0530 Subject: [PATCH 3/6] add magic method abs to symbol --- python/mxnet/symbol/symbol.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/python/mxnet/symbol/symbol.py b/python/mxnet/symbol/symbol.py index deedf0fe83d2..40bf4ef10be7 100644 --- a/python/mxnet/symbol/symbol.py +++ b/python/mxnet/symbol/symbol.py @@ -93,6 +93,10 @@ def __iter__(self): """ return (self[i] for i in range(len(self))) + def __abs__(self): + """x.__abs__() <=> abs(x) <=> x.abs() <=> mx.symbol.abs(x, y)""" + return self.abs() + def __add__(self, other): """x.__add__(y) <=> x+y From 6e64ee95b89cae255d3c80041f42821b7dbbfe56 Mon Sep 17 00:00:00 2001 From: kshitij12345 Date: Mon, 29 Jul 2019 21:11:03 +0530 Subject: [PATCH 4/6] add relevant tests --- tests/python/unittest/test_symbol.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/python/unittest/test_symbol.py b/tests/python/unittest/test_symbol.py index 2dfe3e44eedb..fc0d300e02c9 100644 --- a/tests/python/unittest/test_symbol.py +++ b/tests/python/unittest/test_symbol.py @@ -22,7 +22,7 @@ import numpy as np from common import assertRaises, models from mxnet.base import NotImplementedForSymbol -from mxnet.test_utils import discard_stderr +from mxnet.test_utils import discard_stderr, rand_shape_nd import pickle as pkl def test_symbol_basic(): @@ -188,6 +188,21 @@ def test_symbol_infer_shape_var(): assert arg_shapes[1] == overwrite_shape assert out_shapes[0] == overwrite_shape + +def test_symbol_magic_abs(): + for dim in range(1, 7): + with mx.name.NameManager(): + data = mx.symbol.Variable('data') + method = data.abs(name='abs0') + magic = abs(data) + regular = mx.symbol.abs(data, name='abs0') + ctx = {'ctx': mx.context.current_context(), 'data': rand_shape_nd(dim)} + mx.test_utils.check_consistency( + [method, magic], ctx_list=[ctx, ctx]) + mx.test_utils.check_consistency( + [regular, magic], ctx_list=[ctx, ctx]) + + def test_symbol_fluent(): has_grad = set(['flatten', 'expand_dims', 'flip', 'tile', 'transpose', 'sum', 'nansum', 'prod', 'nanprod', 'mean', 'max', 'min', 'reshape', 'broadcast_to', 'split', From c981d7e78e30c6cc40d128156922cd8c60e5c7de Mon Sep 17 00:00:00 2001 From: JackieWu Date: Wed, 31 Jul 2019 08:59:10 +0800 Subject: [PATCH 5/6] retrigger CI From 06df5335061bbfbddebe79bc7b621c32d938720a Mon Sep 17 00:00:00 2001 From: JackieWu Date: Thu, 1 Aug 2019 07:06:53 +0800 Subject: [PATCH 6/6] retrigger CI