test_fc_op.py 2.2 KB
Newer Older
1 2
import unittest
import numpy as np
L
Liu Yiqun 已提交
3
from op_test import OpTest
4 5


6
class TestFCOp1(OpTest):
7
    def setUp(self):
L
Liu Yiqun 已提交
8
        self.op_type = "fc"
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
        x1 = np.random.random((16, 32)).astype("float32")
        w1 = np.random.random((32, 10)).astype("float32")
        b = np.random.random(10).astype("float32")
        self.inputs = {"X": {"X1": x1}, "W": {"W1": w1}, "b": b}
        mul_out1 = np.dot(x1, w1)
        sum_out = mul_out1
        add_out = sum_out + b
        identity_out = add_out
        self.outputs = {
            "mul_out": {
                "mul_out1": mul_out1,
            },
            "sum_out": sum_out,
            "add_out": add_out,
            "Y": identity_out
        }

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(["X1", "W1", "b"], "Y", max_relative_error=0.05)


class TestFCOp2(OpTest):
    def setUp(self):
        self.op_type = "fc"
        x1 = np.random.random((16, 32)).astype("float32")
        x2 = np.random.random((16, 32)).astype("float32")
        w1 = np.random.random((32, 10)).astype("float32")
        w2 = np.random.random((32, 10)).astype("float32")
        b = np.random.random(10).astype("float32")
41
        self.inputs = {
L
Liu Yiqun 已提交
42
            "X": {
43 44
                "X1": x1,
                "X2": x2
L
Liu Yiqun 已提交
45 46
            },
            "W": {
47 48
                "W1": w1,
                "W2": w2
L
Liu Yiqun 已提交
49 50
            },
            "b": b
51
        }
L
Liu Yiqun 已提交
52
        #self.attrs = {"activation": "sigmoid"}
53 54 55 56
        mul_out1 = np.dot(x1, w1)
        mul_out2 = np.dot(x2, w2)
        sum_out = mul_out1 + mul_out2
        add_out = np.add(sum_out, b)
L
Liu Yiqun 已提交
57 58
        #sigmoid_out = 1 / (1 + np.exp(-add_out))
        sigmoid_out = add_out
59
        self.outputs = {
60 61 62 63 64
            "mul_out": {
                "mul_out0": mul_out1,
                "mul_out1": mul_out2
            },
            "sum_out": sum_out,
65
            "add_out": add_out,
L
Liu Yiqun 已提交
66
            "Y": sigmoid_out
67
        }
68

L
Liu Yiqun 已提交
69
    def test_check_output(self):
70
        self.check_output()
71

72 73 74
    def test_check_grad(self):
        self.check_grad(
            ["X1", "X2", "W1", "W2", "b"], "Y", max_relative_error=0.05)
75 76 77 78


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