|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#pragma once |
| 16 | +#include <algorithm> |
| 17 | +#include "paddle/framework/eigen.h" |
| 18 | +#include "paddle/framework/op_registry.h" |
| 19 | + |
| 20 | +namespace paddle { |
| 21 | +namespace operators { |
| 22 | + |
| 23 | +using Tensor = framework::Tensor; |
| 24 | + |
| 25 | +template <typename T, int MajorType = Eigen::RowMajor, |
| 26 | + typename IndexType = Eigen::DenseIndex> |
| 27 | +using EigenMatrix = framework::EigenMatrix<T, MajorType, IndexType>; |
| 28 | + |
| 29 | +template <typename T, int MajorType = Eigen::RowMajor, |
| 30 | + typename IndexType = Eigen::DenseIndex> |
| 31 | +using EigenVector = framework::EigenVector<T, MajorType, IndexType>; |
| 32 | + |
| 33 | +template <typename T, int MajorType = Eigen::RowMajor, |
| 34 | + typename IndexType = Eigen::DenseIndex> |
| 35 | +using EigenScalar = framework::EigenScalar<T, MajorType, IndexType>; |
| 36 | + |
| 37 | +template <typename Place, typename T> |
| 38 | +class AccuracyKernel : public framework::OpKernel { |
| 39 | + public: |
| 40 | + void Compute(const framework::ExecutionContext& ctx) const override { |
| 41 | + auto* inference = ctx.Input<Tensor>("Inference"); |
| 42 | + auto* label = ctx.Input<Tensor>("Label"); |
| 43 | + auto* accuracy = ctx.Output<Tensor>("Accuracy"); |
| 44 | + |
| 45 | + float* accuracy_data = accuracy->mutable_data<float>(ctx.GetPlace()); |
| 46 | + |
| 47 | + const T* inference_data = inference->data<T>(); |
| 48 | + const T* label_data = label->data<T>(); |
| 49 | + |
| 50 | + size_t num_samples = inference->dims()[0]; |
| 51 | + size_t class_dim = inference->dims()[1]; |
| 52 | + *accuracy_data = 0.0f; |
| 53 | + |
| 54 | + if (num_samples == 0) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + int num_correct = 0; |
| 59 | + // assume inference is already the topk of the output |
| 60 | + for (size_t i = 0; i < num_samples; ++i) { |
| 61 | + PADDLE_ENFORCE_GE(label_data[i], 0, "label must >= 0"); |
| 62 | + for (size_t j = 0; j < class_dim; ++j) { |
| 63 | + if (inference_data[i * class_dim + j] == label_data[i]) { |
| 64 | + ++num_correct; |
| 65 | + break; |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // FIXME(typhoonzero): we don't accumulate the accuracy for now. |
| 71 | + *accuracy_data = |
| 72 | + static_cast<float>(num_correct) / static_cast<float>(num_samples); |
| 73 | + } |
| 74 | +}; |
| 75 | + |
| 76 | +} // namespace operators |
| 77 | +} // namespace paddle |
0 commit comments