You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
3.7 KiB
100 lines
3.7 KiB
8 years ago
|
/* 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 {
|
||
|
|
||
8 years ago
|
class AucOp : public framework::OperatorWithKernel {
|
||
8 years ago
|
public:
|
||
|
using framework::OperatorWithKernel::OperatorWithKernel;
|
||
|
|
||
|
protected:
|
||
7 years ago
|
void InferShape(framework::InferShapeContext *ctx) const override {
|
||
7 years ago
|
PADDLE_ENFORCE(ctx->HasInput("Out"), "Input of Out should not be null.");
|
||
7 years ago
|
PADDLE_ENFORCE(ctx->HasInput("Indices"),
|
||
7 years ago
|
"Input of Indices should not be null.");
|
||
7 years ago
|
PADDLE_ENFORCE(ctx->HasInput("Label"),
|
||
7 years ago
|
"Input of Label should not be null.");
|
||
7 years ago
|
auto inference_height = ctx->GetInputDim("Out")[0];
|
||
|
auto label_height = ctx->GetInputDim("Label")[0];
|
||
7 years ago
|
|
||
7 years ago
|
PADDLE_ENFORCE_EQ(inference_height, label_height,
|
||
|
"Out and Label should have same height.");
|
||
8 years ago
|
|
||
7 years ago
|
ctx->SetOutputDim("AUC", {1});
|
||
7 years ago
|
ctx->ShareLoD("Out", /*->*/ "AUC");
|
||
|
}
|
||
|
|
||
|
protected:
|
||
7 years ago
|
framework::OpKernelType GetExpectedKernelType(
|
||
7 years ago
|
const framework::ExecutionContext &ctx) const override {
|
||
7 years ago
|
return framework::OpKernelType(
|
||
|
framework::ToDataType(ctx.Input<Tensor>("Out")->type()),
|
||
|
ctx.device_context());
|
||
8 years ago
|
}
|
||
|
};
|
||
|
|
||
|
class AucOpMaker : public framework::OpProtoAndCheckerMaker {
|
||
|
public:
|
||
7 years ago
|
AucOpMaker(OpProto *proto, OpAttrChecker *op_checker)
|
||
8 years ago
|
: OpProtoAndCheckerMaker(proto, op_checker) {
|
||
7 years ago
|
AddInput("Out",
|
||
|
"A floating point 2D tensor, values are in the range [0, 1]."
|
||
7 years ago
|
"Each row is sorted in descending order. This input should be the"
|
||
7 years ago
|
"output of topk."
|
||
|
"Typically, this tensor indicates the probability of each label");
|
||
|
AddInput("Indices",
|
||
|
"An int 2D tensor, indicating the indices of original"
|
||
7 years ago
|
"tensor before sorting. Typically, this tensor indicates which "
|
||
|
"label the probability stands for.");
|
||
8 years ago
|
AddInput("Label",
|
||
7 years ago
|
"A 2D int tensor indicating the label of the training data."
|
||
|
"The height is batch size and width is always 1.");
|
||
8 years ago
|
// TODO(typhoonzero): support weight input
|
||
|
AddOutput("AUC",
|
||
8 years ago
|
"A scalar representing the "
|
||
7 years ago
|
"current area-under-the-curve.");
|
||
8 years ago
|
|
||
8 years ago
|
AddAttr<std::string>("curve", "Curve type, can be 'ROC' or 'PR'.")
|
||
8 years ago
|
.SetDefault("ROC");
|
||
|
AddAttr<int>("num_thresholds",
|
||
|
"The number of thresholds to use when discretizing the"
|
||
|
" roc curve.")
|
||
|
.SetDefault(200);
|
||
|
|
||
7 years ago
|
AddComment(R"DOC(
|
||
|
Area Under The Curve (AUC) Operator.
|
||
7 years ago
|
|
||
7 years ago
|
This implementation computes the AUC according to forward output and label.
|
||
|
It is used very widely in binary classification evaluation. As a note:
|
||
7 years ago
|
If input label contains values other than 0 and 1, it will be cast
|
||
7 years ago
|
to bool. You can find the relevant definitions here:
|
||
7 years ago
|
https://en.wikipedia.org/wiki/Receiver_operating_characteristic#Area_under_the_curve
|
||
|
|
||
7 years ago
|
There are two types of possible curves:
|
||
|
1. ROC: Receiver operating characteristic
|
||
|
2. PR: Precision Recall
|
||
7 years ago
|
)DOC");
|
||
8 years ago
|
}
|
||
|
};
|
||
|
|
||
|
} // namespace operators
|
||
|
} // namespace paddle
|
||
|
|
||
|
namespace ops = paddle::operators;
|
||
8 years ago
|
REGISTER_OP_WITHOUT_GRADIENT(auc, ops::AucOp, ops::AucOpMaker);
|
||
8 years ago
|
REGISTER_OP_CPU_KERNEL(auc, ops::AucKernel<paddle::platform::CPUPlace, float>);
|