From 848ec97ab33870692a84349f304502ee4c0f3fd1 Mon Sep 17 00:00:00 2001 From: zhoukunsheng Date: Wed, 27 Mar 2019 18:54:21 +0800 Subject: [PATCH] test=develop add zeros_like op --- paddle/fluid/API.spec | 1 + python/paddle/fluid/layers/tensor.py | 30 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/paddle/fluid/API.spec b/paddle/fluid/API.spec index 032da0cad8..14c9f30ffd 100644 --- a/paddle/fluid/API.spec +++ b/paddle/fluid/API.spec @@ -266,6 +266,7 @@ paddle.fluid.layers.has_inf (ArgSpec(args=['x'], varargs=None, keywords=None, de paddle.fluid.layers.has_nan (ArgSpec(args=['x'], varargs=None, keywords=None, defaults=None), ('document', '2e53e83127dbfd86e7098bdfe9a549e8')) paddle.fluid.layers.isfinite (ArgSpec(args=['x'], varargs=None, keywords=None, defaults=None), ('document', '0a437011c3906079fd8947ed3e52d292')) paddle.fluid.layers.range (ArgSpec(args=['start', 'end', 'step', 'dtype'], varargs=None, keywords=None, defaults=None), ('document', '2ec937ede953ded2fdff2675883900bb')) +paddle.fluid.layers.zeros_like (ArgSpec(args=['x', 'out'], varargs=None, keywords=None, defaults=(None,)), ('document', '4516005c2efb5f1b0adb4d0bac231408')) paddle.fluid.layers.While.__init__ (ArgSpec(args=['self', 'cond', 'is_test', 'name'], varargs=None, keywords=None, defaults=(False, None)), ('document', '6adf97f83acf6453d4a6a4b1070f3754')) paddle.fluid.layers.While.block (ArgSpec(args=['self'], varargs=None, keywords=None, defaults=None), ('document', '6adf97f83acf6453d4a6a4b1070f3754')) paddle.fluid.layers.Switch.__init__ (ArgSpec(args=['self', 'name'], varargs=None, keywords=None, defaults=(None,)), ('document', '6adf97f83acf6453d4a6a4b1070f3754')) diff --git a/python/paddle/fluid/layers/tensor.py b/python/paddle/fluid/layers/tensor.py index a18e5b6a9c..cc89d49fa8 100644 --- a/python/paddle/fluid/layers/tensor.py +++ b/python/paddle/fluid/layers/tensor.py @@ -45,6 +45,7 @@ __all__ = [ 'has_nan', 'isfinite', 'range', + 'zeros_like', ] @@ -827,3 +828,32 @@ def range(start, end, step, dtype): 'Step': step}, outputs={'Out': [out]}) return out + + +def zeros_like(x, out=None): + """ + **zeros_like** + + This function creates a zeros tensor which has identical shape and dtype + with `x`. + + Args: + x(Variable): The input tensor which specifies shape and dtype. + out(Variable): The output tensor. + + Returns: + Variable: The tensor variable storing the output. + + Examples: + .. code-block:: python + + data = fluid.layers.zeros_like(x) + """ + + helper = LayerHelper("zeros_like", **locals()) + if out is None: + out = helper.create_variable_for_type_inference(dtype=x.dtype) + helper.append_op( + type='fill_zeros_like', inputs={'X': [x]}, outputs={'Out': [out]}) + out.stop_gradient = True + return out -- GitLab