szha closed pull request #8329: fluent methods for missed ops URL: https://github.com/apache/incubator-mxnet/pull/8329
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/docs/api/python/ndarray/ndarray.md b/docs/api/python/ndarray/ndarray.md index 615b9dc5a7..09564c2f20 100644 --- a/docs/api/python/ndarray/ndarray.md +++ b/docs/api/python/ndarray/ndarray.md @@ -125,6 +125,7 @@ The `ndarray` package provides several classes: NDArray.T NDArray.reshape + NDArray.reshape_like NDArray.flatten NDArray.expand_dims NDArray.split @@ -194,6 +195,7 @@ The `ndarray` package provides several classes: NDArray.topk NDArray.argmax NDArray.argmin + NDArray.argmax_channel ``` ### Arithmetic operations @@ -266,7 +268,22 @@ The `ndarray` package provides several classes: NDArray.sqrt NDArray.rsqrt + NDArray.cbrt + NDArray.rcbrt NDArray.square + NDArray.reciprocal +``` + +## Basic neural network functions + +```eval_rst +.. autosummary:: + :nosignatures: + + NDArray.relu + NDArray.sigmoid + NDArray.softmax + NDArray.log_softmax ``` ### In-place arithmetic operations @@ -358,6 +375,7 @@ The `ndarray` package provides several classes: cast reshape + reshape_like flatten expand_dims ``` @@ -394,6 +412,7 @@ The `ndarray` package provides several classes: concat split + stack ``` ### Indexing routines @@ -514,11 +533,13 @@ The `ndarray` package provides several classes: power sqrt rsqrt + cbrt + rcbrt square reciprocal ``` -### Logic functions +### Comparison ```eval_rst .. autosummary:: @@ -559,6 +580,18 @@ The `ndarray` package provides several classes: argsort argmax argmin + argmax_channel +``` + +### Sequence operation + +```eval_rst +.. autosummary:: + :nosignatures: + + SequenceLast + SequenceMask + SequenceReverse ``` ### Miscellaneous @@ -592,6 +625,8 @@ The `ndarray` package provides several classes: SoftmaxOutput softmax log_softmax + relu + sigmoid ``` ### More diff --git a/docs/api/python/symbol/symbol.md b/docs/api/python/symbol/symbol.md index 7570e18ba7..e93976d603 100644 --- a/docs/api/python/symbol/symbol.md +++ b/docs/api/python/symbol/symbol.md @@ -143,9 +143,23 @@ Composite multiple symbols into a new one by an operator. Symbol.sqrt Symbol.rsqrt + Symbol.cbrt + Symbol.rcbrt Symbol.square ``` +## Basic neural network functions + +```eval_rst +.. autosummary:: + :nosignatures: + + Symbol.relu + Symbol.sigmoid + Symbol.softmax + Symbol.log_softmax +``` + #### Comparison operators ```eval_rst @@ -178,6 +192,7 @@ Composite multiple symbols into a new one by an operator. Symbol.astype Symbol.reshape + Symbol.reshape_like Symbol.flatten Symbol.expand_dims ``` @@ -246,6 +261,7 @@ Composite multiple symbols into a new one by an operator. Symbol.topk Symbol.argmax Symbol.argmin + Symbol.argmax_channel ``` ### Query information @@ -355,6 +371,7 @@ Composite multiple symbols into a new one by an operator. cast reshape + reshape_like flatten expand_dims ``` @@ -391,6 +408,7 @@ Composite multiple symbols into a new one by an operator. concat split + stack ``` ### Indexing routines @@ -424,7 +442,6 @@ Composite multiple symbols into a new one by an operator. broadcast_div broadcast_mod negative - reciprocal dot batch_dot add_n @@ -492,7 +509,6 @@ Composite multiple symbols into a new one by an operator. trunc ``` - ### Exponents and logarithms ```eval_rst @@ -519,9 +535,10 @@ Composite multiple symbols into a new one by an operator. cbrt rcbrt square + reciprocal ``` -### Logic functions +### Comparison ```eval_rst .. autosummary:: @@ -534,6 +551,7 @@ Composite multiple symbols into a new one by an operator. broadcast_lesser broadcast_lesser_equal ``` + ### Random sampling ```eval_rst @@ -561,6 +579,18 @@ Composite multiple symbols into a new one by an operator. argsort argmax argmin + argmax_channel +``` + +### Sequence operation + +```eval_rst +.. autosummary:: + :nosignatures: + + SequenceLast + SequenceMask + SequenceReverse ``` ### Miscellaneous @@ -596,6 +626,8 @@ Composite multiple symbols into a new one by an operator. SoftmaxOutput softmax log_softmax + relu + sigmoid ``` ### More diff --git a/python/mxnet/ndarray/ndarray.py b/python/mxnet/ndarray/ndarray.py index 2f9972b21b..1cd9f40e52 100644 --- a/python/mxnet/ndarray/ndarray.py +++ b/python/mxnet/ndarray/ndarray.py @@ -736,6 +736,14 @@ def reshape(self, shape): ctypes.byref(handle))) return NDArray(handle=handle, writable=self.writable) + def reshape_like(self, *args, **kwargs): + """Convenience fluent method for :py:func:`reshape_like`. + + The arguments are the same as for :py:func:`reshape_like`, with + this array as data. + """ + return op.reshape_like(self, *args, **kwargs) + def zeros_like(self, *args, **kwargs): """Convenience fluent method for :py:func:`zeros_like`. @@ -864,6 +872,14 @@ def argmax(self, *args, **kwargs): """ return op.argmax(self, *args, **kwargs) + def argmax_channel(self, *args, **kwargs): + """Convenience fluent method for :py:func:`argmax_channel`. + + The arguments are the same as for :py:func:`argmax_channel`, with + this array as data. + """ + return op.argmax_channel(self, *args, **kwargs) + def argmin(self, *args, **kwargs): """Convenience fluent method for :py:func:`argmin`. @@ -1224,6 +1240,22 @@ def rsqrt(self, *args, **kwargs): """ return op.rsqrt(self, *args, **kwargs) + def cbrt(self, *args, **kwargs): + """Convenience fluent method for :py:func:`cbrt`. + + The arguments are the same as for :py:func:`cbrt`, with + this array as data. + """ + return op.cbrt(self, *args, **kwargs) + + def rcbrt(self, *args, **kwargs): + """Convenience fluent method for :py:func:`rcbrt`. + + The arguments are the same as for :py:func:`rcbrt`, with + this array as data. + """ + return op.rcbrt(self, *args, **kwargs) + def square(self, *args, **kwargs): """Convenience fluent method for :py:func:`square`. @@ -1232,6 +1264,46 @@ def square(self, *args, **kwargs): """ return op.square(self, *args, **kwargs) + def reciprocal(self, *args, **kwargs): + """Convenience fluent method for :py:func:`reciprocal`. + + The arguments are the same as for :py:func:`reciprocal`, with + this array as data. + """ + return op.reciprocal(self, *args, **kwargs) + + def relu(self, *args, **kwargs): + """Convenience fluent method for :py:func:`relu`. + + The arguments are the same as for :py:func:`relu`, with + this array as data. + """ + return op.relu(self, *args, **kwargs) + + def sigmoid(self, *args, **kwargs): + """Convenience fluent method for :py:func:`sigmoid`. + + The arguments are the same as for :py:func:`sigmoid`, with + this array as data. + """ + return op.sigmoid(self, *args, **kwargs) + + def softmax(self, *args, **kwargs): + """Convenience fluent method for :py:func:`softmax`. + + The arguments are the same as for :py:func:`softmax`, with + this array as data. + """ + return op.softmax(self, *args, **kwargs) + + def log_softmax(self, *args, **kwargs): + """Convenience fluent method for :py:func:`log_softmax`. + + The arguments are the same as for :py:func:`log_softmax`, with + this array as data. + """ + return op.log_softmax(self, *args, **kwargs) + # pylint: disable= undefined-variable def broadcast_to(self, shape): """Broadcasts the input array to a new shape. diff --git a/python/mxnet/symbol/symbol.py b/python/mxnet/symbol/symbol.py index 3c76826cdd..6903db0b0d 100644 --- a/python/mxnet/symbol/symbol.py +++ b/python/mxnet/symbol/symbol.py @@ -1745,6 +1745,14 @@ def reshape(self, *args, **kwargs): """ return op.reshape(self, *args, **kwargs) + def reshape_like(self, *args, **kwargs): + """Convenience fluent method for :py:func:`reshape_like`. + + The arguments are the same as for :py:func:`reshape_like`, with + this array as data. + """ + return op.reshape_like(self, *args, **kwargs) + def astype(self, *args, **kwargs): """Convenience fluent method for :py:func:`cast`. @@ -1881,6 +1889,14 @@ def argmax(self, *args, **kwargs): """ return op.argmax(self, *args, **kwargs) + def argmax_channel(self, *args, **kwargs): + """Convenience fluent method for :py:func:`argmax_channel`. + + The arguments are the same as for :py:func:`argmax_channel`, with + this array as data. + """ + return op.argmax_channel(self, *args, **kwargs) + def argmin(self, *args, **kwargs): """Convenience fluent method for :py:func:`argmin`. @@ -2249,6 +2265,22 @@ def rsqrt(self, *args, **kwargs): """ return op.rsqrt(self, *args, **kwargs) + def cbrt(self, *args, **kwargs): + """Convenience fluent method for :py:func:`cbrt`. + + The arguments are the same as for :py:func:`cbrt`, with + this array as data. + """ + return op.cbrt(self, *args, **kwargs) + + def rcbrt(self, *args, **kwargs): + """Convenience fluent method for :py:func:`rcbrt`. + + The arguments are the same as for :py:func:`rcbrt`, with + this array as data. + """ + return op.rcbrt(self, *args, **kwargs) + def square(self, *args, **kwargs): """Convenience fluent method for :py:func:`square`. @@ -2257,6 +2289,46 @@ def square(self, *args, **kwargs): """ return op.square(self, *args, **kwargs) + def reciprocal(self, *args, **kwargs): + """Convenience fluent method for :py:func:`reciprocal`. + + The arguments are the same as for :py:func:`reciprocal`, with + this array as data. + """ + return op.reciprocal(self, *args, **kwargs) + + def relu(self, *args, **kwargs): + """Convenience fluent method for :py:func:`relu`. + + The arguments are the same as for :py:func:`relu`, with + this array as data. + """ + return op.relu(self, *args, **kwargs) + + def sigmoid(self, *args, **kwargs): + """Convenience fluent method for :py:func:`sigmoid`. + + The arguments are the same as for :py:func:`sigmoid`, with + this array as data. + """ + return op.sigmoid(self, *args, **kwargs) + + def softmax(self, *args, **kwargs): + """Convenience fluent method for :py:func:`softmax`. + + The arguments are the same as for :py:func:`softmax`, with + this array as data. + """ + return op.softmax(self, *args, **kwargs) + + def log_softmax(self, *args, **kwargs): + """Convenience fluent method for :py:func:`log_softmax`. + + The arguments are the same as for :py:func:`log_softmax`, with + this array as data. + """ + return op.log_softmax(self, *args, **kwargs) + def wait_to_read(self): raise NotImplementedForSymbol(self.wait_to_read, None) diff --git a/tests/python/unittest/test_ndarray.py b/tests/python/unittest/test_ndarray.py index e5dddeb95a..576d963540 100644 --- a/tests/python/unittest/test_ndarray.py +++ b/tests/python/unittest/test_ndarray.py @@ -742,7 +742,9 @@ def test_ndarray_fluent(): 'one_hot', 'pick', 'sort', 'topk', 'argsort', 'argmax', 'argmin', 'clip', 'abs', 'sign', 'sin', 'cos', 'tan', 'arcsin', 'arccos', 'arctan', 'degrees', 'radians', 'sinh', 'cosh', 'tanh', 'arcsinh', 'arccosh', 'arctanh', - 'exp', 'expm1', 'log', 'log10', 'log2', 'log1p', 'sqrt', 'rsqrt', 'square']) + 'exp', 'expm1', 'log', 'log10', 'log2', 'log1p', 'sqrt', 'rsqrt', 'square', + 'reshape_like', 'cbrt', 'rcbrt', 'relu', 'sigmoid', 'softmax', 'log_softmax', + 'reciprocal']) def check_fluent_regular(func, kwargs, shape=(5, 17, 1), equal_nan=False): with mx.name.NameManager(): data = mx.nd.random_uniform(shape=shape, ctx=default_context()) @@ -756,11 +758,12 @@ def check_fluent_regular(func, kwargs, shape=(5, 17, 1), equal_nan=False): for func in ['flatten', 'norm', 'round', 'rint', 'fix', 'floor', 'ceil', 'trunc', 'zeros_like', 'ones_like', 'abs', 'sign', 'sin', 'cos', 'degrees', 'radians', - 'exp', 'expm1', 'square']: + 'exp', 'expm1', 'square', 'reciprocal', 'argmax_channel']: check_fluent_regular(func, {}) for func in ['arccosh', 'arcsin', 'arccos', 'arctan', 'tan', 'sinh', 'cosh', 'tanh', - 'arcsinh', 'arctanh', 'log', 'log10', 'log2', 'log1p', 'sqrt', 'rsqrt']: + 'arcsinh', 'arctanh', 'log', 'log10', 'log2', 'log1p', 'sqrt', 'rsqrt', + 'cbrt', 'rcbrt', 'relu', 'sigmoid', 'softmax', 'log_softmax']: check_fluent_regular(func, {}, equal_nan=True) for func in ['expand_dims', 'flip', 'sort', 'topk', 'argsort', 'argmax', 'argmin']: @@ -778,6 +781,7 @@ def check_fluent_regular(func, kwargs, shape=(5, 17, 1), equal_nan=False): check_fluent_regular('clip', {'a_min': 0.25, 'a_max': 0.75}) check_fluent_regular('broadcast_axes', {'axis': (2,), 'size': (5,)}) check_fluent_regular('pad', {'mode': 'constant', 'pad_width': (0,0,0,0,3,0,0,4)}, shape=(5, 17, 2, 3)) + check_fluent_regular('reshape_like', {'rhs': mx.nd.ones((30, 17))}, shape=(5, 17, 2, 3)) for func in ['sum', 'nansum', 'prod', 'nanprod', 'mean', 'max', 'min']: check_fluent_regular(func, {'axis': (1, 2)}) diff --git a/tests/python/unittest/test_symbol.py b/tests/python/unittest/test_symbol.py index 2d31c600a4..30e76a272e 100644 --- a/tests/python/unittest/test_symbol.py +++ b/tests/python/unittest/test_symbol.py @@ -168,7 +168,8 @@ def test_symbol_fluent(): 'clip', 'abs', 'sign', 'sin', 'cos', 'tan', 'arcsin', 'arccos', 'arctan', 'degrees', 'radians', 'sinh', 'cosh', 'tanh', 'arcsinh', 'arccosh', 'arctanh', 'exp', 'expm1', 'log', 'log10', 'log2', 'log1p', 'sqrt', 'rsqrt', - 'square']) + 'square', 'reciprocal' 'reshape_like', 'cbrt', 'rcbrt', 'relu', 'sigmoid', + 'softmax', 'log_softmax']) def check_fluent_regular(func, kwargs, shape=(5, 17, 1), equal_nan=False): with mx.name.NameManager(): data = mx.symbol.Variable('data') @@ -181,11 +182,12 @@ def check_fluent_regular(func, kwargs, shape=(5, 17, 1), equal_nan=False): for func in ['flatten', 'norm', 'round', 'rint', 'fix', 'floor', 'ceil', 'trunc', 'zeros_like', 'ones_like', 'abs', 'sign', 'sin', 'cos', 'degrees', 'radians', - 'exp', 'expm1', 'square']: + 'exp', 'expm1', 'square', 'reciprocal', 'argmax_channel']: check_fluent_regular(func, {}) for func in ['arccosh', 'arcsin', 'arccos', 'arctan', 'tan', 'sinh', 'cosh', 'tanh', - 'arcsinh', 'arctanh', 'log', 'log10', 'log2', 'log1p', 'sqrt', 'rsqrt']: + 'arcsinh', 'arctanh', 'log', 'log10', 'log2', 'log1p', 'sqrt', 'rsqrt', + 'cbrt', 'rcbrt', 'relu', 'sigmoid', 'softmax', 'log_softmax']: check_fluent_regular(func, {}, equal_nan=True) for func in ['expand_dims', 'flip', 'sort', 'topk', 'argsort', 'argmax', 'argmin']: @@ -201,6 +203,7 @@ def check_fluent_regular(func, kwargs, shape=(5, 17, 1), equal_nan=False): check_fluent_regular('clip', {'a_min': 0.25, 'a_max': 0.75}) check_fluent_regular('broadcast_axes', {'axis': (2,), 'size': (5,)}) check_fluent_regular('pad', {'mode': 'constant', 'pad_width': (0,0,0,0,3,0,0,4)}, shape=(5, 17, 2, 3)) + check_fluent_regular('reshape_like', {'rhs': mx.sym.ones((30, 17))}, shape=(5, 17, 2, 3)) for func in ['sum', 'nansum', 'prod', 'nanprod', 'mean', 'max', 'min']: check_fluent_regular(func, {'axis': (1, 2)}) ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services