test_mul_op.py 2.4 KB
Newer Older
D
dzhwinter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
Q
qijun 已提交
14 15
import unittest
import numpy as np
Q
qijun 已提交
16
from op_test import OpTest
Q
qijun 已提交
17 18


Q
qijun 已提交
19
class TestMulOp(OpTest):
Q
qijun 已提交
20
    def setUp(self):
Q
qijun 已提交
21
        self.op_type = "mul"
D
dangqingqing 已提交
22 23 24 25 26
        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 已提交
27

Q
qijun 已提交
28 29 30 31 32
    def test_check_output(self):
        self.check_output()

    def test_check_grad_normal(self):
        self.check_grad(['X', 'Y'], 'Out', max_relative_error=0.5)
Q
qijun 已提交
33

Q
qijun 已提交
34 35 36
    def test_check_grad_ingore_x(self):
        self.check_grad(
            ['Y'], 'Out', max_relative_error=0.5, no_grad_set=set("X"))
37

Q
qijun 已提交
38 39 40 41 42 43
    def test_check_grad_ingore_y(self):
        self.check_grad(
            ['X'], 'Out', max_relative_error=0.5, no_grad_set=set('Y'))


class TestMulOp2(OpTest):
44
    def setUp(self):
Q
qijun 已提交
45
        self.op_type = "mul"
46 47 48 49
        self.inputs = {
            'X': np.random.random((15, 4, 12, 10)).astype("float32"),
            'Y': np.random.random((4, 30, 8, 2, 9)).astype("float32")
        }
F
fengjiayi 已提交
50
        self.attrs = {'x_num_col_dims': 2, 'y_num_col_dims': 2}
Y
Yu Yang 已提交
51 52 53 54
        result = np.dot(self.inputs['X'].reshape(15 * 4, 12 * 10),
                        self.inputs['Y'].reshape(4 * 30, 8 * 2 * 9))
        result = result.reshape(15, 4, 8, 2, 9)
        self.outputs = {'Out': result}
55

Q
qijun 已提交
56 57
    def test_check_output(self):
        self.check_output()
58

Q
qijun 已提交
59 60
    def test_check_grad_normal(self):
        self.check_grad(['X', 'Y'], 'Out', max_relative_error=0.5)
61

Q
qijun 已提交
62
    def test_check_grad_ingore_x(self):
63
        self.check_grad(
Q
qijun 已提交
64
            ['Y'], 'Out', max_relative_error=0.5, no_grad_set=set('X'))
65

Q
qijun 已提交
66
    def test_check_grad_ignore_y(self):
67
        self.check_grad(
Q
qijun 已提交
68
            ['X'], 'Out', max_relative_error=0.5, no_grad_set=set('Y'))
69 70


Q
qijun 已提交
71
if __name__ == "__main__":
Q
qijun 已提交
72
    unittest.main()