test_mul_op.py 1.5 KB
Newer Older
Q
qijun 已提交
1 2
import unittest
import numpy as np
D
dongzhihong 已提交
3 4
from gradient_checker import GradientChecker, create_op
from op_test_util import OpTestMeta
Q
qijun 已提交
5 6 7 8 9 10 11


class TestMulOp(unittest.TestCase):
    __metaclass__ = OpTestMeta

    def setUp(self):
        self.type = "mul"
D
dangqingqing 已提交
12 13 14 15 16
        self.inputs = {
            'X': np.random.random((32, 84)).astype("float32"),
            'Y': np.random.random((84, 100)).astype("float32")
        }
        self.outputs = {'Out': np.dot(self.inputs['X'], self.inputs['Y'])}
Q
qijun 已提交
17 18


19
class TestMulGradOp(GradientChecker):
20 21 22
    def setUp(self):
        self.op = create_op("mul")
        self.inputs = {
D
dongzhihong 已提交
23 24 25
            'X': np.random.random((32, 84)).astype("float32"),
            'Y': np.random.random((84, 100)).astype("float32")
        }
26

27 28 29
    def test_cpu_gpu_compare(self):
        self.compare_grad(self.op, self.inputs)

30
    def test_normal(self):
D
dongzhihong 已提交
31 32
        # mul op will enlarge the relative error
        self.check_grad(
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
            self.op, self.inputs, ["X", "Y"], "Out", max_relative_error=0.5)

    def test_ignore_x(self):
        self.check_grad(
            self.op,
            self.inputs, ["Y"],
            "Out",
            max_relative_error=0.5,
            no_grad_set={"X"})

    def test_ignore_y(self):
        self.check_grad(
            self.op,
            self.inputs, ["X"],
            "Out",
            max_relative_error=0.5,
            no_grad_set={"Y"})
D
dongzhihong 已提交
50 51


D
dongzhihong 已提交
52 53
# TODO(dzh,qijun) : mulgrad test case need transpose feature of blas library

Q
qijun 已提交
54 55
if __name__ == '__main__':
    unittest.main()