未验证 提交 3b61d066 编写于 作者: L Leo Chen 提交者: GitHub

fix tensor to_string when shape contains zero (#32501)

上级 4db2cc90
......@@ -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])
......
......@@ -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:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册