test_fc_op.py 1.8 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):
8 9
        x0 = np.random.random((16, 32)).astype("float32")
        w0 = np.random.random((32, 10)).astype("float32")
10

11
        mul_out0 = np.dot(x0, w0)
12
        identity_out = mul_out0
13 14

        self.op_type = "fc"
15 16
        self.inputs = {"X": [("X0", x0)], "W": [("W0", w0)]}
        self.outputs = {"MulOut": [("MulOut0", mul_out0)], "Out": identity_out}
17 18 19 20 21

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
22
        self.check_grad(["X0", "W0"], "Out", max_relative_error=0.01)
23 24 25 26


class TestFCOp2(OpTest):
    def setUp(self):
27
        x0 = np.random.random((16, 4, 8)).astype("float32")
28
        x1 = np.random.random((4, 4, 32)).astype("float32")
29
        w0 = np.random.random((32, 10)).astype("float32")
30
        w1 = np.random.random((32, 10)).astype("float32")
31
        b = np.random.random(10).astype("float32")
32 33

        mul_out0 = np.dot(x0.reshape(16, 4 * 8), w0)
34
        mul_out1 = np.dot(x1.reshape(4 * 4, 32), w1)
35 36 37 38 39
        sum_out = mul_out0 + mul_out1
        add_out = np.add(sum_out, b)
        sigmoid_out = 1 / (1 + np.exp(-add_out))

        self.op_type = "fc"
40
        self.inputs = {
41 42
            "X": [("X0", x0), ("X1", x1)],
            "W": [("W0", w0), ("W1", w1)],
43 44
            "B": b
        }
45
        self.attrs = {"xNumColDims": [1, 2], "activation": "sigmoid"}
46
        self.outputs = {
47 48 49
            "MulOut": [("MulOut0", mul_out0), ("MulOut1", mul_out1)],
            "SumOut": sum_out,
            "AddOut": add_out,
50
            "Out": sigmoid_out
51
        }
52

L
Liu Yiqun 已提交
53
    def test_check_output(self):
54
        self.check_output()
55

56 57
    def test_check_grad(self):
        self.check_grad(
58
            ["X0", "X1", "W0", "W1", "B"], "Out", max_relative_error=0.01)
59 60 61 62


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