metric_op.py 7.1 KB
Newer Older
F
fengjiayi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#   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.
"""
All layers just related to metric.
"""

18 19
from __future__ import print_function

D
dzhwinter 已提交
20
import warnings
F
fengjiayi 已提交
21 22 23 24
from ..layer_helper import LayerHelper
from ..initializer import Normal, Constant
from ..framework import Variable
from ..param_attr import ParamAttr
25
from . import nn
F
fengjiayi 已提交
26

D
dzhwinter 已提交
27
__all__ = ['accuracy', 'auc']
F
fengjiayi 已提交
28 29 30 31


def accuracy(input, label, k=1, correct=None, total=None):
    """
D
dzhwinter 已提交
32 33 34
    accuracy layer.
    Refer to the https://en.wikipedia.org/wiki/Precision_and_recall

F
fengjiayi 已提交
35
    This function computes the accuracy using the input and label.
D
dzhwinter 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    If the correct label occurs in top k predictions, then correct will increment by one.
    Note: the dtype of accuracy is determined by input. the input and label dtype can be different.

    Args:
        input(Variable): The input of accuracy layer, which is the predictions of network.
          Carry LoD information is supported.
        label(Variable): The label of dataset.
        k(int): The top k predictions for each class will be checked.
        correct(Variable): The correct predictions count.
        total(Variable): The total entries count.

    Returns:
        Variable: The correct rate.

    Examples:
        .. code-block:: python

J
JesseyXujin 已提交
53
           import paddle.fluid as fluid
D
dzhwinter 已提交
54
           data = fluid.layers.data(name="data", shape=[-1, 32, 32], dtype="float32")
J
JesseyXujin 已提交
55
           label = fluid.layers.data(name="label", shape=[-1,1], dtype="int32")
D
dzhwinter 已提交
56
           predict = fluid.layers.fc(input=data, size=10)
J
JesseyXujin 已提交
57
           accuracy_out = fluid.layers.accuracy(input=predict, label=label, k=5)
D
dzhwinter 已提交
58

F
fengjiayi 已提交
59 60
    """
    helper = LayerHelper("accuracy", **locals())
Q
qingqing01 已提交
61
    topk_out, topk_indices = nn.topk(input, k=k)
X
Xin Pan 已提交
62
    acc_out = helper.create_variable_for_type_inference(dtype="float32")
F
fengjiayi 已提交
63
    if correct is None:
X
Xin Pan 已提交
64
        correct = helper.create_variable_for_type_inference(dtype="int64")
F
fengjiayi 已提交
65
    if total is None:
X
Xin Pan 已提交
66
        total = helper.create_variable_for_type_inference(dtype="int64")
F
fengjiayi 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79
    helper.append_op(
        type="accuracy",
        inputs={
            "Out": [topk_out],
            "Indices": [topk_indices],
            "Label": [label]
        },
        outputs={
            "Accuracy": [acc_out],
            "Correct": [correct],
            "Total": [total],
        })
    return acc_out
D
dzhwinter 已提交
80 81


T
tangwei12 已提交
82 83 84 85 86 87
def auc(input,
        label,
        curve='ROC',
        num_thresholds=2**12 - 1,
        topk=1,
        slide_steps=1):
