diff --git a/python/paddle/fluid/tests/unittests/test_var_base.py b/python/paddle/fluid/tests/unittests/test_var_base.py index 41aef68db624d5a7743aca36fb8b890bd212ee3c..511813fc1cd0f5f20e735edd8b6514ae4a77b2a8 100644 --- a/python/paddle/fluid/tests/unittests/test_var_base.py +++ b/python/paddle/fluid/tests/unittests/test_var_base.py @@ -466,6 +466,17 @@ class TestVarBase(unittest.TestCase): self.assertEqual(a_str, expected) paddle.enable_static() + def test_tensor_str_scaler(self): + paddle.disable_static(paddle.CPUPlace()) + a = paddle.to_tensor(np.array(False)) + a_str = str(a) + + expected = '''Tensor(shape=[], dtype=bool, place=CPUPlace, stop_gradient=True, + False)''' + + self.assertEqual(a_str, expected) + paddle.enable_static() + class TestVarBaseSetitem(unittest.TestCase): def setUp(self): diff --git a/python/paddle/tensor/to_string.py b/python/paddle/tensor/to_string.py index bd956b923a6638a8ef7b97cb86a8dd5ee0425f8e..778a391df605ecf57f86ceac17a521ad50a4d84d 100644 --- a/python/paddle/tensor/to_string.py +++ b/python/paddle/tensor/to_string.py @@ -153,7 +153,7 @@ def _format_tensor(var, sumary, indent=0, max_width=0, signed=False): if len(var.shape) == 0: # currently, shape = [], i.e., scaler tensor is not supported. # If it is supported, it should be formatted like this. - return _format_item(var.item(0), max_width, signed) + return _format_item(var, max_width, signed) elif len(var.shape) == 1: if sumary and var.shape[0] > 2 * edgeitems: items = [