test_cos_sim_op.py 3.3 KB
Newer Older
X
Xinghai Sun 已提交
1 2
import unittest
import numpy as np
Q
qijun 已提交
3
from op_test import OpTest
X
Xinghai Sun 已提交
4 5


Q
qijun 已提交
6
class TestCosSimOp(OpTest):
X
Xinghai Sun 已提交
7
    def setUp(self):
Q
qijun 已提交
8
        self.op_type = "cos_sim"
X
Xinghai Sun 已提交
9
        self.inputs = {
10 11
            'X': np.random.random((6, 5)).astype("float32"),
            'Y': np.random.random((6, 5)).astype("float32")
12 13 14 15 16 17 18 19 20
        }
        expect_x_norm = np.linalg.norm(self.inputs['X'], axis=1)
        expect_y_norm = np.linalg.norm(self.inputs['Y'], axis=1)
        expect_out = (self.inputs['X'] * self.inputs['Y']).sum(axis=1) / \
            expect_x_norm / expect_y_norm
        self.outputs = {
            'XNorm': np.expand_dims(expect_x_norm, 1),
            'YNorm': np.expand_dims(expect_y_norm, 1),
            'Out': np.expand_dims(expect_out, 1)
X
Xinghai Sun 已提交
21 22
        }

Q
qijun 已提交
23 24
    def test_check_output(self):
        self.check_output()
X
Xinghai Sun 已提交
25

Q
qijun 已提交
26
    def test_check_grad_normal(self):
27
        self.check_grad(['X', 'Y'], 'Out', max_relative_error=0.06)
X
Xinghai Sun 已提交
28

Q
qijun 已提交
29
    def test_check_grad_ingore_x(self):
30
        self.check_grad(
31
            ['Y'], 'Out', max_relative_error=0.06, no_grad_set=set("X"))
32

33
    def test_check_grad_ingore_y(self):
X
Xinghai Sun 已提交
34
        self.check_grad(
35
            ['X'], 'Out', max_relative_error=0.06, no_grad_set=set('Y'))
36

X
Xinghai Sun 已提交
37

38
class TestCosSimOp2(TestCosSimOp):
39
    def setUp(self):
40
        self.op_type = "cos_sim"
41
        self.inputs = {
42 43
            'X': np.random.random((6, 5)).astype("float32"),
            'Y': np.random.random((1, 5)).astype("float32")
44 45 46 47 48 49 50 51 52 53 54 55
        }
        expect_x_norm = np.linalg.norm(self.inputs['X'], axis=1)
        expect_y_norm = np.linalg.norm(self.inputs['Y'], axis=1)
        expect_out = (self.inputs['X'] * self.inputs['Y']).sum(axis=1) / \
            expect_x_norm / expect_y_norm
        self.outputs = {
            'XNorm': np.expand_dims(expect_x_norm, 1),
            'YNorm': np.expand_dims(expect_y_norm, 1),
            'Out': np.expand_dims(expect_out, 1)
        }


56
class TestCosSimOp3(TestCosSimOp):
57
    def setUp(self):
58
        self.op_type = "cos_sim"
59
        self.inputs = {
60 61
            'X': np.random.random((6, 5, 2)).astype("float32"),
            'Y': np.random.random((6, 5, 2)).astype("float32")
62 63 64 65 66 67 68 69 70 71 72 73
        }
        expect_x_norm = np.linalg.norm(self.inputs['X'], axis=(1, 2))
        expect_y_norm = np.linalg.norm(self.inputs['Y'], axis=(1, 2))
        expect_out = (self.inputs['X'] * self.inputs['Y']).sum(axis=(1, 2)) / \
            expect_x_norm / expect_y_norm
        self.outputs = {
            'XNorm': np.expand_dims(expect_x_norm, 1),
            'YNorm': np.expand_dims(expect_y_norm, 1),
            'Out': np.expand_dims(expect_out, 1)
        }


74
class TestCosSimOp4(TestCosSimOp):
75
    def setUp(self):
76
        self.op_type = "cos_sim"
77
        self.inputs = {
78 79
            'X': np.random.random((6, 5, 2)).astype("float32"),
            'Y': np.random.random((1, 5, 2)).astype("float32")
80 81 82 83 84 85 86 87 88 89 90 91
        }
        expect_x_norm = np.linalg.norm(self.inputs['X'], axis=(1, 2))
        expect_y_norm = np.linalg.norm(self.inputs['Y'], axis=(1, 2))
        expect_out = (self.inputs['X'] * self.inputs['Y']).sum(axis=(1, 2)) / \
            expect_x_norm / expect_y_norm
        self.outputs = {
            'XNorm': np.expand_dims(expect_x_norm, 1),
            'YNorm': np.expand_dims(expect_y_norm, 1),
            'Out': np.expand_dims(expect_out, 1)
        }


X
Xinghai Sun 已提交
92 93
if __name__ == '__main__':
    unittest.main()