|
|
|
@ -23,21 +23,18 @@ from paddle.fluid import Program, program_guard
|
|
|
|
|
from op_test import OpTest, skip_check_grad_ci
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestPReluAPIError(unittest.TestCase):
|
|
|
|
|
class TestPReluOpError(unittest.TestCase):
|
|
|
|
|
def test_errors(self):
|
|
|
|
|
with fluid.program_guard(fluid.Program(), fluid.Program()):
|
|
|
|
|
layer = fluid.PRelu(
|
|
|
|
|
mode='all',
|
|
|
|
|
param_attr=fluid.ParamAttr(
|
|
|
|
|
initializer=fluid.initializer.Constant(1.0)))
|
|
|
|
|
# the input must be Variable.
|
|
|
|
|
x0 = fluid.create_lod_tensor(
|
|
|
|
|
np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace())
|
|
|
|
|
self.assertRaises(TypeError, layer, x0)
|
|
|
|
|
# the input dtype must be float32
|
|
|
|
|
data_t = fluid.data(
|
|
|
|
|
name="input", shape=[5, 200, 100, 100], dtype="float64")
|
|
|
|
|
self.assertRaises(TypeError, layer, data_t)
|
|
|
|
|
with program_guard(Program()):
|
|
|
|
|
# The input type must be Variable.
|
|
|
|
|
self.assertRaises(TypeError, fluid.layers.prelu, 0.1, 'all')
|
|
|
|
|
# The input dtype must be float16, float32, float64.
|
|
|
|
|
x_int32 = fluid.data(name='x_int32', shape=[12, 10], dtype='int32')
|
|
|
|
|
self.assertRaises(TypeError, fluid.layers.prelu, x_int32, 'all')
|
|
|
|
|
# support the input dtype is float32
|
|
|
|
|
x_fp16 = fluid.layers.data(
|
|
|
|
|
name='x_fp16', shape=[12, 10], dtype='float32')
|
|
|
|
|
fluid.layers.prelu(x_fp16, 'all')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PReluTest(OpTest):
|
|
|
|
@ -79,39 +76,55 @@ class PReluTest(OpTest):
|
|
|
|
|
self.check_grad(['X', 'Alpha'], 'Out')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# TODO(minqiyang): Resume these test cases after fixing Python3 CI job issues
|
|
|
|
|
if six.PY2:
|
|
|
|
|
|
|
|
|
|
@skip_check_grad_ci(
|
|
|
|
|
reason="[skip shape check] Input(Alpha) must be 1-D and only has one data in 'all' mode"
|
|
|
|
|
)
|
|
|
|
|
class TestModeAll(PReluTest):
|
|
|
|
|
def init_input_shape(self):
|
|
|
|
|
self.x_shape = (2, 3, 4, 5)
|
|
|
|
|
|
|
|
|
|
def init_attr(self):
|
|
|
|
|
self.attrs = {'mode': "all"}
|
|
|
|
|
@skip_check_grad_ci(
|
|
|
|
|
reason="[skip shape check] Input(Alpha) must be 1-D and only has one data in 'all' mode"
|
|
|
|
|
)
|
|
|
|
|
class TestModeAll(PReluTest):
|
|
|
|
|
def init_input_shape(self):
|
|
|
|
|
self.x_shape = (2, 3, 4, 5)
|
|
|
|
|
|
|
|
|
|
class TestModeElt(PReluTest):
|
|
|
|
|
def init_input_shape(self):
|
|
|
|
|
self.x_shape = (3, 2, 5, 10)
|
|
|
|
|
def init_attr(self):
|
|
|
|
|
self.attrs = {'mode': "all"}
|
|
|
|
|
|
|
|
|
|
def init_attr(self):
|
|
|
|
|
self.attrs = {'mode': "element"}
|
|
|
|
|
|
|
|
|
|
class TestModeElt(PReluTest):
|
|
|
|
|
def init_input_shape(self):
|
|
|
|
|
self.x_shape = (3, 2, 5, 10)
|
|
|
|
|
|
|
|
|
|
class TestPReluOpError(unittest.TestCase):
|
|
|
|
|
def test_errors(self):
|
|
|
|
|
with program_guard(Program()):
|
|
|
|
|
# The input type must be Variable.
|
|
|
|
|
self.assertRaises(TypeError, fluid.layers.prelu, 1, 'all')
|
|
|
|
|
# The input dtype must be float16, float32, float64.
|
|
|
|
|
x_int32 = fluid.data(name='x_int32', shape=[12, 10], dtype='int32')
|
|
|
|
|
self.assertRaises(TypeError, fluid.layers.prelu, x_int32, 'all')
|
|
|
|
|
# support the input dtype is float32
|
|
|
|
|
x_fp16 = fluid.layers.data(
|
|
|
|
|
name='x_fp16', shape=[12, 10], dtype='float32')
|
|
|
|
|
fluid.layers.prelu(x_fp16, 'all')
|
|
|
|
|
def init_attr(self):
|
|
|
|
|
self.attrs = {'mode': "element"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def prelu_t(x, mode, param_attr=None, name=None):
|
|
|
|
|
helper = fluid.layer_helper.LayerHelper('prelu', **locals())
|
|
|
|
|
alpha_shape = [1, x.shape[1], 1, 1]
|
|
|
|
|
dtype = helper.input_dtype(input_param_name='x')
|
|
|
|
|
alpha = helper.create_parameter(
|
|
|
|
|
attr=helper.param_attr,
|
|
|
|
|
shape=alpha_shape,
|
|
|
|
|
dtype='float32',
|
|
|
|
|
is_bias=False,
|
|
|
|
|
default_initializer=fluid.initializer.ConstantInitializer(0.25))
|
|
|
|
|
out = helper.create_variable_for_type_inference(dtype)
|
|
|
|
|
helper.append_op(
|
|
|
|
|
type="prelu",
|
|
|
|
|
inputs={"X": x,
|
|
|
|
|
'Alpha': alpha},
|
|
|
|
|
attrs={"mode": mode},
|
|
|
|
|
outputs={"Out": out})
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# error message test if mode is not one of 'all', 'channel', 'element'
|
|
|
|
|
class TestModeError(unittest.TestCase):
|
|
|
|
|
def test_mode_error(self):
|
|
|
|
|
main_program = Program()
|
|
|
|
|
with fluid.program_guard(main_program, Program()):
|
|
|
|
|
x = fluid.data(name='x', shape=[2, 3, 4, 5])
|
|
|
|
|
try:
|
|
|
|
|
y = prelu_t(x, 'any')
|
|
|
|
|
except Exception as e:
|
|
|
|
|
assert (e.args[0].find('InvalidArgumentError') != -1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|