auc_op.cc 3.0 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 25 26 27 28 29 30 31 32
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
  void InferShape(const framework::InferShapeContext &ctx) const override {
    PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("Inference"),
                            "Input of Inference must be initialized.");
    PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("Label"),
                            "Input of Inference must be initialized.");
    auto *inference = ctx.Input<framework::Tensor>("Inference");
    auto *label = ctx.Input<framework::Tensor>("Label");

T
auc_op  
typhoonzero 已提交
33
    PADDLE_ENFORCE_EQ(inference->dims(), label->dims(),
T
typhoonzero 已提交
34
                      "inference and label should have same shape");
T
typhoonzero 已提交
35

T
update  
typhoonzero 已提交
36
    ctx.Output<framework::LoDTensor>("AUC")->Resize({1});
T
typhoonzero 已提交
37 38 39 40 41 42 43 44
  }
};

class AucOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  AucOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddInput("Inference",
T
typhoonzero 已提交
45 46
             "A floating point tensor of arbitrary shape and whose values"
             "are in the range [0, 1].");
T
auc_op  
typhoonzero 已提交
47
    AddInput("Label",
T
typhoonzero 已提交
48 49
             "A tensor whose shape matches "
             "Inference. Will be cast to bool.");
T
auc_op  
typhoonzero 已提交
50 51
    // TODO(typhoonzero): support weight input
    AddOutput("AUC",
T
typhoonzero 已提交
52
              "A scalar representing the "
T
auc_op  
typhoonzero 已提交
53
              "current area-under-curve.");
T
typhoonzero 已提交
54

T
typhoonzero 已提交
55
    AddAttr<std::string>("curve", "Curve type, can be 'ROC' or 'PR'.")
T
typhoonzero 已提交
56 57 58 59 60 61 62 63
        .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.
T
auc_op  
typhoonzero 已提交
64
        Best to use for binary classification evaluations.
T
typhoonzero 已提交
65 66

        If input label contains values other than 0 and 1, it will be cast
T
auc_op  
typhoonzero 已提交
67 68
        to bool.

T
typhoonzero 已提交
69 70 71 72
        You can find the definations here: 
        https://en.wikipedia.org/wiki/Receiver_operating_characteristic#Area_under_the_curve
        
        Possible curves are:
T
auc_op  
typhoonzero 已提交
73 74
        - ROC: Receiver operating characteristic
        - PR: Precision Recall
T
typhoonzero 已提交
75 76 77 78 79 80 81 82
        )DOC");
  }
};

}  // namespace operators
}  // namespace paddle

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