From 1f830691172df3730fb782134fd50d58e4cd63e9 Mon Sep 17 00:00:00 2001 From: Zhou Wei <52485244+zhouwei25@users.noreply.github.com> Date: Fri, 10 Apr 2020 13:43:27 +0800 Subject: [PATCH] API(to_variable/save_dygraph) error message enhancement(#23683) * API(to_variable/save_dygraph) error message enhancement --- python/paddle/fluid/dygraph/base.py | 3 ++- python/paddle/fluid/dygraph/checkpoint.py | 2 +- python/paddle/fluid/layer_helper_base.py | 4 ++-- python/paddle/fluid/tests/unittests/test_var_base.py | 7 +++++++ 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/python/paddle/fluid/dygraph/base.py b/python/paddle/fluid/dygraph/base.py index 1015cc5d9f..8c5c5bd791 100644 --- a/python/paddle/fluid/dygraph/base.py +++ b/python/paddle/fluid/dygraph/base.py @@ -540,4 +540,5 @@ def to_variable(value, name=None, zero_copy=None): return value else: raise TypeError( - "to_variable only accepts 'ndarray' and 'Variable' as value's input") + "The type of input value is invalid, expected type is 'ndarray' or 'Variable', but received %s" + % type(value)) diff --git a/python/paddle/fluid/dygraph/checkpoint.py b/python/paddle/fluid/dygraph/checkpoint.py index 870404e64b..3a6e46bc64 100644 --- a/python/paddle/fluid/dygraph/checkpoint.py +++ b/python/paddle/fluid/dygraph/checkpoint.py @@ -63,7 +63,7 @@ def save_dygraph(state_dict, model_path): ''' base_name = os.path.basename(model_path) - assert base_name != "", "model_path MUST be format of dirname/filename [dirname\\filename in Window], Now filename is empty str" + assert base_name != "", "The input model_path MUST be format of dirname/filename [dirname\\filename in Windows system], but received filename is empty string." suffix = ".pdparams" assert len(state_dict) > 0, "state_dict is empty, no need to save" diff --git a/python/paddle/fluid/layer_helper_base.py b/python/paddle/fluid/layer_helper_base.py index 3f6f59f0e0..dd83f2f04a 100644 --- a/python/paddle/fluid/layer_helper_base.py +++ b/python/paddle/fluid/layer_helper_base.py @@ -81,8 +81,8 @@ class LayerHelperBase(object): return value else: raise TypeError( - "to_variable only accepts 'ndarray' or 'Variable' or 'VarBase' as value's input" - ) + "The type of input value is invalid, expected type is 'ndarray' or 'Variable', but received %s" + % type(value)) def _create_weight_normalize(self, attr, shape, dtype): from .layers import elementwise_mul, elementwise_div, reshape diff --git a/python/paddle/fluid/tests/unittests/test_var_base.py b/python/paddle/fluid/tests/unittests/test_var_base.py index 07b2a75912..4dd41a1eb7 100644 --- a/python/paddle/fluid/tests/unittests/test_var_base.py +++ b/python/paddle/fluid/tests/unittests/test_var_base.py @@ -39,6 +39,13 @@ class TestVarBase(unittest.TestCase): self.assertEqual(var.shape, self.shape) self.assertEqual(var.dtype, core.VarDesc.VarType.FP32) self.assertEqual(var.type, core.VarDesc.VarType.LOD_TENSOR) + # The type of input must be 'ndarray' or 'Variable', it will raise TypeError + with self.assertRaises(TypeError): + var = fluid.dygraph.to_variable("test", name="abc") + # test to_variable of LayerObjectHelper(LayerHelperBase) + with self.assertRaises(TypeError): + linear = fluid.dygraph.Linear(32, 64) + var = linear._helper.to_variable("test", name="abc") def test_write_property(self): with fluid.dygraph.guard(): -- GitLab