test_squared_l2_distance_op.py 2.6 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
import unittest
import numpy as np
Q
qijun 已提交
17
from op_test import OpTest
18 19


Q
qijun 已提交
20
class TestSquaredL2DistanceOp_f0(OpTest):
21
    def setUp(self):
Q
qijun 已提交
22
        self.op_type = "squared_l2_distance"
23
        self.inputs = {
Q
qijun 已提交
24 25
            'X': np.random.uniform(0.1, 0.6, (2, 3)).astype("float32"),
            'Y': np.random.uniform(0.1, 0.6, (2, 3)).astype("float32")
26
        }
Y
yangyaming 已提交
27 28
        sub_res = self.inputs['X'] - self.inputs['Y']
        output = sub_res * sub_res
29
        self.outputs = {
Y
yangyaming 已提交
30 31 32 33
            'sub_result': sub_res,
            'Out': np.expand_dims(output.sum(1), 1)
        }

Q
qijun 已提交
34 35 36 37 38
    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(['X', 'Y'], 'Out')
Y
yangyaming 已提交
39 40


Q
qijun 已提交
41
class TestSquaredL2DistanceOp_f1(OpTest):
Y
yangyaming 已提交
42
    def setUp(self):
Q
qijun 已提交
43
        self.op_type = "squared_l2_distance"
Y
yangyaming 已提交
44
        self.inputs = {
Q
qijun 已提交
45 46
            'X': np.random.uniform(0.1, 0.6, (2, 3)).astype("float32"),
            'Y': np.random.uniform(0.1, 0.6, (1, 3)).astype("float32")
Y
yangyaming 已提交
47 48 49 50 51 52 53 54
        }
        sub_res = self.inputs['X'] - self.inputs['Y']
        output = sub_res * sub_res
        self.outputs = {
            'sub_result': sub_res,
            'Out': np.expand_dims(output.sum(1), 1)
        }

Q
qijun 已提交
55 56
    def test_check_output(self):
        self.check_output()
Y
yangyaming 已提交
57

Q
qijun 已提交
58 59
    def test_check_grad(self):
        self.check_grad(['X', 'Y'], 'Out')
Y
yangyaming 已提交
60

Q
qijun 已提交
61 62

class TestSquaredL2DistanceOp_f2(OpTest):
Y
yangyaming 已提交
63
    def setUp(self):
Q
qijun 已提交
64
        self.op_type = "squared_l2_distance"
Y
yangyaming 已提交
65
        self.inputs = {
Q
qijun 已提交
66 67
            'X': np.random.uniform(0.1, 0.6, (2, 3, 4)).astype("float32"),
            'Y': np.random.uniform(0.1, 0.6, (1, 3, 4)).astype("float32")
Y
yangyaming 已提交
68 69
        }
        sub_res = self.inputs['X'] - self.inputs['Y']
Q
qijun 已提交
70
        sub_res = sub_res.reshape((2, 3 * 4))
Y
yangyaming 已提交
71 72 73
        output = sub_res * sub_res
        self.outputs = {
            'sub_result': sub_res,
74 75 76
            'Out': np.expand_dims(output.sum(1), 1)
        }

Q
qijun 已提交
77 78
    def test_check_output(self):
        self.check_output()
79

Q
qijun 已提交
80 81
    def test_check_grad(self):
        self.check_grad(['X', 'Y'], 'Out')
82 83


Q
qijun 已提交
84
if __name__ == "__main__":
85
    unittest.main()