/* 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. */ #pragma once #include #include #include "paddle/framework/eigen.h" #include "paddle/framework/op_registry.h" #include "paddle/utils/Logging.h" namespace paddle { namespace operators { using Tensor = framework::Tensor; using LoDTensor = framework::LoDTensor; template class PositiveNegativePairKernel : public framework::OpKernel { public: struct PredictionResult { PredictionResult(T score, T label, T weight) : score(score), label(label), weight(weight) {} T score; T label; T weight; }; void Compute(const framework::ExecutionContext& context) const override { auto score_t = context.Input("Score"); auto label_t = context.Input("Label"); auto query_t = context.Input("QueryID"); auto acc_positive_t = context.Input("AccumulatePositivePair"); auto acc_negative_t = context.Input("AccumulateNegativePair"); auto acc_neutral_t = context.Input("AccumulateNeutralPair"); auto positive_t = context.Output("PositivePair"); auto negative_t = context.Output("NegativePair"); auto neutral_t = context.Output("NeutralPair"); auto weight_t = context.Input("Weight"); auto score = score_t->data(); auto label = label_t->data(); auto query = query_t->data(); const T* weight = nullptr; auto has_weight = weight_t != nullptr; if (has_weight) { weight = weight_t->data(); } T* positive = positive_t->mutable_data(context.GetPlace()); T* negative = negative_t->mutable_data(context.GetPlace()); T* neutral = neutral_t->mutable_data(context.GetPlace()); auto score_dim = score_t->dims(); auto batch_size = score_dim[0]; auto width = score_dim[1]; auto column = context.Attr("column"); if (column < 0) { column += width; } // construct document instances for each query: Query => List[, ...] std::unordered_map> predictions; for (auto i = 0; i < batch_size; ++i) { if (predictions.find(query[i]) == predictions.end()) { predictions.emplace( std::make_pair(query[i], std::vector())); } predictions[query[i]].push_back(PredictionResult( score[i * width + column], label[i], has_weight ? weight[i] : 1.0)); } // for each query, accumulate pair counts T pos = 0, neg = 0, neu = 0; if (acc_positive_t != nullptr && acc_negative_t != nullptr && acc_neutral_t != nullptr) { pos = acc_positive_t->data()[0]; neg = acc_negative_t->data()[0]; neu = acc_neutral_t->data()[0]; } auto evaluate_one_list = [&pos, &neg, &neu](std::vector vec) { for (auto ite1 = vec.begin(); ite1 != vec.end(); ++ite1) { for (auto ite2 = ite1 + 1; ite2 != vec.end(); ++ite2) { if (ite1->label == ite2->label) { // labels are equal, ignore. continue; } T w = (ite1->weight + ite2->weight) * 0.5; if (ite1->score == ite2->score) { neu += w; } (ite1->score - ite2->score) * (ite1->label - ite2->label) > 0.0 ? pos += w : neg += w; } } }; for (auto prediction : predictions) { evaluate_one_list(prediction.second); } *positive = pos; *negative = neg; *neutral = neu; } }; } // namespace operators } // namespace paddle