test_cos_sim_op.py 3.8 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.
X
Xinghai Sun 已提交
14 15
import unittest
import numpy as np
Q
qijun 已提交
16
from op_test import OpTest
X
Xinghai Sun 已提交
17 18


Q
qijun 已提交
19
class TestCosSimOp(OpTest):
X
Xinghai Sun 已提交
20
    def setUp(self):
Q
qijun 已提交
21
        self.op_type = "cos_sim"
X
Xinghai Sun 已提交
22
        self.inputs = {
23 24
            'X': np.random.random((6, 5)).astype("float32"),
            'Y': np.random.random((6, 5)).astype("float32")
25 26 27 28 29 30 31 32 33
        }
        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 已提交
34 35
        }

Q
qijun 已提交
36 37
    def test_check_output(self):
        self.check_output()
X
Xinghai Sun 已提交
38

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

Q
qijun 已提交
42
    def test_check_grad_ingore_x(self):
43
        self.check_grad(
44
            ['Y'], 'Out', max_relative_error=0.06, no_grad_set=set("X"))
45

46
    def test_check_grad_ingore_y(self):
X
Xinghai Sun 已提交
47
        self.check_grad(
48
            ['X'], 'Out', max_relative_error=0.06, no_grad_set=set('Y'))
49

X
Xinghai Sun 已提交
50

51
class TestCosSimOp2(TestCosSimOp):
52
    def setUp(self):
53
        self.op_type = "cos_sim"
54
        self.inputs = {
55 56
            'X': np.random.random((6, 5)).astype("float32"),
            'Y': np.random.random((1, 5)).astype("float32")
57 58 59 60 61 62 63 64 65 66 67 68
        }
        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)
        }


69
class TestCosSimOp3(TestCosSimOp):
70
    def setUp(self):
71
        self.op_type = "cos_sim"
72
        self.inputs = {
73 74
            'X': np.random.random((6, 5, 2)).astype("float32"),
            'Y': np.random.random((6, 5, 2)).astype("float32")
75 76 77 78 79 80 81 82 83 84 85 86
        }
        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)
        }


87
class TestCosSimOp4(TestCosSimOp):
88
    def setUp(self):
89
        self.op_type = "cos_sim"
90
        self.inputs = {
91 92
            'X': np.random.random((6, 5, 2)).astype("float32"),
            'Y': np.random.random((1, 5, 2)).astype("float32")
93 94 95 96 97 98 99 100 101 102 103 104
        }
        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 已提交
105 106
if __name__ == '__main__':
    unittest.main()