auc_op.cc 3.2 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
T
typhoonzero 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

15
#include "paddle/fluid/framework/infershape_utils.h"
16
#include "paddle/fluid/framework/op_registry.h"
17 18
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/multiary.h"
T
typhoonzero 已提交
19 20 21 22

namespace paddle {
namespace operators {

T
update  
typhoonzero 已提交
23
class AucOp : public framework::OperatorWithKernel {
T
typhoonzero 已提交
24 25 26
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

武毅 已提交
27
 protected:
28
  framework::OpKernelType GetExpectedKernelType(
武毅 已提交
29
      const framework::ExecutionContext &ctx) const override {
30 31
    return framework::OpKernelType(
        OperatorWithKernel::IndicateVarDataType(ctx, "Predict"),
32
        ctx.device_context());
T
typhoonzero 已提交
33 34 35 36 37
  }
};

class AucOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
38
  void Make() override {
Q
Qiao Longfei 已提交
39 40 41
    AddInput("Predict",
             "A floating point 2D tensor with shape [batch_size, 2], values "
             "are in the range [0, 1]."
武毅 已提交
42
             "Typically, this tensor indicates the probability of each label");
T
auc_op  
typhoonzero 已提交
43
    AddInput("Label",
Q
Qiao Longfei 已提交
44 45
             "A 2D int tensor indicating the label of the training data. "
             "shape: [batch_size, 1]");
T
tangwei12 已提交
46

T
auc_op  
typhoonzero 已提交
47
    // TODO(typhoonzero): support weight input
T
tangwei12 已提交
48 49 50
    AddInput("StatPos", "Statistic value when label = 1");
    AddInput("StatNeg", "Statistic value when label = 0");

T
auc_op  
typhoonzero 已提交
51
    AddOutput("AUC",
T
typhoonzero 已提交
52
              "A scalar representing the "
53
              "current area-under-the-curve.");
T
tangwei12 已提交
54

T
tangwei12 已提交
55 56
    AddOutput("StatPosOut", "Statistic value when label = 1");
    AddOutput("StatNegOut", "Statistic value when label = 0");
T
typhoonzero 已提交
57

T
typhoonzero 已提交
58
    AddAttr<std::string>("curve", "Curve type, can be 'ROC' or 'PR'.")
T
typhoonzero 已提交
59
        .SetDefault("ROC");
T
tangwei12 已提交
60

T
tangwei12 已提交
61 62 63
    AddAttr<int>(
        "num_thresholds",
        "The number of thresholds to use when discretizing the roc curve.")
T
tangwei12 已提交
64
        .SetDefault((2 << 12) - 1);
T
tangwei12 已提交
65 66
    AddAttr<int>("slide_steps", "Use slide steps to calc batch auc.")
        .SetDefault(1);
67 68
    AddComment(R"DOC(
Area Under The Curve (AUC) Operator.
武毅 已提交
69

70 71
This implementation computes the AUC according to forward output and label.
It is used very widely in binary classification evaluation. As a note:
武毅 已提交
72
If input label contains values other than 0 and 1, it will be cast
73
to bool. You can find the relevant definitions here:
武毅 已提交
74 75
https://en.wikipedia.org/wiki/Receiver_operating_characteristic#Area_under_the_curve

76 77 78
There are two types of possible curves:
1. ROC: Receiver operating characteristic
2. PR: Precision Recall
武毅 已提交
79
)DOC");
T
typhoonzero 已提交
80 81 82 83 84 85 86
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
87 88 89 90
DECLARE_INFER_SHAPE_FUNCTOR(auc, AucInferShapeFunctor,
                            PD_INFER_META(phi::AucInferMeta));
REGISTER_OP_WITHOUT_GRADIENT(auc, ops::AucOp, ops::AucOpMaker,
                             AucInferShapeFunctor);