diff --git a/python/paddle/fluid/tests/unittests/test_var_base.py b/python/paddle/fluid/tests/unittests/test_var_base.py index 76c871f37216b8c65014a665f547643b7f755b96..77432a59de273c570e87dda388adf8e11475eff7 100644 --- a/python/paddle/fluid/tests/unittests/test_var_base.py +++ b/python/paddle/fluid/tests/unittests/test_var_base.py @@ -631,6 +631,18 @@ class TestVarBase(unittest.TestCase): self.assertEqual(a_str, expected) paddle.enable_static() + def test_tensor_str_shape_with_zero(self): + paddle.disable_static(paddle.CPUPlace()) + x = paddle.ones((10, 10)) + y = paddle.fluid.layers.where(x == 0) + a_str = str(y) + + expected = '''Tensor(shape=[0, 2], dtype=int64, place=CPUPlace, stop_gradient=True, + [])''' + + self.assertEqual(a_str, expected) + paddle.enable_static() + def test_print_tensor_dtype(self): paddle.disable_static(paddle.CPUPlace()) a = paddle.rand([1]) diff --git a/python/paddle/tensor/to_string.py b/python/paddle/tensor/to_string.py index 778a391df605ecf57f86ceac17a521ad50a4d84d..e5148d039c9273acd01eea4224634189d386a44e 100644 --- a/python/paddle/tensor/to_string.py +++ b/python/paddle/tensor/to_string.py @@ -93,6 +93,10 @@ def set_printoptions(precision=None, def _to_sumary(var): edgeitems = DEFAULT_PRINT_OPTIONS.edgeitems + # Handle tensor of shape contains 0, like [0, 2], [3, 0, 3] + if np.prod(var.shape) == 0: + return np.array([]) + if len(var.shape) == 0: return var elif len(var.shape) == 1: