test_cos_sim_op.py 4.0 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# 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 已提交
15
import unittest
16

X
Xinghai Sun 已提交
17
import numpy as np
18
from op_test import OpTest
19

X
Xinghai Sun 已提交
20

Q
qijun 已提交
21
class TestCosSimOp(OpTest):
X
Xinghai Sun 已提交
22
    def setUp(self):
Q
qijun 已提交
23
        self.op_type = "cos_sim"
X
Xinghai Sun 已提交
24
        self.inputs = {
25
            'X': np.random.random((6, 20)).astype("float32"),
26
            'Y': np.random.random((6, 20)).astype("float32"),
27 28 29
        }
        expect_x_norm = np.linalg.norm(self.inputs['X'], axis=1)
        expect_y_norm = np.linalg.norm(self.inputs['Y'], axis=1)
30 31 32 33 34
        expect_out = (
            (self.inputs['X'] * self.inputs['Y']).sum(axis=1)
            / expect_x_norm
            / expect_y_norm
        )
35 36 37
        self.outputs = {
            'XNorm': np.expand_dims(expect_x_norm, 1),
            'YNorm': np.expand_dims(expect_y_norm, 1),
38
            'Out': np.expand_dims(expect_out, 1),
X
Xinghai Sun 已提交
39 40
        }

Q
qijun 已提交
41 42
    def test_check_output(self):
        self.check_output()
X
Xinghai Sun 已提交
43

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

Q
qijun 已提交
47
    def test_check_grad_ingore_x(self):
48 49 50
        self.check_grad(
            ['Y'], 'Out', max_relative_error=0.06, no_grad_set=set("X")
        )
51

52
    def test_check_grad_ingore_y(self):
53 54 55
        self.check_grad(
            ['X'], 'Out', max_relative_error=0.06, no_grad_set=set('Y')
        )
56

X
Xinghai Sun 已提交
57

58
class TestCosSimOp2(TestCosSimOp):
59
    def setUp(self):
60
        self.op_type = "cos_sim"
61
        self.inputs = {
Z
zhupengyang 已提交
62
            'X': np.random.random((6, 100)).astype("float32"),
63
            'Y': np.random.random((1, 100)).astype("float32"),
64 65 66
        }
        expect_x_norm = np.linalg.norm(self.inputs['X'], axis=1)
        expect_y_norm = np.linalg.norm(self.inputs['Y'], axis=1)
67 68 69 70 71
        expect_out = (
            (self.inputs['X'] * self.inputs['Y']).sum(axis=1)
            / expect_x_norm
            / expect_y_norm
        )
72 73 74
        self.outputs = {
            'XNorm': np.expand_dims(expect_x_norm, 1),
            'YNorm': np.expand_dims(expect_y_norm, 1),
75
            'Out': np.expand_dims(expect_out, 1),
76 77 78
        }


79
class TestCosSimOp3(TestCosSimOp):
80
    def setUp(self):
81
        self.op_type = "cos_sim"
82
        self.inputs = {
Z
zhupengyang 已提交
83
            'X': np.random.random((6, 5, 4)).astype("float32"),
84
            'Y': np.random.random((6, 5, 4)).astype("float32"),
85 86 87
        }
        expect_x_norm = np.linalg.norm(self.inputs['X'], axis=(1, 2))
        expect_y_norm = np.linalg.norm(self.inputs['Y'], axis=(1, 2))
88 89 90 91 92
        expect_out = (
            (self.inputs['X'] * self.inputs['Y']).sum(axis=(1, 2))
            / expect_x_norm
            / expect_y_norm
        )
93 94 95
        self.outputs = {
            'XNorm': np.expand_dims(expect_x_norm, 1),
            'YNorm': np.expand_dims(expect_y_norm, 1),
96
            'Out': np.expand_dims(expect_out, 1),
97 98 99
        }


100
class TestCosSimOp4(TestCosSimOp):
101
    def setUp(self):
102
        self.op_type = "cos_sim"
103
        self.inputs = {
Z
zhupengyang 已提交
104
            'X': np.random.random((6, 5, 20)).astype("float32"),
105
            'Y': np.random.random((1, 5, 20)).astype("float32"),
106 107 108
        }
        expect_x_norm = np.linalg.norm(self.inputs['X'], axis=(1, 2))
        expect_y_norm = np.linalg.norm(self.inputs['Y'], axis=(1, 2))
109 110 111 112 113
        expect_out = (
            (self.inputs['X'] * self.inputs['Y']).sum(axis=(1, 2))
            / expect_x_norm
            / expect_y_norm
        )
114 115 116
        self.outputs = {
            'XNorm': np.expand_dims(expect_x_norm, 1),
            'YNorm': np.expand_dims(expect_y_norm, 1),
117
            'Out': np.expand_dims(expect_out, 1),
118 119 120
        }


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