From 2ff7ea3337322a229884af73f96474b747750a63 Mon Sep 17 00:00:00 2001 From: zhoukunsheng Date: Tue, 14 May 2019 10:41:01 +0800 Subject: [PATCH] Expose sign op (#17117) * test=develop add sign op * Update nn.py test=develop delete stop_gradient assignment --- paddle/fluid/API.spec | 1 + python/paddle/fluid/layers/nn.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/paddle/fluid/API.spec b/paddle/fluid/API.spec index 82dd3d3e65d..5c92a29bb33 100644 --- a/paddle/fluid/API.spec +++ b/paddle/fluid/API.spec @@ -236,6 +236,7 @@ paddle.fluid.layers.pixel_shuffle (ArgSpec(args=['x', 'upscale_factor'], varargs paddle.fluid.layers.fsp_matrix (ArgSpec(args=['x', 'y'], varargs=None, keywords=None, defaults=None), ('document', 'b76ccca3735bea4a58a0dbf0d77c5393')) paddle.fluid.layers.continuous_value_model (ArgSpec(args=['input', 'cvm', 'use_cvm'], varargs=None, keywords=None, defaults=(True,)), ('document', 'a07a44c2bacdcd09c1f5f35a96a0514e')) paddle.fluid.layers.where (ArgSpec(args=['condition'], varargs=None, keywords=None, defaults=None), ('document', '3126e3039e752ce26077f1efaca355c6')) +paddle.fluid.layers.sign (ArgSpec(args=['x'], varargs=None, keywords=None, defaults=None), ('document', 'ccf6bb7912afd2818d24bc45461e807a')) paddle.fluid.layers.data (ArgSpec(args=['name', 'shape', 'append_batch_size', 'dtype', 'lod_level', 'type', 'stop_gradient'], varargs=None, keywords=None, defaults=(True, 'float32', 0, VarType.LOD_TENSOR, True)), ('document', 'adf285346e23316097f7789b572491e9')) paddle.fluid.layers.open_files (ArgSpec(args=['filenames', 'shapes', 'lod_levels', 'dtypes', 'thread_num', 'buffer_size', 'pass_num', 'is_test'], varargs=None, keywords=None, defaults=(None, None, 1, None)), ('document', 'cf12066a3139026119f97f9d4381a1bd')) paddle.fluid.layers.read_file (ArgSpec(args=['reader'], varargs=None, keywords=None, defaults=None), ('document', 'b0a1c2fc51c27a106da28f3308c41f5e')) diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index ea8ddc91eca..bab76fdfcb1 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -201,6 +201,7 @@ __all__ = [ 'fsp_matrix', 'continuous_value_model', 'where', + 'sign', ] kIgnoreIndex = -100 @@ -11396,3 +11397,34 @@ def where(condition): helper.append_op( type='where', inputs={'Condition': condition}, outputs={'Out': [out]}) return out + + +def sign(x): + """ + **sign** + + This function returns sign of every element in `x`: 1 for positive, -1 for negative and 0 for zero. + + Args: + x(Variable|numpy.ndarray): The input tensor. + + Returns: + Variable: The output sign tensor with identical shape and dtype to `x`. + + Examples: + .. code-block:: python + + # [1, 0, -1] + data = fluid.layers.sign(np.array([3, 0, -2])) + """ + + helper = LayerHelper("sign", **locals()) + + if not isinstance(x, Variable): + x = assign(x) + + out = helper.create_variable_for_type_inference(dtype=x.dtype) + + helper.append_op(type='sign', inputs={'X': [x]}, outputs={'Out': [out]}) + + return out -- GitLab