未验证 提交 1f830691 编写于 作者: Z Zhou Wei 提交者: GitHub

API(to_variable/save_dygraph) error message enhancement(#23683)

* API(to_variable/save_dygraph) error message enhancement
上级 1ac9db43
......@@ -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))
......@@ -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"
......
......@@ -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
......
......@@ -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():
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册