auc_op.cc 4.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. */

Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/auc_op.h"
16
#include <string>
T
typhoonzero 已提交
17 18 19 20

namespace paddle {
namespace operators {

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

 protected:
武毅 已提交
26
  void InferShape(framework::InferShapeContext *ctx) const override {
27
    PADDLE_ENFORCE(ctx->HasInput("Out"), "Input of Out should not be null.");
武毅 已提交
28
    PADDLE_ENFORCE(ctx->HasInput("Indices"),
29
                   "Input of Indices should not be null.");
T
typhoonzero 已提交
30
    PADDLE_ENFORCE(ctx->HasInput("Label"),
31
                   "Input of Label should not be null.");
武毅 已提交
32 33
    auto inference_height = ctx->GetInputDim("Out")[0];
    auto label_height = ctx->GetInputDim("Label")[0];
T
typhoonzero 已提交
34

武毅 已提交
35 36
    PADDLE_ENFORCE_EQ(inference_height, label_height,
                      "Out and Label should have same height.");
T
typhoonzero 已提交
37

W
Wu Yi 已提交
38 39
    int num_thres = ctx->Attrs().Get<int>("num_thresholds");

T
typhoonzero 已提交
40
    ctx->SetOutputDim("AUC", {1});
W
Wu Yi 已提交
41 42 43 44 45
    ctx->SetOutputDim("TPOut", {num_thres});
    ctx->SetOutputDim("TNOut", {num_thres});
    ctx->SetOutputDim("FPOut", {num_thres});
    ctx->SetOutputDim("FNOut", {num_thres});

武毅 已提交
46 47 48 49
    ctx->ShareLoD("Out", /*->*/ "AUC");
  }

 protected:
50
  framework::OpKernelType GetExpectedKernelType(
武毅 已提交
51
      const framework::ExecutionContext &ctx) const override {
Y
Yu Yang 已提交
52 53 54
    return framework::OpKernelType(
        framework::ToDataType(ctx.Input<Tensor>("Out")->type()),
        ctx.device_context());
T
typhoonzero 已提交
55 56 57 58 59
  }
};

class AucOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
60
  void Make() override {
武毅 已提交
61 62
    AddInput("Out",
             "A floating point 2D tensor, values are in the range [0, 1]."
63
             "Each row is sorted in descending order. This input should be the"
武毅 已提交
64 65 66 67
             "output of topk."
             "Typically, this tensor indicates the probability of each label");
    AddInput("Indices",
             "An int 2D tensor, indicating the indices of original"
68 69
             "tensor before sorting. Typically, this tensor indicates which "
             "label the probability stands for.");
T
auc_op  
typhoonzero 已提交
70
    AddInput("Label",
武毅 已提交
71 72
             "A 2D int tensor indicating the label of the training data."
             "The height is batch size and width is always 1.");
W
Wu Yi 已提交
73 74 75 76
    AddInput("TP", "True-Positive value.");
    AddInput("FP", "False-Positive value.");
    AddInput("TN", "True-Negative value.");
    AddInput("FN", "False-Negative value.");
T
auc_op  
typhoonzero 已提交
77 78
    // TODO(typhoonzero): support weight input
    AddOutput("AUC",
T
typhoonzero 已提交
79
              "A scalar representing the "
80
              "current area-under-the-curve.");
W
Wu Yi 已提交
81 82 83 84
    AddOutput("TPOut", "True-Positive value.");
    AddOutput("FPOut", "False-Positive value.");
    AddOutput("TNOut", "True-Negative value.");
    AddOutput("FNOut", "False-Negative value.");
T
typhoonzero 已提交
85

T
typhoonzero 已提交
86
    AddAttr<std::string>("curve", "Curve type, can be 'ROC' or 'PR'.")
T
typhoonzero 已提交
87 88 89 90 91 92
        .SetDefault("ROC");
    AddAttr<int>("num_thresholds",
                 "The number of thresholds to use when discretizing the"
                 " roc curve.")
        .SetDefault(200);

93 94
    AddComment(R"DOC(
Area Under The Curve (AUC) Operator.
武毅 已提交
95

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

102 103 104
There are two types of possible curves:
1. ROC: Receiver operating characteristic
2. PR: Precision Recall
武毅 已提交
105
)DOC");
T
typhoonzero 已提交
106 107 108 109 110 111 112
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
T
update  
typhoonzero 已提交
113
REGISTER_OP_WITHOUT_GRADIENT(auc, ops::AucOp, ops::AucOpMaker);
T
typhoonzero 已提交
114
REGISTER_OP_CPU_KERNEL(auc, ops::AucKernel<paddle::platform::CPUPlace, float>);