test_rank_loss_op.py 3.8 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

Y
Yibing Liu 已提交
17 18
import unittest
import numpy as np
19
from op_test import OpTest
20 21
import paddle.fluid as fluid
from paddle.fluid import Program, program_guard
Y
Yibing Liu 已提交
22 23


Y
Yibing Liu 已提交
24
class TestRankLossOp(OpTest):
Y
Yibing Liu 已提交
25 26
    def setUp(self):
        self.op_type = "rank_loss"
27
        shape = (100, 1)
Y
Yibing Liu 已提交
28
        # labels_{i} = {0, 1.0} or {0, 0.5, 1.0}
29 30 31 32
        label_shape, left_shape, right_shape = self.set_shape()
        label = np.random.randint(0, 2, size=shape).astype("float32")
        left = np.random.random(shape).astype("float32")
        right = np.random.random(shape).astype("float32")
Y
Yibing Liu 已提交
33
        loss = np.log(1.0 + np.exp(left - right)) - label * (left - right)
34 35 36 37 38 39 40 41 42
        loss = np.reshape(loss, label_shape)
        self.inputs = {
            'Label': label.reshape(label_shape),
            'Left': left.reshape(left_shape),
            'Right': right.reshape(right_shape)
        }
        self.outputs = {'Out': loss.reshape(label_shape)}

    def set_shape(self):
43
        batch_size = 100
44
        return (batch_size, 1), (batch_size, 1), (batch_size, 1)
Y
Yibing Liu 已提交
45 46 47 48 49

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
Y
Yibing Liu 已提交
50 51 52 53 54 55 56
        self.check_grad(["Left", "Right"], "Out")

    def test_check_grad_ignore_left(self):
        self.check_grad(["Right"], "Out", no_grad_set=set('Left'))

    def test_check_grad_ignore_right(self):
        self.check_grad(["Left"], "Out", no_grad_set=set('Right'))
Y
Yibing Liu 已提交
57 58


59 60
class TestRankLossOp1(TestRankLossOp):
    def set_shape(self):
61
        batch_size = 100
62 63 64 65 66
        return (batch_size), (batch_size, 1), (batch_size, 1)


class TestRankLossOp2(TestRankLossOp):
    def set_shape(self):
67
        batch_size = 100
68 69 70 71 72
        return (batch_size, 1), (batch_size), (batch_size, 1)


class TestRankLossOp3(TestRankLossOp):
    def set_shape(self):
73
        batch_size = 100
74 75 76 77 78
        return (batch_size, 1), (batch_size, 1), (batch_size)


class TestRankLossOp4(TestRankLossOp):
    def set_shape(self):
79
        batch_size = 100
80 81 82 83 84
        return (batch_size), (batch_size), (batch_size, 1)


class TestRankLossOp5(TestRankLossOp):
    def set_shape(self):
85
        batch_size = 100
86 87 88
        return (batch_size), (batch_size), (batch_size)


89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
class TestRankLossOpError(unittest.TestCase):
    def test_errors(self):
        with program_guard(Program(), Program()):
            label = fluid.data(name="label", shape=[16, 1], dtype="float32")
            left = fluid.data(name="left", shape=[16, 1], dtype="float32")
            right = fluid.data(name="right", shape=[16, 1], dtype="float32")

            def test_label_Variable():
                label_data = np.random.rand(16, 1).astype("float32")
                out = fluid.layers.rank_loss(label_data, left, right)

            self.assertRaises(TypeError, test_label_Variable)

            def test_left_Variable():
                left_data = np.random.rand(16, 1).astype("float32")
                out = fluid.layers.rank_loss(label, left_data, right)

            self.assertRaises(TypeError, test_left_Variable)

            def test_right_Variable():
                right_data = np.random.rand(16, 1).astype("float32")
                out = fluid.layers.rank_loss(label, left, right_data)

            self.assertRaises(TypeError, test_right_Variable)


Y
Yibing Liu 已提交
115 116
if __name__ == '__main__':
    unittest.main()