accuracy_op.cc 2.7 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 {
Q
Qiao Longfei 已提交
25 26 27 28 29 30 31 32 33 34 35 36
    PADDLE_ENFORCE(ctx->HasInput("Inference"),
                   "Input(Inference) of AccuracyOp should not be null.");
    PADDLE_ENFORCE(ctx->HasInput("Label"),
                   "Input(Label) of AccuracyOp should not be null.");
    PADDLE_ENFORCE(ctx->HasOutput("Accuracy"),
                   "Output(Accuracy) of AccuracyOp should not be null.");

    auto inference_dim = ctx->GetInputDim("Inference");
    auto label_dim = ctx->GetInputDim("Label");

    PADDLE_ENFORCE_EQ(label_dim.size(), 1, "label must be a vector");
    PADDLE_ENFORCE_EQ(inference_dim[0], label_dim[0],
武毅 已提交
37 38
                      "inference size must be the same as label size");

Q
Qiao Longfei 已提交
39 40
    ctx->SetOutputDim("Accuracy", {1});
    ctx->ShareLoD("Inference", /*->*/ "Accuracy");
武毅 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54
  }
};

class AccuracyOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  AccuracyOpMaker(framework::OpProto *proto,
                  framework::OpAttrChecker *op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
    // TODO(typhoonzero): support both inference value and indices.
    AddInput("Inference", "topk(indices) the network output");
    AddInput("Label", "Label of the training data");
    // TODO(typhoonzero): AddInput("Weight", ...
    AddOutput("Accuracy", "The accuracy of current batch");

D
Fix bug  
dangqingqing 已提交
55 56
    AddComment(R"DOC(
Accuracy. It will print accuracy rate for classification.
武毅 已提交
57 58
The accuracy is:
..  math::
59 60 61 62
accuracy = \\frac{NumOfCorrectPredicts}{NumOfAllSamples})

Both the input `Inference` and `Label` can carry the LoD (Level of Details)
information, or not. But the output only shares the LoD with input `Inference`.
D
Fix bug  
dangqingqing 已提交
63
)DOC");
武毅 已提交
64 65 66 67 68 69 70 71
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP_WITHOUT_GRADIENT(accuracy, ops::AccuracyOp, ops::AccuracyOpMaker);
72
REGISTER_OP_CPU_KERNEL(
Y
Yu Yang 已提交
73
    accuracy, ops::AccuracyKernel<paddle::platform::CPUPlace, int>,
74
    ops::AccuracyKernel<paddle::platform::CPUPlace, int64_t>);