test_smooth_l1_loss_op.py 3.4 KB
Newer Older
D
dzhwinter 已提交
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
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.

Y
yangyaming 已提交
15 16
import unittest
import numpy as np
17
from op_test import OpTest
Y
yangyaming 已提交
18 19 20 21 22 23 24 25 26 27


def smooth_l1_loss_forward(val, sigma2):
    abs_val = abs(val)
    if abs_val < 1.0 / sigma2:
        return 0.5 * val * val * sigma2
    else:
        return abs_val - 0.5 / sigma2


28
class TestSmoothL1LossOp1(OpTest):
Y
yangyaming 已提交
29
    def setUp(self):
30
        self.op_type = "smooth_l1_loss"
Y
yangyaming 已提交
31
        dims = (5, 10)
Y
yangyaming 已提交
32 33 34 35 36 37 38 39 40 41
        self.inputs = {
            'X': np.random.random(dims).astype("float32"),
            'Y': np.random.random(dims).astype("float32")
        }
        sigma = 3.0
        self.attrs = {'sigma': sigma}
        sigma2 = sigma * sigma
        diff = self.inputs['X'] - self.inputs['Y']
        loss = np.vectorize(smooth_l1_loss_forward)(diff, sigma2).sum(1)
        loss = loss.reshape((dims[0], 1))
Y
Yu Yang 已提交
42 43 44 45
        self.outputs = {
            'Diff': diff.astype('float32'),
            'Out': loss.astype('float32')
        }
46 47 48 49 50

    def test_check_output(self):
        self.check_output()

    def test_check_grad_normal(self):
Y
yangyaming 已提交
51
        self.check_grad(['X', 'Y'], 'Out', max_relative_error=0.02)
Y
yangyaming 已提交
52

53 54
    def test_check_grad_ingore_x(self):
        self.check_grad(
Y
yangyaming 已提交
55
            ['Y'], 'Out', max_relative_error=0.03, no_grad_set=set("X"))
56 57 58

    def test_check_grad_ingore_y(self):
        self.check_grad(
Y
yangyaming 已提交
59
            ['X'], 'Out', max_relative_error=0.03, no_grad_set=set('Y'))
Y
yangyaming 已提交
60 61


62
class TestSmoothL1LossOp2(OpTest):
Y
yangyaming 已提交
63
    def setUp(self):
64
        self.op_type = "smooth_l1_loss"
Y
yangyaming 已提交
65
        dims = (5, 10)
Y
yangyaming 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79
        self.inputs = {
            'X': np.random.random(dims).astype("float32"),
            'Y': np.random.random(dims).astype("float32"),
            'InsideWeight': np.random.random(dims).astype("float32"),
            'OutsideWeight': np.random.random(dims).astype("float32")
        }
        sigma = 3.0
        self.attrs = {'sigma': sigma}
        sigma2 = sigma * sigma
        diff = self.inputs['X'] - self.inputs['Y']
        diff = diff * self.inputs['InsideWeight']
        loss = np.vectorize(smooth_l1_loss_forward)(diff, sigma2)
        loss = loss * self.inputs['OutsideWeight']
        loss = loss.sum(1).reshape((dims[0], 1))
Y
Yu Yang 已提交
80 81 82 83
        self.outputs = {
            'Diff': diff.astype('float32'),
            'Out': loss.astype('float32')
        }
84 85 86 87 88

    def test_check_output(self):
        self.check_output()

    def test_check_grad_normal(self):
Y
yangyaming 已提交
89
        self.check_grad(['X', 'Y'], 'Out', max_relative_error=0.03)
90 91 92 93 94

    def test_check_grad_ingore_x(self):
        self.check_grad(
            ['Y'],
            'Out',
Y
yangyaming 已提交
95
            max_relative_error=0.03,
96 97 98
            no_grad_set=set(['X', 'InsideWeight', 'OutsideWeight']))

    def test_check_grad_ingore_y(self):
Y
yangyaming 已提交
99
        self.check_grad(
100 101
            ['X'],
            'Out',
Y
yangyaming 已提交
102
            max_relative_error=0.03,
103
            no_grad_set=set(['Y', 'InsideWeight', 'OutsideWeight']))
Y
yangyaming 已提交
104 105 106 107


if __name__ == '__main__':
    unittest.main()