From 896bda0c98eac96eaf383cf01daeadbb505ff626 Mon Sep 17 00:00:00 2001 From: HappyAngel Date: Sun, 26 Apr 2020 02:17:04 -0500 Subject: [PATCH] python API(get_tensor_from_selected_rows and tensor_array_to_tensor)error message enhance, test=develop (#23636) --- python/paddle/fluid/layers/nn.py | 5 +++++ python/paddle/fluid/layers/tensor.py | 5 +++++ .../test_get_tensor_from_selected_rows_op.py | 21 +++++++++++++++++++ .../unittests/test_tensor_array_to_tensor.py | 19 +++++++++++++++++ 4 files changed, 50 insertions(+) diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index c4cfe141c9d..b872fc1ebc6 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -12586,6 +12586,11 @@ def get_tensor_from_selected_rows(x, name=None): out = fluid.layers.get_tensor_from_selected_rows(input) """ + check_type(x, 'x', Variable, 'get_tensor_from_selected_rows') + if x.type != core.VarDesc.VarType.SELECTED_ROWS: + raise TypeError( + "The type of 'x' in get_tensor_from_selected_rows must be SELECTED_ROWS." + ) helper = LayerHelper('get_tensor_from_selected_rows', **locals()) out = helper.create_variable_for_type_inference(dtype=x.dtype) helper.append_op( diff --git a/python/paddle/fluid/layers/tensor.py b/python/paddle/fluid/layers/tensor.py index 6dca5dd2bd4..70972f20877 100644 --- a/python/paddle/fluid/layers/tensor.py +++ b/python/paddle/fluid/layers/tensor.py @@ -440,6 +440,11 @@ def tensor_array_to_tensor(input, axis=1, name=None, use_stack=False): numpy.array(list(map(lambda x: int(x.shape[axis]), input)))) return res, sizes + check_type(input, 'input', (list, Variable), 'tensor_array_to_tensor') + if isinstance(input, list): + for i, input_x in enumerate(input): + check_type(input_x, 'input[' + str(i) + ']', Variable, + 'tensor_array_to_tensor') helper = LayerHelper('tensor_array_to_tensor', **locals()) out = helper.create_variable_for_type_inference(dtype=helper.input_dtype()) out_index = helper.create_variable_for_type_inference(dtype="int32") diff --git a/python/paddle/fluid/tests/unittests/test_get_tensor_from_selected_rows_op.py b/python/paddle/fluid/tests/unittests/test_get_tensor_from_selected_rows_op.py index 6cd02dad577..2f6c87aefaa 100644 --- a/python/paddle/fluid/tests/unittests/test_get_tensor_from_selected_rows_op.py +++ b/python/paddle/fluid/tests/unittests/test_get_tensor_from_selected_rows_op.py @@ -17,7 +17,28 @@ from __future__ import print_function import unittest import paddle.fluid.core as core import numpy as np +import paddle.fluid as fluid from paddle.fluid.op import Operator +from paddle.fluid import Program, program_guard + + +class TestGetTensorFromSelectedRowsError(unittest.TestCase): + """get_tensor_from_selected_rows error message enhance""" + + def test_errors(self): + with program_guard(Program()): + x_var = fluid.data('X', [2, 3]) + x_data = np.random.random((2, 4)).astype("float32") + + def test_Variable(): + fluid.layers.get_tensor_from_selected_rows(x=x_data) + + self.assertRaises(TypeError, test_Variable) + + def test_SELECTED_ROWS(): + fluid.layers.get_tensor_from_selected_rows(x=x_var) + + self.assertRaises(TypeError, test_SELECTED_ROWS) class TestGetTensorFromSelectedRows(unittest.TestCase): diff --git a/python/paddle/fluid/tests/unittests/test_tensor_array_to_tensor.py b/python/paddle/fluid/tests/unittests/test_tensor_array_to_tensor.py index a07694e7bd7..ff6cbdde066 100644 --- a/python/paddle/fluid/tests/unittests/test_tensor_array_to_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_tensor_array_to_tensor.py @@ -20,6 +20,25 @@ import paddle.fluid as fluid import paddle.fluid.core as core from paddle.fluid.op import Operator from paddle.fluid.executor import Executor +from paddle.fluid import Program, program_guard + + +class TestTensorArrayToTensorError(unittest.TestCase): + """Tensor_array_to_tensor error message enhance""" + + def test_errors(self): + with program_guard(Program()): + input_data = numpy.random.random((2, 4)).astype("float32") + + def test_Variable(): + fluid.layers.tensor_array_to_tensor(input=input_data) + + self.assertRaises(TypeError, test_Variable) + + def test_list_Variable(): + fluid.layers.tensor_array_to_tensor(input=[input_data]) + + self.assertRaises(TypeError, test_list_Variable) class TestLoDTensorArrayConcat(unittest.TestCase): -- GitLab