test_elementwise_mul_op.py 2.7 KB
Newer Older
1 2 3 4 5
import unittest
import numpy as np
from op_test import OpTest


G
gongweibao 已提交
6
class ElementwiseMulOp(OpTest):
7 8 9
    def setUp(self):
        self.op_type = "elementwise_mul"
        self.inputs = {
10 11
            'X': np.random.uniform(0.1, 1, [13, 17]).astype("float64"),
            'Y': np.random.uniform(0.1, 1, [13, 17]).astype("float64")
12 13 14 15 16 17 18
        }
        self.outputs = {'Out': np.multiply(self.inputs['X'], self.inputs['Y'])}

    def test_check_output(self):
        self.check_output()

    def test_check_grad_normal(self):
19
        self.check_grad(['X', 'Y'], 'Out')
20 21

    def test_check_grad_ingore_x(self):
22
        self.check_grad(['Y'], 'Out', no_grad_set=set("X"))
23 24

    def test_check_grad_ingore_y(self):
25
        self.check_grad(['X'], 'Out', no_grad_set=set('Y'))
26 27


G
gongweibao 已提交
28
class TestElementwiseMulOp_Vector(ElementwiseMulOp):
29 30 31
    def setUp(self):
        self.op_type = "elementwise_mul"
        self.inputs = {
32 33
            'X': np.random.random((32, )).astype("float64"),
            'Y': np.random.random((32, )).astype("float64")
34 35 36 37
        }
        self.outputs = {'Out': np.multiply(self.inputs['X'], self.inputs['Y'])}


G
gongweibao 已提交
38
class TestElementwiseMulOp_broadcast_0(ElementwiseMulOp):
39 40 41
    def setUp(self):
        self.op_type = "elementwise_mul"
        self.inputs = {
42 43
            'X': np.random.rand(2, 3, 4).astype(np.float64),
            'Y': np.random.rand(2).astype(np.float64)
44 45 46 47 48 49 50 51
        }

        self.attrs = {'axis': 0}
        self.outputs = {
            'Out': self.inputs['X'] * self.inputs['Y'].reshape(2, 1, 1)
        }


G
gongweibao 已提交
52
class TestElementwiseMulOp_broadcast_1(ElementwiseMulOp):
53 54 55
    def setUp(self):
        self.op_type = "elementwise_mul"
        self.inputs = {
56 57
            'X': np.random.rand(2, 3, 4).astype(np.float64),
            'Y': np.random.rand(3).astype(np.float64)
58 59 60 61 62 63 64 65
        }

        self.attrs = {'axis': 1}
        self.outputs = {
            'Out': self.inputs['X'] * self.inputs['Y'].reshape(1, 3, 1)
        }


G
gongweibao 已提交
66
class TestElementwiseMulOp_broadcast_2(ElementwiseMulOp):
67 68 69
    def setUp(self):
        self.op_type = "elementwise_mul"
        self.inputs = {
70 71
            'X': np.random.rand(2, 3, 4).astype(np.float64),
            'Y': np.random.rand(4).astype(np.float64)
72 73 74 75 76 77 78
        }

        self.outputs = {
            'Out': self.inputs['X'] * self.inputs['Y'].reshape(1, 1, 4)
        }


G
gongweibao 已提交
79
class TestElementwiseMulOp_broadcast_3(ElementwiseMulOp):
80 81 82
    def setUp(self):
        self.op_type = "elementwise_mul"
        self.inputs = {
83 84
            'X': np.random.rand(2, 3, 4, 5).astype(np.float64),
            'Y': np.random.rand(3, 4).astype(np.float64)
85 86 87 88 89 90 91 92 93 94
        }

        self.attrs = {'axis': 1}
        self.outputs = {
            'Out': self.inputs['X'] * self.inputs['Y'].reshape(1, 3, 4, 1)
        }


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