accuracy_op.cc 5.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. */

W
Wu Yi 已提交
15
#include "paddle/fluid/operators/metrics/accuracy_op.h"
武毅 已提交
16 17 18 19 20 21 22 23

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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    PADDLE_ENFORCE_EQ(
        ctx->HasInput("Out"), true,
        platform::errors::NotFound("Input (Out) of AccuracyOp is not found."));
    PADDLE_ENFORCE_EQ(ctx->HasInput("Indices"), true,
                      platform::errors::NotFound(
                          "Input (Indices) of AccuracyOp is not found."));
    PADDLE_ENFORCE_EQ(ctx->HasInput("Label"), true,
                      platform::errors::NotFound(
                          "Input (Label) of AccuracyOp is not found."));
    PADDLE_ENFORCE_EQ(ctx->HasOutput("Accuracy"), true,
                      platform::errors::NotFound(
                          "Output (Accuracy) of AccuracyOp is not found."));
    PADDLE_ENFORCE_EQ(ctx->HasOutput("Correct"), true,
                      platform::errors::NotFound(
                          "Output (Correct) of AccuracyOp is not found."));
    PADDLE_ENFORCE_EQ(ctx->HasOutput("Total"), true,
                      platform::errors::NotFound(
                          "Output (Total) of AccuracyOp is not found."));

    OP_INOUT_CHECK(ctx->HasInput("Out"), "Input", "Out", "Accuracy");
    OP_INOUT_CHECK(ctx->HasInput("Indices"), "Input", "Indices", "Accuracy");
    OP_INOUT_CHECK(ctx->HasInput("Label"), "Input", "Label", "Accuracy");
    OP_INOUT_CHECK(ctx->HasOutput("Accuracy"), "Output", "Accuracy",
                   "Accuracy");
    OP_INOUT_CHECK(ctx->HasOutput("Correct"), "Output", "Correct", "Accuracy");
    OP_INOUT_CHECK(ctx->HasOutput("Total"), "Output", "Total", "Accuracy");
Q
Qiao Longfei 已提交
51

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

57 58
    PADDLE_ENFORCE_EQ(
        label_dim.size(), 2,
59 60 61 62
        platform::errors::InvalidArgument(
            "ShapeError: label's dimensions of AccuracyOp must be 2. "
            "But received label's dimensions = %d, label's shape = [%s]",
            label_dim.size(), label_dim));
63 64
    if (ctx->IsRuntime()) {
      PADDLE_ENFORCE_EQ(label_dim[1], 1,
65 66 67 68 69
                        platform::errors::InvalidArgument(
                            "ShapeError: label's second dimension of "
                            "AccuracyOp must be 1. But received label's "
                            "second dimension is = %d, label's shape = [%s]",
                            label_dim[1], label_dim));
70 71
      PADDLE_ENFORCE_EQ(
          inference_dim[0], label_dim[0],
72 73 74 75 76 77 78
          platform::errors::InvalidArgument(
              "ShapeError: the output's num_rows of AccuracyOp must be"
              " the same as label's num_rows. But received output's "
              "shape = [%s], label's shape = [%s], output's num_rows = %d, "
              "label's "
              "num_rows = %d",
              inference_dim, label_dim, inference_dim[0], label_dim[0]));
79
    }
武毅 已提交
80

Q
Qiao Longfei 已提交
81
    ctx->SetOutputDim("Accuracy", {1});
D
Dong Zhihong 已提交
82
    ctx->SetOutputDim("Correct", {1});
D
Dong Zhihong 已提交
83
    ctx->SetOutputDim("Total", {1});
武毅 已提交
84 85 86 87
    ctx->ShareLoD("Out", /*->*/ "Accuracy");
  }

 protected:
88
  framework::OpKernelType GetExpectedKernelType(
武毅 已提交
89
      const framework::ExecutionContext &ctx) const override {
90 91
    return framework::OpKernelType(
        OperatorWithKernel::IndicateVarDataType(ctx, "Out"), ctx.GetPlace());
武毅 已提交
92 93 94 95 96
  }
};

class AccuracyOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
97
  void Make() override {
武毅 已提交
98
    // TODO(typhoonzero): support both inference value and indices.
K
Kexin Zhao 已提交
99 100
    AddInput("Out", "The network output of topk (inferences)");
    AddInput("Indices", "The the network output of topk (indices)");
武毅 已提交
101 102 103
    AddInput("Label", "Label of the training data");
    // TODO(typhoonzero): AddInput("Weight", ...
    AddOutput("Accuracy", "The accuracy of current batch");
D
Dong Zhihong 已提交
104
    AddOutput("Correct", "The correct samples count of current batch");
D
Dong Zhihong 已提交
105
    AddOutput("Total", "The samples count of current batch");
武毅 已提交
106

D
Fix bug  
dangqingqing 已提交
107
    AddComment(R"DOC(
K
Kexin Zhao 已提交
108 109 110 111 112 113 114 115 116 117
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).
118

D
Fix bug  
dangqingqing 已提交
119
)DOC");
武毅 已提交
120 121 122 123 124 125 126
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
H
hong 已提交
127 128 129 130
REGISTER_OPERATOR(
    accuracy, ops::AccuracyOp, ops::AccuracyOpMaker,
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>);
武毅 已提交
131 132 133 134 135
// 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>);