accuracy_op.cc 4.0 KB
Newer Older
武毅 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* 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/accuracy_op.h"

namespace paddle {
namespace operators {

class AccuracyOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

24
  void InferShape(framework::InferShapeContext *ctx) const override {
武毅 已提交
25 26 27 28
    PADDLE_ENFORCE(ctx->HasInput("Out"),
                   "Input (Out) of accuracy op should not be null.");
    PADDLE_ENFORCE(ctx->HasInput("Indices"),
                   "Input (Indices) of accuracy op should not be null.");
Q
Qiao Longfei 已提交
29
    PADDLE_ENFORCE(ctx->HasInput("Label"),
武毅 已提交
30
                   "Input (Label) of accuracy op should not be null.");
Q
Qiao Longfei 已提交
31
    PADDLE_ENFORCE(ctx->HasOutput("Accuracy"),
武毅 已提交
32
                   "Output (Accuracy) of AccuracyOp should not be null.");
D
Dong Zhihong 已提交
33 34
    PADDLE_ENFORCE(ctx->HasOutput("Correct"),
                   "Output (Correct) of AccuracyOp should not be null.");
D
Dong Zhihong 已提交
35 36
    PADDLE_ENFORCE(ctx->HasOutput("Total"),
                   "Output (Total) of AccuracyOp should not be null.");
Q
Qiao Longfei 已提交
37

武毅 已提交
38
    auto inference_dim = ctx->GetInputDim("Out");
Q
Qiao Longfei 已提交
39
    auto label_dim = ctx->GetInputDim("Label");
K
Kexin Zhao 已提交
40
    // Assume indices has same shape as inference, because
武毅 已提交
41
    // it's the output of topk.
Q
Qiao Longfei 已提交
42

F
fengjiayi 已提交
43 44
    PADDLE_ENFORCE_EQ(label_dim.size(), 2, "label's rank must be 2.");
    PADDLE_ENFORCE_EQ(label_dim[1], 1, "label's second dimension must be 1");
Q
Qiao Longfei 已提交
45
    PADDLE_ENFORCE_EQ(inference_dim[0], label_dim[0],
武毅 已提交
46 47
                      "the inference tensor's num_rows must be"
                      " the same as label.");
武毅 已提交
48

Q
Qiao Longfei 已提交
49
    ctx->SetOutputDim("Accuracy", {1});
D
Dong Zhihong 已提交
50
    ctx->SetOutputDim("Correct", {1});
D
Dong Zhihong 已提交
51
    ctx->SetOutputDim("Total", {1});
武毅 已提交
52 53 54 55
    ctx->ShareLoD("Out", /*->*/ "Accuracy");
  }

 protected:
56
  framework::OpKernelType GetExpectedKernelType(
武毅 已提交
57
      const framework::ExecutionContext &ctx) const override {
Y
Yu Yang 已提交
58 59
    return framework::OpKernelType(
        framework::ToDataType(ctx.Input<Tensor>("Out")->type()),
Q
QI JUN 已提交
60
        ctx.GetPlace());
武毅 已提交
61 62 63 64 65
  }
};

class AccuracyOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
66
  AccuracyOpMaker(OpProto *proto, OpAttrChecker *op_checker)
武毅 已提交
67 68
      : OpProtoAndCheckerMaker(proto, op_checker) {
    // TODO(typhoonzero): support both inference value and indices.
K
Kexin Zhao 已提交
69 70
    AddInput("Out", "The network output of topk (inferences)");
    AddInput("Indices", "The the network output of topk (indices)");
武毅 已提交
71 72 73
    AddInput("Label", "Label of the training data");
    // TODO(typhoonzero): AddInput("Weight", ...
    AddOutput("Accuracy", "The accuracy of current batch");
D
Dong Zhihong 已提交
74
    AddOutput("Correct", "The correct samples count of current batch");
D
Dong Zhihong 已提交
75
    AddOutput("Total", "The samples count of current batch");
武毅 已提交
76

D
Fix bug  
dangqingqing 已提交
77
    AddComment(R"DOC(
K
Kexin Zhao 已提交
78 79 80 81 82 83 84 85 86 87
Accuracy Operator. 

It will print accuracy rate for classification.
The accuracy is calculated as follows:

$$accuracy = \frac{NumOfCorrectPredicts}{NumOfAllSamples}$$

Both the input Out and Label can carry the LoD (Level of Details)
information, or not. But the output only shares the LoD information 
with the input Out(Inference).
88

D
Fix bug  
dangqingqing 已提交
89
)DOC");
武毅 已提交
90 91 92 93 94 95 96
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
F
fengjiayi 已提交
97 98
REGISTER_OPERATOR(accuracy, ops::AccuracyOp, ops::AccuracyOpMaker,
                  paddle::framework::EmptyGradOpMaker);
武毅 已提交
99 100 101 102 103
// FIXME(typhoonzero): types of T is for infernece data.
// label data is always int.
REGISTER_OP_CPU_KERNEL(accuracy,
                       ops::AccuracyKernel<paddle::platform::CPUPlace, float>,
                       ops::AccuracyKernel<paddle::platform::CPUPlace, double>);