test_prelu_op.py 1.0 KB
Newer Older
Z
zchen0211 已提交
1 2 3 4 5
import unittest
import numpy as np
from op_test import OpTest


Z
zchen0211 已提交
6
class PReluTest(OpTest):
Z
zchen0211 已提交
7 8
    def setUp(self):
        self.op_type = "prelu"
Z
zchen0211 已提交
9
        x_np = np.random.normal(size=(10, 10)).astype("float32")
Y
Yu Yang 已提交
10 11 12 13 14 15 16 17

        for pos, val in np.ndenumerate(x_np):
            # Since zero point in prelu is not differentiable, avoid randomize
            # zero.
            while abs(val) < 1e-3:
                x_np[pos] = np.random.normal()
                val = x_np[pos]

Z
zchen0211 已提交
18 19
        x_np_sign = np.sign(x_np)
        x_np = x_np_sign * np.maximum(x_np, .005)
Z
zchen0211 已提交
20 21
        alpha_np = np.array([.1])
        self.inputs = {'X': x_np, 'Alpha': alpha_np}
Z
zchen0211 已提交
22
        out_np = np.maximum(self.inputs['X'], 0.)
Z
zchen0211 已提交
23 24
        out_np = out_np + np.minimum(self.inputs['X'],
                                     0.) * self.inputs['Alpha']
Z
zchen0211 已提交
25 26
        assert out_np is not self.inputs['X']
        self.outputs = {'Out': out_np}
Z
zchen0211 已提交
27

28
    def test_check_output(self):
Z
zchen0211 已提交
29 30
        self.check_output()

31
    def test_check_grad(self):
Z
zchen0211 已提交
32 33 34 35 36
        self.check_grad(['X'], 'Out')


if __name__ == "__main__":
    unittest.main()