未验证 提交 6ad72106 编写于 作者: D danleifeng 提交者: GitHub

[API 2.0] Fix api 'is_empty' (#27593)

* fix is_empty api and code example; test=develop
上级 ee13a2ab
...@@ -3788,45 +3788,46 @@ def reorder_lod_tensor_by_rank(x, rank_table): ...@@ -3788,45 +3788,46 @@ def reorder_lod_tensor_by_rank(x, rank_table):
return out 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
:old_api: paddle.fluid.layers.is_empty
Test whether a Variable is empty. Test whether a Tensor is empty.
Args: Args:
x (Variable): The Variable to be tested. x (Tensor): The Tensor to be tested.
cond (Variable, optional): Output parameter. Default: None. If this parameter is given, it name (str, optional): The default value is ``None`` . Normally users
saves the test result of given 'x'. don't have to set this parameter. For more information,
please refer to :ref:`api_guide_Name` .
Returns: Returns:
Variable: A bool scalar. True if 'x' is an empty Variable. Tensor: A bool scalar Tensor. True if 'x' is an empty Tensor.
Raises:
TypeError: If input cond is not a variable, or cond's dtype is
not bool.
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
input = fluid.layers.data(name="input", shape=[4, 32, 32], dtype="float32")
res = fluid.layers.is_empty(x=input) input = paddle.rand(shape=[4, 32, 32], dtype='float32')
# or: res = paddle.is_empty(x=input)
# fluid.layers.is_empty(x=input, cond=res) print("res:", res)
# ('res:', Tensor: eager_tmp_1
# - place: CPUPlace
# - shape: [1]
# - layout: NCHW
# - dtype: bool
# - data: [0])
""" """
if in_dygraph_mode():
return core.ops.is_empty(x)
check_variable_and_dtype(x, 'x', ['float32', 'float64', 'int32', 'int64'], check_variable_and_dtype(x, 'x', ['float32', 'float64', 'int32', 'int64'],
'is_empty') '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()) helper = LayerHelper("is_empty", **locals())
if cond is None: cond = helper.create_variable_for_type_inference(dtype='bool')
cond = helper.create_variable_for_type_inference(dtype='bool') cond.stop_gradient = True
cond.stop_gradient = True
else:
check_dtype(cond.dtype, 'cond', ['bool'], 'is_empty')
helper.append_op( helper.append_op(
type='is_empty', inputs={'X': [x]}, outputs={'Out': [cond]}) type='is_empty', inputs={'X': [x]}, outputs={'Out': [cond]})
return cond return cond
...@@ -17,7 +17,7 @@ from __future__ import print_function ...@@ -17,7 +17,7 @@ from __future__ import print_function
import unittest import unittest
import numpy as np import numpy as np
from op_test import OpTest from op_test import OpTest
import paddle.fluid as fluid import paddle
class TestEmpty(OpTest): class TestEmpty(OpTest):
...@@ -39,39 +39,39 @@ class TestNotEmpty(TestEmpty): ...@@ -39,39 +39,39 @@ class TestNotEmpty(TestEmpty):
class TestIsEmptyOpError(unittest.TestCase): class TestIsEmptyOpError(unittest.TestCase):
def test_errors(self): def test_errors(self):
with fluid.program_guard(fluid.Program(), fluid.Program()): paddle.enable_static()
with paddle.static.program_guard(paddle.static.Program(),
paddle.static.Program()):
input_data = np.random.random((3, 2)).astype("float64") input_data = np.random.random((3, 2)).astype("float64")
def test_Variable(): def test_Variable():
# the input type must be Variable # the input type must be Variable
fluid.layers.is_empty(x=input_data) paddle.is_empty(x=input_data)
self.assertRaises(TypeError, test_Variable) self.assertRaises(TypeError, test_Variable)
def test_cond_Variable():
# cond type must be Variable or None
x2 = fluid.layers.data(name="x2", shape=[3, 2], dtype="float32")
cond_data = np.random.random((3, 2)).astype("float32")
fluid.layers.is_empty(x=x2, cond=cond_data)
self.assertRaises(TypeError, test_cond_Variable)
def test_type(): def test_type():
# dtype must be float32, float64, int32, int64 # dtype must be float32, float64, int32, int64
x3 = fluid.layers.data( x3 = paddle.static.data(
name="x3", shape=[4, 32, 32], dtype="bool") name="x3", shape=[4, 32, 32], dtype="bool")
res = fluid.layers.is_empty(x=x3) res = paddle.is_empty(x=x3)
self.assertRaises(TypeError, test_type) self.assertRaises(TypeError, test_type)
def test_cond_type(): def test_name_type():
# cond dtype must be bool. # name type must be string.
x4 = fluid.layers.data(name="x4", shape=[3, 2], dtype="float32") x4 = paddle.static.data(
cond = fluid.layers.data( name="x4", shape=[3, 2], dtype="float32")
name="cond", shape=[1], dtype="float32") res = paddle.is_empty(x=x4, name=1)
fluid.layers.is_empty(x=x4, cond=cond)
self.assertRaises(TypeError, test_name_type)
self.assertRaises(TypeError, test_cond_type) class TestIsEmptyOpDygraph(unittest.TestCase):
def test_dygraph(self):
paddle.disable_static()
input = paddle.rand(shape=[4, 32, 32], dtype='float32')
res = paddle.is_empty(x=input)
if __name__ == "__main__": if __name__ == "__main__":
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册