diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index c4cfe141c9dfd9406e2260a8f18633d77de0833b..b872fc1ebc6ef84a92c52cb1a3992907c44470f7 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 6dca5dd2bd4a4d6860edd13db8895350646726e4..70972f20877838bba3e6f7643d98768a1452de3e 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 6cd02dad577b681b8c452bdb9574df60ffb4f82e..2f6c87aefaa9aab11d7291195c787296bad49bbd 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 a07694e7bd7d7d271ab5ca494514d814e05e8bfd..ff6cbdde066bb431153852a9bed7ff9b57c0c02f 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):