test_cos_sim_op.py 3.9 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.

15 16
from __future__ import print_function

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


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

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

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

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

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

X
Xinghai Sun 已提交
53

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


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


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