From 08c8f0c534c736120192fec3b4458b4f872b4ba4 Mon Sep 17 00:00:00 2001 From: wawltor <980627148@qq.com> Date: Wed, 9 Oct 2019 14:13:44 +0800 Subject: [PATCH] Fix api, add input type and dtype check for sign_op (#20138) * test=develop Add input type and dtype check for sign_op. * test=develop Fix the api text format in sign op. * test=develop Fix the api examples in sign op add update the api.spec. --- paddle/fluid/API.spec | 2 +- python/paddle/fluid/layers/nn.py | 14 ++++++++++++-- .../paddle/fluid/tests/unittests/test_sign_op.py | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/paddle/fluid/API.spec b/paddle/fluid/API.spec index b72c1eb997e..37153f71741 100755 --- a/paddle/fluid/API.spec +++ b/paddle/fluid/API.spec @@ -302,7 +302,7 @@ paddle.fluid.layers.pixel_shuffle (ArgSpec(args=['x', 'upscale_factor'], varargs paddle.fluid.layers.fsp_matrix (ArgSpec(args=['x', 'y'], varargs=None, keywords=None, defaults=None), ('document', 'd803767ef4fb885013a28c98634e0bc4')) paddle.fluid.layers.continuous_value_model (ArgSpec(args=['input', 'cvm', 'use_cvm'], varargs=None, keywords=None, defaults=(True,)), ('document', 'c03490ffaa1b78258747157c313db4cd')) paddle.fluid.layers.where (ArgSpec(args=['condition'], varargs=None, keywords=None, defaults=None), ('document', 'b1e1487760295e1ff55307b880a99e18')) -paddle.fluid.layers.sign (ArgSpec(args=['x'], varargs=None, keywords=None, defaults=None), ('document', 'fa2f457a81714430c5677c2d68744728')) +paddle.fluid.layers.sign (ArgSpec(args=['x'], varargs=None, keywords=None, defaults=None), ('document', 'b56afe9ae3fc553c95d907fd7ef6c314')) paddle.fluid.layers.deformable_conv (ArgSpec(args=['input', 'offset', 'mask', 'num_filters', 'filter_size', 'stride', 'padding', 'dilation', 'groups', 'deformable_groups', 'im2col_step', 'param_attr', 'bias_attr', 'modulated', 'name'], varargs=None, keywords=None, defaults=(1, 0, 1, None, None, None, None, None, True, None)), ('document', '9b9c9d1282f994ccd4538201e0b6856f')) paddle.fluid.layers.unfold (ArgSpec(args=['x', 'kernel_sizes', 'strides', 'paddings', 'dilations', 'name'], varargs=None, keywords=None, defaults=(1, 0, 1, None)), ('document', '3f884662ad443d9ecc2b3734b4f61ad6')) paddle.fluid.layers.deformable_roi_pooling (ArgSpec(args=['input', 'rois', 'trans', 'no_trans', 'spatial_scale', 'group_size', 'pooled_height', 'pooled_width', 'part_size', 'sample_per_part', 'trans_std', 'position_sensitive', 'name'], varargs=None, keywords=None, defaults=(False, 1.0, [1, 1], 1, 1, None, 1, 0.1, False, None)), ('document', '47c5d1c890b36fa00ff3285c9398f613')) diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index bb12a20a911..627e2cac82e 100755 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -14881,14 +14881,24 @@ def sign(x): import numpy as np # [1, 0, -1] - data = fluid.layers.sign(np.array([3, 0, -2], dtype='int32')) + data = fluid.layers.sign(np.array([3, 0, -2], dtype='float32')) """ helper = LayerHelper("sign", **locals()) if not isinstance(x, Variable): - x = assign(x) + if isinstance(x, np.ndarray): + x = assign(x) + else: + raise TypeError( + "The type of 'x' in sign_op must be Variable or numpy.ndarray, but received %s." + % (type(x))) + + if convert_dtype(x.dtype) not in ['float32', 'float64']: + raise TypeError( + "The data type of 'x' in sign_op must be float32 or float64, but received %s." + % (convert_dtype(x.dtype))) out = helper.create_variable_for_type_inference(dtype=x.dtype) diff --git a/python/paddle/fluid/tests/unittests/test_sign_op.py b/python/paddle/fluid/tests/unittests/test_sign_op.py index 85a9d9cae47..00ac43b9ba5 100644 --- a/python/paddle/fluid/tests/unittests/test_sign_op.py +++ b/python/paddle/fluid/tests/unittests/test_sign_op.py @@ -17,6 +17,8 @@ from __future__ import print_function import unittest import numpy as np from op_test import OpTest +import paddle.fluid as fluid +from paddle.fluid import Program, program_guard class TestSignOp(OpTest): @@ -34,5 +36,17 @@ class TestSignOp(OpTest): self.check_grad(['X'], 'Out') +class TestSignOpError(OpTest): + def test_errors(self): + with program_guard(Program(), Program()): + # The input type of sign_op must be Variable or numpy.ndarray. + input1 = 12 + self.assertRaises(TypeError, fluid.layers.sign, input1) + # The input dtype of sign_op must be float32, float64. + input2 = fluid.layers.data( + name='input2', shape=[12, 10], dtype="int32") + self.assertRaises(TypeError, fluid.layers.sign, input2) + + if __name__ == "__main__": unittest.main() -- GitLab