test_auc_single_pred_op.py 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# 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
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# 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.

import unittest
16

17
import numpy as np
W
wanghuancoder 已提交
18
from eager_op_test import OpTest
19

20
import paddle
21 22 23 24 25 26 27 28 29


class TestAucSinglePredOp(OpTest):
    def setUp(self):
        self.op_type = "auc"
        pred = np.random.random((128, 2)).astype("float32")
        pred0 = pred[:, 0].reshape(128, 1)
        labels = np.random.randint(0, 2, (128, 1)).astype("int64")
        num_thresholds = 200
30
        slide_steps = 1
31

32
        stat_pos = np.zeros(
33 34
            (1 + slide_steps) * (num_thresholds + 1) + 1,
        ).astype("int64")
35
        stat_neg = np.zeros(
36 37
            (1 + slide_steps) * (num_thresholds + 1) + 1,
        ).astype("int64")
38 39 40 41 42

        self.inputs = {
            'Predict': pred0,
            'Label': labels,
            "StatPos": stat_pos,
43
            "StatNeg": stat_neg,
44 45 46 47
        }
        self.attrs = {
            'curve': 'ROC',
            'num_thresholds': num_thresholds,
48
            "slide_steps": slide_steps,
49 50
        }

51
        python_auc = paddle.metric.Auc(
52 53
            name="auc", curve='ROC', num_thresholds=num_thresholds
        )
54 55 56 57
        for i in range(128):
            pred[i][1] = pred[i][0]
        python_auc.update(pred, labels)

58
        pos = python_auc._stat_pos.tolist() * 2
59
        pos.append(1)
60
        neg = python_auc._stat_neg.tolist() * 2
61
        neg.append(1)
62
        self.outputs = {
63
            'AUC': np.array(python_auc.accumulate()),
64
            'StatPosOut': np.array(pos),
65
            'StatNegOut': np.array(neg),
66 67 68
        }

    def test_check_output(self):
W
wanghuancoder 已提交
69
        self.check_output(check_dygraph=False)
70 71


H
hutuxian 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
class TestAucGlobalSinglePredOp(OpTest):
    def setUp(self):
        self.op_type = "auc"
        pred = np.random.random((128, 2)).astype("float32")
        pred0 = pred[:, 0].reshape(128, 1)
        labels = np.random.randint(0, 2, (128, 1)).astype("int64")
        num_thresholds = 200
        slide_steps = 0

        stat_pos = np.zeros((1, (num_thresholds + 1))).astype("int64")
        stat_neg = np.zeros((1, (num_thresholds + 1))).astype("int64")

        self.inputs = {
            'Predict': pred0,
            'Label': labels,
            "StatPos": stat_pos,
88
            "StatNeg": stat_neg,
H
hutuxian 已提交
89 90 91 92
        }
        self.attrs = {
            'curve': 'ROC',
            'num_thresholds': num_thresholds,
93
            "slide_steps": slide_steps,
H
hutuxian 已提交
94 95
        }

96
        python_auc = paddle.metric.Auc(
97 98
            name="auc", curve='ROC', num_thresholds=num_thresholds
        )
H
hutuxian 已提交
99 100 101 102 103 104 105
        for i in range(128):
            pred[i][1] = pred[i][0]
        python_auc.update(pred, labels)

        pos = python_auc._stat_pos
        neg = python_auc._stat_neg
        self.outputs = {
106
            'AUC': np.array(python_auc.accumulate()),
H
hutuxian 已提交
107
            'StatPosOut': np.array(pos),
108
            'StatNegOut': np.array(neg),
H
hutuxian 已提交
109 110 111
        }

    def test_check_output(self):
W
wanghuancoder 已提交
112
        self.check_output(check_dygraph=False)
H
hutuxian 已提交
113 114


115 116
if __name__ == "__main__":
    unittest.main()