未验证 提交 1f3b9ef5 编写于 作者: G gouzil 提交者: GitHub

add autogen code support for accuracy (#52424)

* add autogen code support for accuracy

* fix input
上级 2f850990
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
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/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/phi/infermeta/ternary.h"
namespace paddle {
namespace operators {
class AccuracyOp : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;
protected:
phi::KernelKey GetExpectedKernelType(
const framework::ExecutionContext &ctx) const override {
return phi::KernelKey(OperatorWithKernel::IndicateVarDataType(ctx, "Out"),
ctx.GetPlace());
}
};
class AccuracyOpMaker : public framework::OpProtoAndCheckerMaker {
public:
void Make() override {
// TODO(typhoonzero): support both inference value and indices.
AddInput("Out", "The network output of topk (inferences)");
AddInput("Indices", "The network output of topk (indices)");
AddInput("Label", "Label of the training data");
// TODO(typhoonzero): AddInput("Weight", ...
AddOutput("Accuracy", "The accuracy of current batch");
AddOutput("Correct", "The correct samples count of current batch");
AddOutput("Total", "The samples count of current batch");
AddComment(R"DOC(
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).
)DOC");
}
};
} // namespace operators
} // namespace paddle
// FIXME(typhoonzero): types of T is for infernece data.
// label data is always int.
DECLARE_INFER_SHAPE_FUNCTOR(accuracy,
AccuracyInferShapeFunctor,
PD_INFER_META(phi::AccuracyInferMeta));
namespace ops = paddle::operators;
REGISTER_OPERATOR(
accuracy,
ops::AccuracyOp,
ops::AccuracyOpMaker,
paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
AccuracyInferShapeFunctor);
...@@ -10,15 +10,6 @@ ...@@ -10,15 +10,6 @@
func : abs func : abs
backward : abs_grad backward : abs_grad
- op : accuracy
args : (Tensor x, Tensor indices, Tensor label)
output : Tensor(accuracy), Tensor(correct), Tensor(total)
infer_meta :
func : AccuracyInferMeta
kernel :
func : accuracy
data_type : x
- op : adadelta_ - op : adadelta_
args : (Tensor param, Tensor grad, Tensor avg_squared_grad, Tensor avg_squared_update, Tensor master_param, float rho, float epsilon, bool multi_precision) args : (Tensor param, Tensor grad, Tensor avg_squared_grad, Tensor avg_squared_update, Tensor master_param, float rho, float epsilon, bool multi_precision)
output : Tensor(param_out), Tensor(moment_out), Tensor(inf_norm_out), Tensor(master_param_out) output : Tensor(param_out), Tensor(moment_out), Tensor(inf_norm_out), Tensor(master_param_out)
......
...@@ -23,6 +23,12 @@ ...@@ -23,6 +23,12 @@
extra : extra :
attrs : [bool use_mkldnn = false] attrs : [bool use_mkldnn = false]
- op : accuracy
inputs :
{x : Out , indices : Indices, label: Label}
outputs :
{accuracy : Accuracy, correct : Correct, total : Total}
- op : acos - op : acos
inputs : inputs :
x : X x : X
......
...@@ -5,6 +5,15 @@ ...@@ -5,6 +5,15 @@
# are consistent and correspond one-to-one. It's forbidden that the # are consistent and correspond one-to-one. It's forbidden that the
# operator configured in this yaml file does not have Python API. # operator configured in this yaml file does not have Python API.
- op : accuracy
args : (Tensor x, Tensor indices, Tensor label)
output : Tensor(accuracy), Tensor(correct), Tensor(total)
infer_meta :
func : AccuracyInferMeta
kernel :
func : accuracy
data_type : x
- op : acos - op : acos
args : (Tensor x) args : (Tensor x)
output : Tensor output : Tensor
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册