Y
Yibing Liu 已提交
88
    """
Y
Yibing Liu 已提交
89
    **Area Under the Curve (AUC) Layer**
Y
Yibing Liu 已提交
90 91

    This implementation computes the AUC according to forward output and label.
92
    It is used very widely in binary classification evaluation.
Y
Yibing Liu 已提交
93

94
    Note: If input label contains values other than 0 and 1, it will be cast
Y
Yibing Liu 已提交
95 96
    to `bool`. Find the relevant definitions `here <https://en.wikipedia.org\
    /wiki/Receiver_operating_characteristic#Area_under_the_curve>`_.
Y
Yibing Liu 已提交
97 98

    There are two types of possible curves:
Y
Yibing Liu 已提交
99 100 101

        1. ROC: Receiver operating characteristic;
        2. PR: Precision Recall
Y
Yibing Liu 已提交
102 103

    Args:
104 105 106
        input(Variable): A floating-point 2D Variable, values are in the range
                         [0, 1]. Each row is sorted in descending order. This
                         input should be the output of topk. Typically, this
Y
Yibing Liu 已提交
107
                         Variable indicates the probability of each label.
108
        label(Variable): A 2D int Variable indicating the label of the training
Y
Yibing Liu 已提交
109 110
                         data. The height is batch size and width is always 1.
        curve(str): Curve type, can be 'ROC' or 'PR'. Default 'ROC'.
111
        num_thresholds(int): The number of thresholds to use when discretizing
Y
Yibing Liu 已提交
112
                             the roc curve. Default 200.
W
Wu Yi 已提交
113
        topk(int): only topk number of prediction output will be used for auc.
T
tangwei12 已提交
114 115
        slide_steps: when calc batch auc, we can not only use step currently but the previous steps can be used. slide_steps=1 means use the current step, slide_steps=3 means use current step and the previous second steps, slide_steps=0 use all of the steps.

Y
Yibing Liu 已提交
116 117

    Returns:
118 119 120
        Variable: A tuple representing the current AUC.
        The return tuple is auc_out, batch_auc_out, [
        batch_stat_pos, batch_stat_neg, stat_pos, stat_neg ]
Y
Yibing Liu 已提交
121 122 123

    Examples:
        .. code-block:: python
124

J
JesseyXujin 已提交
125 126 127 128 129
            import paddle.fluid as fluid
            data = fluid.layers.data(name="data", shape=[32, 32], dtype="float32")
            label = fluid.layers.data(name="label", shape=[1], dtype="int32")
            predict = fluid.layers.fc(input=data, size=2)
            auc_out = fluid.layers.auc(input=predict, label=label)
Y
Yibing Liu 已提交
130
    """
D
dzhwinter 已提交
131
    helper = LayerHelper("auc", **locals())
X
Xin Pan 已提交
132 133
    auc_out = helper.create_variable_for_type_inference(dtype="float64")
    batch_auc_out = helper.create_variable_for_type_inference(dtype="float64")
W
Wu Yi 已提交
134
    # make tp, tn, fp, fn persistable, so that can accumulate all batches.
T
tangwei12 已提交
135 136 137 138 139 140 141 142 143 144 145 146

    # for batch auc
    batch_stat_pos = helper.create_global_variable(
        persistable=True,
        dtype='int64',
        shape=[slide_steps, num_thresholds + 1])
    batch_stat_neg = helper.create_global_variable(
        persistable=True,
        dtype='int64',
        shape=[slide_steps, num_thresholds + 1])

    # for global auc
T
tangwei12 已提交
147
    stat_pos = helper.create_global_variable(
T
tangwei12 已提交
148
        persistable=True, dtype='int64', shape=[1, num_thresholds + 1])
T
tangwei12 已提交
149
    stat_neg = helper.create_global_variable(
T
tangwei12 已提交
150
        persistable=True, dtype='int64', shape=[1, num_thresholds + 1])
T
tangwei12 已提交
151

T
tangwei12 已提交
152
    for var in [batch_stat_pos, batch_stat_neg, stat_pos, stat_neg]:
W
Wu Yi 已提交
153 154 155 156
        helper.set_variable_initializer(
            var, Constant(
                value=0.0, force_cpu=True))

T
tangwei12 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    # Batch AUC
    helper.append_op(
        type="auc",
        inputs={
            "Predict": [input],
            "Label": [label],
            "StatPos": [batch_stat_pos],
            "StatNeg": [batch_stat_neg]
        },
        attrs={
            "curve": curve,
            "num_thresholds": num_thresholds,
            "slide_steps": slide_steps
        },
        outputs={
            "AUC": [batch_auc_out],
            "StatPosOut": [batch_stat_pos],
            "StatNegOut": [batch_stat_neg]
        })
    # Global AUC
D
dzhwinter 已提交
177
    helper.append_op(
178
        type="auc",
D
dzhwinter 已提交
179
        inputs={
Q
Qiao Longfei 已提交
180
            "Predict": [input],
W
Wu Yi 已提交
181
            "Label": [label],
T
tangwei12 已提交
182 183
            "StatPos": [stat_pos],
            "StatNeg": [stat_neg]
D
dzhwinter 已提交
184
        },
T
tangwei12 已提交
185 186 187 188 189
        attrs={
            "curve": curve,
            "num_thresholds": num_thresholds,
            "slide_steps": 0
        },
W
Wu Yi 已提交
190 191
        outputs={
            "AUC": [auc_out],
T
tangwei12 已提交
192 193
            "StatPosOut": [stat_pos],
            "StatNegOut": [stat_neg]
W
Wu Yi 已提交
194
        })
T
tangwei12 已提交
195 196 197
    return auc_out, batch_auc_out, [
        batch_stat_pos, batch_stat_neg, stat_pos, stat_neg
    ]