accuracy_op.cc 2.6 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
武毅 已提交
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. */

15
#include "paddle/fluid/framework/infershape_utils.h"
16
#include "paddle/fluid/framework/op_registry.h"
17
#include "paddle/phi/infermeta/ternary.h"
武毅 已提交
18 19 20 21 22 23 24 25

namespace paddle {
namespace operators {

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

武毅 已提交
26
 protected:
27
  framework::OpKernelType GetExpectedKernelType(
武毅 已提交
28
      const framework::ExecutionContext &ctx) const override {
29 30
    return framework::OpKernelType(
        OperatorWithKernel::IndicateVarDataType(ctx, "Out"), ctx.GetPlace());
武毅 已提交
31 32 33 34 35
  }
};

class AccuracyOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
36
  void Make() override {
武毅 已提交
37
    // TODO(typhoonzero): support both inference value and indices.
K
Kexin Zhao 已提交
38 39
    AddInput("Out", "The network output of topk (inferences)");
    AddInput("Indices", "The the network output of topk (indices)");
武毅 已提交
40 41 42
    AddInput("Label", "Label of the training data");
    // TODO(typhoonzero): AddInput("Weight", ...
    AddOutput("Accuracy", "The accuracy of current batch");
D
Dong Zhihong 已提交
43
    AddOutput("Correct", "The correct samples count of current batch");
D
Dong Zhihong 已提交
44
    AddOutput("Total", "The samples count of current batch");
武毅 已提交
45

D
Fix bug  
dangqingqing 已提交
46
    AddComment(R"DOC(
K
Kexin Zhao 已提交
47 48 49 50 51 52 53 54 55 56
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).
57

D
Fix bug  
dangqingqing 已提交
58
)DOC");
武毅 已提交
59 60 61 62 63 64
  }
};

}  // namespace operators
}  // namespace paddle

65 66
// FIXME(typhoonzero): types of T is for infernece data.
// label data is always int.
67 68
DECLARE_INFER_SHAPE_FUNCTOR(accuracy, AccuracyInferShapeFunctor,
                            PD_INFER_META(phi::AccuracyInferMeta));
武毅 已提交
69
namespace ops = paddle::operators;
H
hong 已提交
70 71 72
REGISTER_OPERATOR(
    accuracy, ops::AccuracyOp, ops::AccuracyOpMaker,
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
73 74
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
    AccuracyInferShapeFunctor);