auc_op.h 3.6 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
T
typhoonzero 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

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. */

#pragma once
T
tangwei12 已提交
16

17 18
#include <string>
#include <vector>
Y
Yi Wang 已提交
19
#include "paddle/fluid/framework/op_registry.h"
T
typhoonzero 已提交
20 21 22 23 24 25

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;

Q
QI JUN 已提交
26
template <typename DeviceContext, typename T>
T
typhoonzero 已提交
27
class AucKernel : public framework::OpKernel<T> {
T
typhoonzero 已提交
28
 public:
T
tangwei12 已提交
29 30 31 32 33 34 35 36
  void Compute(const framework::ExecutionContext &ctx) const override {
    auto *predict = ctx.Input<Tensor>("Predict");
    auto *label = ctx.Input<Tensor>("Label");

    std::string curve = ctx.Attr<std::string>("curve");
    int num_thresholds = ctx.Attr<int>("num_thresholds");
    int num_pred_buckets = num_thresholds + 1;

W
Wu Yi 已提交
37 38
    // Only use output var for now, make sure it's persistable and
    // not cleaned up for each batch.
T
tangwei12 已提交
39 40 41
    auto *auc = ctx.Output<Tensor>("AUC");
    auto *stat_pos = ctx.Output<Tensor>("StatPosOut");
    auto *stat_neg = ctx.Output<Tensor>("StatNegOut");
T
typhoonzero 已提交
42

T
tangwei12 已提交
43 44 45 46
    auto *stat_pos_data = stat_pos->mutable_data<int64_t>(ctx.GetPlace());
    auto *stat_neg_data = stat_neg->mutable_data<int64_t>(ctx.GetPlace());
    calcAuc(ctx, label, predict, stat_pos_data, stat_neg_data, num_thresholds,
            auc);
T
typhoonzero 已提交
47

T
tangwei12 已提交
48 49 50 51 52 53
    auto *batch_auc = ctx.Output<Tensor>("BatchAUC");
    std::vector<int64_t> stat_pos_batch(num_pred_buckets, 0);
    std::vector<int64_t> stat_neg_batch(num_pred_buckets, 0);
    calcAuc(ctx, label, predict, stat_pos_batch.data(), stat_neg_batch.data(),
            num_thresholds, batch_auc);
  }
T
typhoonzero 已提交
54

T
tangwei12 已提交
55 56 57 58 59 60 61 62 63 64 65 66
 private:
  inline static double trapezoidArea(double X1, double X2, double Y1,
                                     double Y2) {
    return (X1 > X2 ? (X1 - X2) : (X2 - X1)) * (Y1 + Y2) / 2.0;
  }

  inline static void calcAuc(const framework::ExecutionContext &ctx,
                             const framework::Tensor *label,
                             const framework::Tensor *predict,
                             int64_t *stat_pos, int64_t *stat_neg,
                             int num_thresholds,
                             framework::Tensor *auc_tensor) {
Q
Qiao Longfei 已提交
67 68
    size_t batch_size = predict->dims()[0];
    size_t inference_width = predict->dims()[1];
T
tangwei12 已提交
69 70 71 72
    const T *inference_data = predict->data<T>();
    const auto *label_data = label->data<int64_t>();

    auto *auc = auc_tensor->mutable_data<double>(ctx.GetPlace());
T
auc_op  
typhoonzero 已提交
73

T
tangwei12 已提交
74 75 76 77 78 79 80
    for (size_t i = 0; i < batch_size; i++) {
      uint32_t binIdx = static_cast<uint32_t>(
          inference_data[i * inference_width + 1] * num_thresholds);
      if (label_data[i]) {
        stat_pos[binIdx] += 1.0;
      } else {
        stat_neg[binIdx] += 1.0;
T
typhoonzero 已提交
81 82
      }
    }
T
tangwei12 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

    *auc = 0.0f;

    double totPos = 0.0;
    double totNeg = 0.0;
    double totPosPrev = 0.0;
    double totNegPrev = 0.0;

    int idx = num_thresholds;

    while (idx >= 0) {
      totPosPrev = totPos;
      totNegPrev = totNeg;
      totPos += stat_pos[idx];
      totNeg += stat_neg[idx];
      *auc += trapezoidArea(totNeg, totNegPrev, totPos, totPosPrev);

      --idx;
T
typhoonzero 已提交
101
    }
T
tangwei12 已提交
102 103 104

    if (totPos > 0.0 && totNeg > 0.0) {
      *auc = *auc / totPos / totNeg;
T
typhoonzero 已提交
105 106 107 108 109 110
    }
  }
};

}  // namespace operators
}  // namespace paddle