diff --git a/python/paddle/fluid/layers/control_flow.py b/python/paddle/fluid/layers/control_flow.py index 2002b8a95decfd6d6c55538e2dff0a793828dd9b..96417ecda0a749e187dd0c27401a60053a685087 100755 --- a/python/paddle/fluid/layers/control_flow.py +++ b/python/paddle/fluid/layers/control_flow.py @@ -3788,7 +3788,7 @@ def reorder_lod_tensor_by_rank(x, rank_table): return out -def is_empty(x, cond=None): +def is_empty(x, name=None): """ :alias_main: paddle.is_empty :alias: paddle.is_empty,paddle.tensor.is_empty,paddle.tensor.logic.is_empty @@ -3798,35 +3798,60 @@ def is_empty(x, cond=None): Args: x (Variable): The Variable to be tested. - cond (Variable, optional): Output parameter. Default: None. If this parameter is given, it - saves the test result of given 'x'. + name (str, optional): The default value is ``None`` . Normally users + don't have to set this parameter. For more information, + please refer to :ref:`api_guide_Name` . Returns: Variable: A bool scalar. True if 'x' is an empty Variable. - Raises: - TypeError: If input cond is not a variable, or cond's dtype is - not bool. - Examples: .. code-block:: python - import paddle.fluid as fluid - input = fluid.layers.data(name="input", shape=[4, 32, 32], dtype="float32") - res = fluid.layers.is_empty(x=input) - # or: - # fluid.layers.is_empty(x=input, cond=res) + # static mode + import numpy as np + import paddle + + paddle.enable_static() + input = paddle.static.data(name="input", shape=[4, 32, 32], dtype="float32") + res = paddle.is_empty(x=input) + + exe = paddle.static.Executor(paddle.CPUPlace()) + data = np.ones((4, 32, 32)).astype(np.float32) + out = exe.run(feed={'input':data}, fetch_list=[res]) + print("is_empty: ", out) + # ('out:', [array([False])]) + + + .. code-block:: python + + # dygraph_mode + import paddle + + input = paddle.rand(shape=[4, 32, 32], dtype='float32') + res = paddle.is_empty(x=input) + print("res:", res) + # ('res:', Tensor: eager_tmp_1 + # - place: CPUPlace + # - shape: [1] + # - layout: NCHW + # - dtype: bool + # - data: [0]) """ + if in_dygraph_mode(): + assert isinstance( + x, Variable + ), "The input data 'x' in is_empty must be Variable in dygraph mode" + return core.ops.is_empty(x) + check_variable_and_dtype(x, 'x', ['float32', 'float64', 'int32', 'int64'], 'is_empty') - check_type(cond, 'cond', (Variable, type(None)), 'is_empty') + check_type(name, "name", (str, type(None)), "is_empty") + helper = LayerHelper("is_empty", **locals()) - if cond is None: - cond = helper.create_variable_for_type_inference(dtype='bool') - cond.stop_gradient = True - else: - check_dtype(cond.dtype, 'cond', ['bool'], 'is_empty') + cond = helper.create_variable_for_type_inference(dtype='bool') + cond.stop_gradient = True helper.append_op( type='is_empty', inputs={'X': [x]}, outputs={'Out': [cond]}) return cond