From ca552b342733f7b42f3b27215d92ac62e2683649 Mon Sep 17 00:00:00 2001 From: stu1130 Date: Sat, 15 Jun 2019 17:13:45 -0700 Subject: [PATCH 01/11] draft --- python/mxnet/ndarray/numpy/_op.py | 52 ++++++++++++++++++++++++++++++- python/mxnet/numpy/multiarray.py | 47 +++++++++++++++++++++++++++- src/operator/tensor/init_op.cc | 1 + 3 files changed, 98 insertions(+), 2 deletions(-) diff --git a/python/mxnet/ndarray/numpy/_op.py b/python/mxnet/ndarray/numpy/_op.py index 04de2cd9a5d2..edd6dfac057a 100644 --- a/python/mxnet/ndarray/numpy/_op.py +++ b/python/mxnet/ndarray/numpy/_op.py @@ -26,7 +26,7 @@ __all__ = ['zeros', 'ones', 'maximum', 'minimum', 'stack', 'arange', 'argmax', 'add', 'subtract', 'multiply', 'divide', 'mod', 'power', 'concatenate', - 'clip', 'split', 'swapaxes', 'expand_dims', 'tile'] + 'clip', 'split', 'swapaxes', 'expand_dims', 'tile', 'linspace'] @set_module('mxnet.ndarray.numpy') @@ -629,3 +629,53 @@ def tile(A, reps): The tiled output array. """ return _npi.tile(A, reps) + + +def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0): + """Return evenly spaced numbers over a specified interval. + + Returns num evenly spaced samples, calculated over the interval [start, stop]. + The endpoint of the interval can optionally be excluded. + + Parameters + ---------- + start : array_like + The starting value of the sequence. + stop : array_like + The end value of the sequence, unless endpoint is set to False. In + that case, the sequence consists of all but the last of num + 1 + evenly spaced samples, so that stop is excluded. Note that the step + size changes when endpoint is False. + num : int, optional + Number of samples to generate. Default is 50. Must be non-negative. + endpoint : bool, optional + If True, stop is the last sample. Otherwise, it is not included. + Default is True. + retstep: bool, optional + If True, return (samples, step), where step is the spacing between samples. + dtype: dtype, optional + The type of the output array. If dtype is not given, infer the data + type from the other input arguments. + axis : int, optional + The axis in the result to store the samples. Relevant only if start or + stop are array-like. By default (0), the samples will be along a new + axis inserted at the beginning. Use -1 to get an axis at the end. + Returns + ------- + samples : ndarray + There are num equally spaced samples in the closed interval + `[start, stop]` or the half-open interval `[start, stop)` + (depending on whether endpoint is True or False). + step : float, optional + Only returned if retstep is True + Size of spacing between samples. + + Notes + ----- + This function currently does not support ``start`` and ``stop`` as ndarrays and + axis could only be 0 now. + """ + if isinstance(start, (list, _np.ndarray, mx.nd.NDArray)) or \ + isinstance(stop, (list, _np.ndarray, mx.nd.NDArray)): + raise NotImplementedError('start and stop only support int') + return _npi.linspace(start=start, stop=stop, num=num, endpoint=endpoint, dtype=dtype) diff --git a/python/mxnet/numpy/multiarray.py b/python/mxnet/numpy/multiarray.py index 3c981d1ba63c..3e3c0d34613f 100644 --- a/python/mxnet/numpy/multiarray.py +++ b/python/mxnet/numpy/multiarray.py @@ -45,7 +45,7 @@ __all__ = ['ndarray', 'empty', 'array', 'zeros', 'ones', 'maximum', 'minimum', 'stack', 'arange', 'argmax', 'add', 'subtract', 'multiply', 'divide', 'mod', 'power', 'concatenate', - 'clip', 'split', 'swapaxes', 'expand_dims', 'tile'] + 'clip', 'split', 'swapaxes', 'expand_dims', 'tile', 'linspace'] # This function is copied from ndarray.py since pylint @@ -1444,6 +1444,9 @@ def arange(start, stop=None, step=1, dtype=None, ctx=None): return _mx_nd_np.arange(start, stop, step, dtype, ctx) + + + @set_module('mxnet.numpy') def argmax(a, axis=None, out=None): """Returns the indices of the maximum values along an axis. @@ -1791,3 +1794,45 @@ def tile(A, reps): The tiled output array. """ return _npi.tile(A, reps) + + +def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0): + """Return evenly spaced numbers over a specified interval. + + Returns num evenly spaced samples, calculated over the interval [start, stop]. + The endpoint of the interval can optionally be excluded. + + Parameters + ---------- + start : array_like + The starting value of the sequence. + stop : array_like + The end value of the sequence, unless endpoint is set to False. In + that case, the sequence consists of all but the last of num + 1 + evenly spaced samples, so that stop is excluded. Note that the step + size changes when endpoint is False. + num : int, optional + Number of samples to generate. Default is 50. Must be non-negative. + endpoint : bool, optional + If True, stop is the last sample. Otherwise, it is not included. + Default is True. + retstep: bool, optional + If True, return (samples, step), where step is the spacing between samples. + dtype: dtype, optional + The type of the output array. If dtype is not given, infer the data + type from the other input arguments. + axis : int, optional + The axis in the result to store the samples. Relevant only if start or + stop are array-like. By default (0), the samples will be along a new + axis inserted at the beginning. Use -1 to get an axis at the end. + Returns + ------- + samples : ndarray + There are num equally spaced samples in the closed interval + `[start, stop]` or the half-open interval `[start, stop)` + (depending on whether endpoint is True or False). + step : float, optional + Only returned if retstep is True + Size of spacing between samples. + """ + return _mx_nd_np.linspace(start, stop, num, endpoint, retstep, dtype, axis) diff --git a/src/operator/tensor/init_op.cc b/src/operator/tensor/init_op.cc index a58498b85aa5..560c41336a48 100644 --- a/src/operator/tensor/init_op.cc +++ b/src/operator/tensor/init_op.cc @@ -101,6 +101,7 @@ NNVM_REGISTER_OP(_arange) .add_arguments(RangeParam::__FIELDS__()); NNVM_REGISTER_OP(_linspace) +.add_alias("_npi_linspace") .describe("Return evenly spaced numbers over a specified interval. Similar to Numpy") .set_num_inputs(0) .set_num_outputs(1) From eabf19461d5b2f628fd25061d4f52f7e89af7179 Mon Sep 17 00:00:00 2001 From: stu1130 Date: Sat, 15 Jun 2019 23:35:28 -0700 Subject: [PATCH 02/11] finish linspace implementation --- python/mxnet/ndarray/numpy/_op.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/python/mxnet/ndarray/numpy/_op.py b/python/mxnet/ndarray/numpy/_op.py index edd6dfac057a..2a099a83109e 100644 --- a/python/mxnet/ndarray/numpy/_op.py +++ b/python/mxnet/ndarray/numpy/_op.py @@ -23,6 +23,7 @@ from ...util import _sanity_check_params, set_module from ...context import current_context from . import _internal as _npi +from ..ndarray import NDArray __all__ = ['zeros', 'ones', 'maximum', 'minimum', 'stack', 'arange', 'argmax', 'add', 'subtract', 'multiply', 'divide', 'mod', 'power', 'concatenate', @@ -675,7 +676,13 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis This function currently does not support ``start`` and ``stop`` as ndarrays and axis could only be 0 now. """ - if isinstance(start, (list, _np.ndarray, mx.nd.NDArray)) or \ - isinstance(stop, (list, _np.ndarray, mx.nd.NDArray)): + if isinstance(start, (list, _np.ndarray, NDArray)) or \ + isinstance(stop, (list, _np.ndarray, NDArray)): raise NotImplementedError('start and stop only support int') - return _npi.linspace(start=start, stop=stop, num=num, endpoint=endpoint, dtype=dtype) + if axis != 0: + raise NotImplementedError("the function only support axis 0") + if retstep: + step = (stop - start) / (num - 1) + return (_npi.linspace(start=start, stop=stop, num=num, endpoint=endpoint, dtype=dtype), step) + else: + return _npi.linspace(start=start, stop=stop, num=num, endpoint=endpoint, dtype=dtype) From 0f2e81a3bf2bdd406d36810ccc8ad3bb6ad6541e Mon Sep 17 00:00:00 2001 From: stu1130 Date: Sun, 16 Jun 2019 15:40:55 -0700 Subject: [PATCH 03/11] finish linspace --- python/mxnet/ndarray/numpy/_op.py | 4 +- python/mxnet/numpy/multiarray.py | 4 +- python/mxnet/symbol/numpy/_symbol.py | 60 +++++++++++++++++++++++++- tests/python/unittest/test_numpy_op.py | 57 ++++++++++++++++++++++++ 4 files changed, 121 insertions(+), 4 deletions(-) diff --git a/python/mxnet/ndarray/numpy/_op.py b/python/mxnet/ndarray/numpy/_op.py index 2a099a83109e..d68f41e79134 100644 --- a/python/mxnet/ndarray/numpy/_op.py +++ b/python/mxnet/ndarray/numpy/_op.py @@ -632,7 +632,7 @@ def tile(A, reps): return _npi.tile(A, reps) -def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0): +def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0, ctx=None): """Return evenly spaced numbers over a specified interval. Returns num evenly spaced samples, calculated over the interval [start, stop]. @@ -681,6 +681,8 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis raise NotImplementedError('start and stop only support int') if axis != 0: raise NotImplementedError("the function only support axis 0") + if not ctx: + ctx = current_context() if retstep: step = (stop - start) / (num - 1) return (_npi.linspace(start=start, stop=stop, num=num, endpoint=endpoint, dtype=dtype), step) diff --git a/python/mxnet/numpy/multiarray.py b/python/mxnet/numpy/multiarray.py index 3e3c0d34613f..eec46be3b5d5 100644 --- a/python/mxnet/numpy/multiarray.py +++ b/python/mxnet/numpy/multiarray.py @@ -1796,7 +1796,7 @@ def tile(A, reps): return _npi.tile(A, reps) -def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0): +def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0, ctx=None): """Return evenly spaced numbers over a specified interval. Returns num evenly spaced samples, calculated over the interval [start, stop]. @@ -1835,4 +1835,4 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis Only returned if retstep is True Size of spacing between samples. """ - return _mx_nd_np.linspace(start, stop, num, endpoint, retstep, dtype, axis) + return _mx_nd_np.linspace(start, stop, num, endpoint, retstep, dtype, axis, ctx) diff --git a/python/mxnet/symbol/numpy/_symbol.py b/python/mxnet/symbol/numpy/_symbol.py index 11a1da81a855..10f3905cc2b9 100644 --- a/python/mxnet/symbol/numpy/_symbol.py +++ b/python/mxnet/symbol/numpy/_symbol.py @@ -31,7 +31,7 @@ __all__ = ['zeros', 'ones', 'maximum', 'minimum', 'stack', 'concatenate', 'arange', 'argmax', 'clip', 'add', 'subtract', 'multiply', 'divide', 'mod', 'power', 'split', 'swapaxes', - 'expand_dims', 'tile'] + 'expand_dims', 'linspace'] def _num_outputs(sym): @@ -1307,4 +1307,62 @@ def tile(A, reps): return _npi.tile(A, reps) +def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0, ctx=None): + """Return evenly spaced numbers over a specified interval. + + Returns num evenly spaced samples, calculated over the interval [start, stop]. + The endpoint of the interval can optionally be excluded. + + Parameters + ---------- + start : array_like + The starting value of the sequence. + stop : array_like + The end value of the sequence, unless endpoint is set to False. In + that case, the sequence consists of all but the last of num + 1 + evenly spaced samples, so that stop is excluded. Note that the step + size changes when endpoint is False. + num : int, optional + Number of samples to generate. Default is 50. Must be non-negative. + endpoint : bool, optional + If True, stop is the last sample. Otherwise, it is not included. + Default is True. + retstep: bool, optional + If True, return (samples, step), where step is the spacing between samples. + dtype: dtype, optional + The type of the output array. If dtype is not given, infer the data + type from the other input arguments. + axis : int, optional + The axis in the result to store the samples. Relevant only if start or + stop are array-like. By default (0), the samples will be along a new + axis inserted at the beginning. Use -1 to get an axis at the end. + Returns + ------- + samples : ndarray + There are num equally spaced samples in the closed interval + `[start, stop]` or the half-open interval `[start, stop)` + (depending on whether endpoint is True or False). + step : float, optional + Only returned if retstep is True + Size of spacing between samples. + + Notes + ----- + This function currently does not support ``start`` and ``stop`` as ndarrays and + axis could only be 0 now. + """ + if isinstance(start, (list, _np.ndarray)) or \ + isinstance(stop, (list, _np.ndarray)): + raise NotImplementedError('start and stop only support int') + if axis != 0: + raise NotImplementedError("the function only support axis 0") + if not ctx: + ctx = current_context() + if retstep: + step = (stop - start) / (num - 1) + return (_npi.linspace(start=start, stop=stop, num=num, endpoint=endpoint, dtype=dtype), step) + else: + return _npi.linspace(start=start, stop=stop, num=num, endpoint=endpoint, dtype=dtype) + + _set_np_symbol_class(_Symbol) diff --git a/tests/python/unittest/test_numpy_op.py b/tests/python/unittest/test_numpy_op.py index 862c4d434045..7dfe6268e503 100644 --- a/tests/python/unittest/test_numpy_op.py +++ b/tests/python/unittest/test_numpy_op.py @@ -554,6 +554,63 @@ def hybrid_forward(self, F, x): assert same(mx_out.asnumpy(), np_out) +@with_seed() +@npx.use_np_shape +def test_np_linspace(): + configs = [ + (0.0, 1.0, 10) + ] + dtypes = ['int32', 'float16', 'float32', 'float64', None] + for config in configs: + for dtype in dtypes: + for endpoint in [False, True]: + for retstep in [False, True]: + if isinstance(config, tuple): + mx_ret = np.linspace(*config, endpoint=endpoint, retstep=retstep, dtype=dtype) + np_ret = _np.linspace(*config, endpoint=endpoint, retstep=retstep, dtype=dtype) + else: + mx_ret = np.linspace(config, endpoint=endpoint, retstep=retstep, dtype=dtype) + np_ret = _np.linspace(config, endpoint=endpoint, retstep=retstep, dtype=dtype) + if retstep: + assert_almost_equal(mx_ret[0].asnumpy(), np_ret[0], atol=1e-3, rtol=1e-5) + same(mx_ret[1], np_ret[1]) + else: + assert_almost_equal(mx_ret.asnumpy(), np_ret, atol=1e-3, rtol=1e-5) + + @npx.use_np + class TestLinspace(HybridBlock): + def __init__(self, start, stop, num=50, endpoint=None, retstep=False, dtype=None, axis=0): + super(TestLinspace, self).__init__() + self._start = start + self._stop = stop + self._num = num + self._endpoint = endpoint + self._retstep = retstep + self._dtype = dtype + + def hybrid_forward(self, F, x): + if self._retstep: + raise ValueError("linspace didn't support retstep = True inside HybridBlock") + else: + return x + F.np.linspace(self._start, self._stop, self._num, \ + self._endpoint, self._retstep, self._dtype) + + for dtype in dtypes: + x = np.zeros(shape=(), dtype=dtype) + for config in configs: + for hybridize in [False, True]: + for endpoint in [False, True]: + if isinstance(config, tuple): + net = TestLinspace(*config, endpoint=endpoint, dtype=dtype) + np_out = _np.linspace(*config, endpoint=endpoint, dtype=dtype) + else: + net = TestLinspace(config, endpoint=endpoint, dtype=dtype) + np_out = _np.linspace(config, endpoint=endpoint, dtype=dtype) + if hybridize: + net.hybridize() + mx_out = net(x) + assert_almost_equal(mx_out.asnumpy(), np_out, atol=1e-3, rtol=1e-5) + @with_seed() @npx.use_np_shape def test_np_argmax(): From d456709eba3a6eedfe4cc8338656e45d5bf47611 Mon Sep 17 00:00:00 2001 From: stu1130 Date: Sun, 16 Jun 2019 15:51:02 -0700 Subject: [PATCH 04/11] delete newline --- python/mxnet/numpy/multiarray.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/python/mxnet/numpy/multiarray.py b/python/mxnet/numpy/multiarray.py index eec46be3b5d5..cc234a232d26 100644 --- a/python/mxnet/numpy/multiarray.py +++ b/python/mxnet/numpy/multiarray.py @@ -1444,9 +1444,6 @@ def arange(start, stop=None, step=1, dtype=None, ctx=None): return _mx_nd_np.arange(start, stop, step, dtype, ctx) - - - @set_module('mxnet.numpy') def argmax(a, axis=None, out=None): """Returns the indices of the maximum values along an axis. From cc1a51e8e9b101903398e41d5f07203aaa62ab59 Mon Sep 17 00:00:00 2001 From: stu1130 Date: Sun, 16 Jun 2019 16:05:20 -0700 Subject: [PATCH 05/11] fix pylint --- python/mxnet/ndarray/numpy/_op.py | 2 +- python/mxnet/symbol/numpy/_symbol.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/python/mxnet/ndarray/numpy/_op.py b/python/mxnet/ndarray/numpy/_op.py index d68f41e79134..76f6ee8db77f 100644 --- a/python/mxnet/ndarray/numpy/_op.py +++ b/python/mxnet/ndarray/numpy/_op.py @@ -670,7 +670,7 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis step : float, optional Only returned if retstep is True Size of spacing between samples. - + Notes ----- This function currently does not support ``start`` and ``stop`` as ndarrays and diff --git a/python/mxnet/symbol/numpy/_symbol.py b/python/mxnet/symbol/numpy/_symbol.py index 10f3905cc2b9..32d472b98d06 100644 --- a/python/mxnet/symbol/numpy/_symbol.py +++ b/python/mxnet/symbol/numpy/_symbol.py @@ -1345,7 +1345,7 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis step : float, optional Only returned if retstep is True Size of spacing between samples. - + Notes ----- This function currently does not support ``start`` and ``stop`` as ndarrays and From 6b02e37bc37db3d8d44c9d0ecc1bef778d2d2fb5 Mon Sep 17 00:00:00 2001 From: stu1130 Date: Sun, 16 Jun 2019 22:22:53 -0700 Subject: [PATCH 06/11] add more unit test --- tests/python/unittest/test_numpy_op.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/python/unittest/test_numpy_op.py b/tests/python/unittest/test_numpy_op.py index 7dfe6268e503..7e4f55fc7c6c 100644 --- a/tests/python/unittest/test_numpy_op.py +++ b/tests/python/unittest/test_numpy_op.py @@ -558,7 +558,9 @@ def hybrid_forward(self, F, x): @npx.use_np_shape def test_np_linspace(): configs = [ - (0.0, 1.0, 10) + (0.0, 1.0, 10), + (-2, 4, 45), + (5.234324, 8.98324, 324) ] dtypes = ['int32', 'float16', 'float32', 'float64', None] for config in configs: From a41a51517bc0c83d7f4436925d37b8318522e5a8 Mon Sep 17 00:00:00 2001 From: stu1130 Date: Mon, 17 Jun 2019 10:31:26 -0700 Subject: [PATCH 07/11] address comment --- tests/python/unittest/test_numpy_op.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/python/unittest/test_numpy_op.py b/tests/python/unittest/test_numpy_op.py index 7e4f55fc7c6c..ca27bacf6e7a 100644 --- a/tests/python/unittest/test_numpy_op.py +++ b/tests/python/unittest/test_numpy_op.py @@ -613,6 +613,7 @@ def hybrid_forward(self, F, x): mx_out = net(x) assert_almost_equal(mx_out.asnumpy(), np_out, atol=1e-3, rtol=1e-5) + @with_seed() @npx.use_np_shape def test_np_argmax(): From 92c5536efcdd7db9634620fa103019e8ad504d76 Mon Sep 17 00:00:00 2001 From: stu1130 Date: Mon, 17 Jun 2019 14:01:42 -0700 Subject: [PATCH 08/11] add more test case --- tests/python/unittest/test_numpy_op.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tests/python/unittest/test_numpy_op.py b/tests/python/unittest/test_numpy_op.py index ca27bacf6e7a..9a04a8ab68c2 100644 --- a/tests/python/unittest/test_numpy_op.py +++ b/tests/python/unittest/test_numpy_op.py @@ -20,10 +20,11 @@ import numpy as _np import mxnet as mx from mxnet import np, npx +from mxnet.base import MXNetError from mxnet.gluon import HybridBlock from mxnet.test_utils import same, assert_almost_equal, rand_shape_nd, rand_ndarray from mxnet.test_utils import check_numeric_gradient -from common import with_seed +from common import assertRaises, with_seed import random @@ -560,7 +561,12 @@ def test_np_linspace(): configs = [ (0.0, 1.0, 10), (-2, 4, 45), - (5.234324, 8.98324, 324) + (5.234324, 8.98324, 324), + (2, 10, 100) + ] + exception_configs = [ + (0, 10, -1), + (0, 1, 2.5) ] dtypes = ['int32', 'float16', 'float32', 'float64', None] for config in configs: @@ -578,7 +584,12 @@ def test_np_linspace(): same(mx_ret[1], np_ret[1]) else: assert_almost_equal(mx_ret.asnumpy(), np_ret, atol=1e-3, rtol=1e-5) - + # check for exception input + for config in exception_configs: + assertRaises(MXNetError, np.linspace, *config) + # check linspace equivalent to arange + for test_index in range(1000): + assert_almost_equal(mx.np.linspace(0, test_index, test_index + 1).asnumpy(), mx.np.arange(test_index + 1).asnumpy()) @npx.use_np class TestLinspace(HybridBlock): def __init__(self, start, stop, num=50, endpoint=None, retstep=False, dtype=None, axis=0): From ba9449adcb9e50244f714e293c7c7df361f6e6b8 Mon Sep 17 00:00:00 2001 From: stu1130 Date: Mon, 17 Jun 2019 14:43:27 -0700 Subject: [PATCH 09/11] disable too-many-arguments --- python/mxnet/ndarray/numpy/_op.py | 2 +- python/mxnet/symbol/numpy/_symbol.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/python/mxnet/ndarray/numpy/_op.py b/python/mxnet/ndarray/numpy/_op.py index 76f6ee8db77f..9d2444e4f03b 100644 --- a/python/mxnet/ndarray/numpy/_op.py +++ b/python/mxnet/ndarray/numpy/_op.py @@ -632,7 +632,7 @@ def tile(A, reps): return _npi.tile(A, reps) -def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0, ctx=None): +def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0, ctx=None): # pylint: disable=too-many-lines """Return evenly spaced numbers over a specified interval. Returns num evenly spaced samples, calculated over the interval [start, stop]. diff --git a/python/mxnet/symbol/numpy/_symbol.py b/python/mxnet/symbol/numpy/_symbol.py index 32d472b98d06..02a9b5826dd4 100644 --- a/python/mxnet/symbol/numpy/_symbol.py +++ b/python/mxnet/symbol/numpy/_symbol.py @@ -1307,7 +1307,7 @@ def tile(A, reps): return _npi.tile(A, reps) -def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0, ctx=None): +def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0, ctx=None): # pylint: disable=too-many-lines """Return evenly spaced numbers over a specified interval. Returns num evenly spaced samples, calculated over the interval [start, stop]. From 96ea48cd2eda6a61dd858e17352f7174eb1adacf Mon Sep 17 00:00:00 2001 From: stu1130 Date: Mon, 17 Jun 2019 16:57:11 -0700 Subject: [PATCH 10/11] resolve confliction --- python/mxnet/ndarray/numpy/_op.py | 2 +- python/mxnet/symbol/numpy/_symbol.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/python/mxnet/ndarray/numpy/_op.py b/python/mxnet/ndarray/numpy/_op.py index 9d2444e4f03b..6e6f210fd6cd 100644 --- a/python/mxnet/ndarray/numpy/_op.py +++ b/python/mxnet/ndarray/numpy/_op.py @@ -632,7 +632,7 @@ def tile(A, reps): return _npi.tile(A, reps) -def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0, ctx=None): # pylint: disable=too-many-lines +def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0, ctx=None): #pylint: disable=too-many-arguments """Return evenly spaced numbers over a specified interval. Returns num evenly spaced samples, calculated over the interval [start, stop]. diff --git a/python/mxnet/symbol/numpy/_symbol.py b/python/mxnet/symbol/numpy/_symbol.py index 02a9b5826dd4..83a79b26eeef 100644 --- a/python/mxnet/symbol/numpy/_symbol.py +++ b/python/mxnet/symbol/numpy/_symbol.py @@ -31,7 +31,7 @@ __all__ = ['zeros', 'ones', 'maximum', 'minimum', 'stack', 'concatenate', 'arange', 'argmax', 'clip', 'add', 'subtract', 'multiply', 'divide', 'mod', 'power', 'split', 'swapaxes', - 'expand_dims', 'linspace'] + 'expand_dims', 'tile', 'linspace'] def _num_outputs(sym): @@ -1307,7 +1307,7 @@ def tile(A, reps): return _npi.tile(A, reps) -def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0, ctx=None): # pylint: disable=too-many-lines +def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0, ctx=None): # pylint: disable=too-many-arguments """Return evenly spaced numbers over a specified interval. Returns num evenly spaced samples, calculated over the interval [start, stop]. From 0513ddf870a5408e64009eb09b9e008bdaefd42f Mon Sep 17 00:00:00 2001 From: stu1130 Date: Wed, 19 Jun 2019 18:19:01 -0700 Subject: [PATCH 11/11] add ctx --- python/mxnet/ndarray/numpy/_op.py | 10 ++++++---- python/mxnet/numpy/multiarray.py | 5 +++-- python/mxnet/symbol/numpy/_symbol.py | 10 ++++++---- tests/python/unittest/test_numpy_op.py | 2 +- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/python/mxnet/ndarray/numpy/_op.py b/python/mxnet/ndarray/numpy/_op.py index 6e6f210fd6cd..cf14d89bdbd2 100644 --- a/python/mxnet/ndarray/numpy/_op.py +++ b/python/mxnet/ndarray/numpy/_op.py @@ -632,7 +632,8 @@ def tile(A, reps): return _npi.tile(A, reps) -def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0, ctx=None): #pylint: disable=too-many-arguments +@set_module('mxnet.ndarray.numpy') +def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0, **kwargs): #pylint: disable=too-many-arguments """Return evenly spaced numbers over a specified interval. Returns num evenly spaced samples, calculated over the interval [start, stop]. @@ -681,10 +682,11 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis raise NotImplementedError('start and stop only support int') if axis != 0: raise NotImplementedError("the function only support axis 0") - if not ctx: + ctx = kwargs.pop('ctx', current_context()) + if ctx is None: ctx = current_context() if retstep: step = (stop - start) / (num - 1) - return (_npi.linspace(start=start, stop=stop, num=num, endpoint=endpoint, dtype=dtype), step) + return (_npi.linspace(start=start, stop=stop, num=num, endpoint=endpoint, ctx=ctx, dtype=dtype), step) else: - return _npi.linspace(start=start, stop=stop, num=num, endpoint=endpoint, dtype=dtype) + return _npi.linspace(start=start, stop=stop, num=num, endpoint=endpoint, ctx=ctx, dtype=dtype) diff --git a/python/mxnet/numpy/multiarray.py b/python/mxnet/numpy/multiarray.py index cc234a232d26..9598593593e9 100644 --- a/python/mxnet/numpy/multiarray.py +++ b/python/mxnet/numpy/multiarray.py @@ -1793,7 +1793,8 @@ def tile(A, reps): return _npi.tile(A, reps) -def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0, ctx=None): +@set_module('mxnet.numpy') +def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0, **kwargs): """Return evenly spaced numbers over a specified interval. Returns num evenly spaced samples, calculated over the interval [start, stop]. @@ -1832,4 +1833,4 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis Only returned if retstep is True Size of spacing between samples. """ - return _mx_nd_np.linspace(start, stop, num, endpoint, retstep, dtype, axis, ctx) + return _mx_nd_np.linspace(start, stop, num, endpoint, retstep, dtype, axis, **kwargs) diff --git a/python/mxnet/symbol/numpy/_symbol.py b/python/mxnet/symbol/numpy/_symbol.py index 83a79b26eeef..e015b7a1a670 100644 --- a/python/mxnet/symbol/numpy/_symbol.py +++ b/python/mxnet/symbol/numpy/_symbol.py @@ -1307,7 +1307,8 @@ def tile(A, reps): return _npi.tile(A, reps) -def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0, ctx=None): # pylint: disable=too-many-arguments +@set_module('mxnet.symbol.numpy') +def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0, **kwargs): # pylint: disable=too-many-arguments """Return evenly spaced numbers over a specified interval. Returns num evenly spaced samples, calculated over the interval [start, stop]. @@ -1356,13 +1357,14 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis raise NotImplementedError('start and stop only support int') if axis != 0: raise NotImplementedError("the function only support axis 0") - if not ctx: + ctx = kwargs.pop('ctx', current_context()) + if ctx is None: ctx = current_context() if retstep: step = (stop - start) / (num - 1) - return (_npi.linspace(start=start, stop=stop, num=num, endpoint=endpoint, dtype=dtype), step) + return (_npi.linspace(start=start, stop=stop, num=num, endpoint=endpoint, ctx=ctx, dtype=dtype), step) else: - return _npi.linspace(start=start, stop=stop, num=num, endpoint=endpoint, dtype=dtype) + return _npi.linspace(start=start, stop=stop, num=num, endpoint=endpoint, ctx=ctx, dtype=dtype) _set_np_symbol_class(_Symbol) diff --git a/tests/python/unittest/test_numpy_op.py b/tests/python/unittest/test_numpy_op.py index 9a04a8ab68c2..07c57dda4097 100644 --- a/tests/python/unittest/test_numpy_op.py +++ b/tests/python/unittest/test_numpy_op.py @@ -560,7 +560,7 @@ def hybrid_forward(self, F, x): def test_np_linspace(): configs = [ (0.0, 1.0, 10), - (-2, 4, 45), + (-2, 4, 30), (5.234324, 8.98324, 324), (2, 10, 100) ]