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

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

#include "paddle/operators/auc_op.h"

namespace paddle {
namespace operators {

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

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

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

T
typhoonzero 已提交
37
    ctx->SetOutputDim("AUC", {1});
武毅 已提交
38 39 40 41 42 43 44 45
    ctx->ShareLoD("Out", /*->*/ "AUC");
  }

 protected:
  // IndicateDataType
  framework::DataType IndicateDataType(
      const framework::ExecutionContext &ctx) const override {
    return framework::ToDataType(ctx.Input<Tensor>("Out")->type());
T
typhoonzero 已提交
46 47 48 49 50 51 52
  }
};

class AucOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  AucOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
武毅 已提交
53 54 55 56 57 58 59 60 61
    AddInput("Out",
             "A floating point 2D tensor, values are in the range [0, 1]."
             "Each row is descend sorted. This input should be the"
             "output of topk."
             "Typically, this tensor indicates the probability of each label");
    AddInput("Indices",
             "An int 2D tensor, indicating the indices of original"
             "tensor before sort. Typically, this tensor indicates which label"
             "the probability stands for.");
T
auc_op  
typhoonzero 已提交
62
    AddInput("Label",
武毅 已提交
63 64
             "A 2D int tensor indicating the label of the training data."
             "The height is batch size and width is always 1.");
T
auc_op  
typhoonzero 已提交
65 66
    // TODO(typhoonzero): support weight input
    AddOutput("AUC",
T
typhoonzero 已提交
67
              "A scalar representing the "
T
auc_op  
typhoonzero 已提交
68
              "current area-under-curve.");
T
typhoonzero 已提交
69

T
typhoonzero 已提交
70
    AddAttr<std::string>("curve", "Curve type, can be 'ROC' or 'PR'.")
T
typhoonzero 已提交
71 72 73 74 75 76 77 78
        .SetDefault("ROC");
    AddAttr<int>("num_thresholds",
                 "The number of thresholds to use when discretizing the"
                 " roc curve.")
        .SetDefault(200);

    AddComment(
        R"DOC(Computes the AUC according forward output and label.
武毅 已提交
79 80 81 82 83 84 85 86 87 88 89 90
Best to use for binary classification evaluations.

If input label contains values other than 0 and 1, it will be cast
to bool.

You can find the definations here: 
https://en.wikipedia.org/wiki/Receiver_operating_characteristic#Area_under_the_curve

Possible curves are:
- ROC: Receiver operating characteristic
- PR: Precision Recall
)DOC");
T
typhoonzero 已提交
91 92 93 94 95 96 97
  }
};

}  // namespace operators
}  // namespace paddle

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