test_mul_op.py 3.0 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
5
from paddle.v2.framework.op import Operator
Q
qijun 已提交
6 7 8 9 10 11 12


class TestMulOp(unittest.TestCase):
    __metaclass__ = OpTestMeta

    def setUp(self):
        self.type = "mul"
D
dangqingqing 已提交
13 14 15 16 17
        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 已提交
18 19


20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
class TestMulOp2(unittest.TestCase):
    __metaclass__ = OpTestMeta

    def setUp(self):
        self.type = "mul"
        self.inputs = {
            'X': np.random.random((15, 4, 12, 10)).astype("float32"),
            'Y': np.random.random((4, 30, 8, 2, 9)).astype("float32")
        }
        self.attrs = {'x_num_row_dims': 2, 'y_num_row_dims': 3}
        self.outputs = {
            'Out': np.dot(self.inputs['X'].reshape(15 * 4, 12 * 10),
                          self.inputs['Y'].reshape(4 * 30, 8 * 2 * 9))
        }


36
class TestMulGradOp(GradientChecker):
37 38 39
    def setUp(self):
        self.op = create_op("mul")
        self.inputs = {
D
dongzhihong 已提交
40 41 42
            'X': np.random.random((32, 84)).astype("float32"),
            'Y': np.random.random((84, 100)).astype("float32")
        }
43

44 45 46
    def test_cpu_gpu_compare(self):
        self.compare_grad(self.op, self.inputs)

47
    def test_normal(self):
D
dongzhihong 已提交
48 49
        # mul op will enlarge the relative error
        self.check_grad(
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
            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 已提交
67 68


69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
class TestMulGradTest2(GradientChecker):
    def setUp(self):
        self.op = Operator(
            "mul", X="X", Y="Y", Out="Out", x_num_row_dims=2, y_num_row_dims=3)
        self.inputs = {
            "X": np.random.random((15, 4, 12, 10)).astype("float32"),
            "Y": np.random.random((4, 30, 8, 2, 9)).astype("float32")
        }

    def test_cpu_gpu_compare(self):
        self.compare_grad(self.op, self.inputs)

    def test_normal(self):
        self.check_grad(
            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 已提交
102 103
# TODO(dzh,qijun) : mulgrad test case need transpose feature of blas library

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