From cce96e8f41ecab65b58952417f1f10178dbd2d79 Mon Sep 17 00:00:00 2001 From: kalenhaha Date: Fri, 2 May 2014 00:16:12 +0800 Subject: [PATCH] fix some bugs in linux --- regrank/xgboost_regrank_eval.h | 28 +++++++++++++++++++++------- regrank/xgboost_regrank_obj.hpp | 14 +++++++------- utils/xgboost_omp.h | 2 +- utils/xgboost_random.h | 5 ++++- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/regrank/xgboost_regrank_eval.h b/regrank/xgboost_regrank_eval.h index 2e90f583a..a25e87e73 100644 --- a/regrank/xgboost_regrank_eval.h +++ b/regrank/xgboost_regrank_eval.h @@ -13,6 +13,8 @@ #include "../utils/xgboost_omp.h" #include "../utils/xgboost_random.h" #include "xgboost_regrank_data.h" +#include +#include namespace xgboost{ namespace regrank{ @@ -199,7 +201,7 @@ namespace xgboost{ const DMatrix::Info &info) const{ if (info.group_ptr.size() <= 1) return 0; float acc = 0; - std::vector> pairs_sort; + std::vector< std::pair > pairs_sort; for (int i = 0; i < info.group_ptr.size() - 1; i++){ for (int j = info.group_ptr[i]; j < info.group_ptr[i + 1]; j++){ pairs_sort.push_back(std::make_pair(preds[j], info.labels[j])); @@ -222,16 +224,23 @@ namespace xgboost{ } private: - float NDCG(std::vector> pairs_sort) const{ - std::sort(pairs_sort.begin(), pairs_sort.end(), std::greater()); + /*\brief Obtain NDCG given the list of labels and predictions + * \param pairs_sort the first field is prediction and the second is label + */ + float NDCG(std::vector< std::pair > pairs_sort) const{ + std::sort(pairs_sort.begin(), pairs_sort.end(), [](std::pair a, std::pair b){ + return std::get<0>(a) > std::get<0>(b); + }); float dcg = DCG(pairs_sort); - std::sort(pairs_sort.begin(), pairs_sort.end(), std::greater()); + std::sort(pairs_sort.begin(), pairs_sort.end(), [](std::pair a, std::pair b){ + return std::get<1>(a) > std::get<1>(b); + }); float IDCG = DCG(pairs_sort); if (IDCG == 0) return 0; return dcg / IDCG; } - float DCG(std::vector> pairs_sort) const{ + float DCG(std::vector< std::pair > pairs_sort) const{ std::vector labels; for (int i = 1; i < pairs_sort.size(); i++){ labels.push_back(std::get<1>(pairs_sort[i])); @@ -263,8 +272,13 @@ namespace xgboost{ } private: - float average_precision(std::vector> pairs_sort) const{ - std::sort(pairs_sort.begin(), pairs_sort.end(), std::greater()); + /*\brief Obtain average precision given the list of labels and predictions + * \param pairs_sort the first field is prediction and the second is label + */ + float average_precision(std::vector< std::pair > pairs_sort) const{ + std::sort(pairs_sort.begin(), pairs_sort.end(), [](std::pair a, std::pair b){ + return std::get<0>(a) > std::get<0>(b); + }); float hits = 0; float average_precision = 0; for (int j = 0; j < pairs_sort.size(); j++){ diff --git a/regrank/xgboost_regrank_obj.hpp b/regrank/xgboost_regrank_obj.hpp index ab472eb3b..f7d889594 100644 --- a/regrank/xgboost_regrank_obj.hpp +++ b/regrank/xgboost_regrank_obj.hpp @@ -245,6 +245,12 @@ namespace xgboost{ } private: + int lambda_; + const static int PAIRWISE = 0; + const static int MAP = 1; + const static int NDCG = 2; + sample::PairSamplerWrapper sampler_; + LossType loss_; /* \brief Sorted tuples of a group by the predictions, and * the fields in the return tuples successively are predicions, * labels, and the index of the instance @@ -400,13 +406,7 @@ namespace xgboost{ return pred_diff_exp / pow(1 + pred_diff_exp, 2); } - private: - int lambda_; - const static int PAIRWISE = 0; - const static int MAP = 1; - const static int NDCG = 2; - sample::PairSamplerWrapper sampler_; - LossType loss_; + }; }; }; diff --git a/utils/xgboost_omp.h b/utils/xgboost_omp.h index ea1e7173c..3a8062b7e 100644 --- a/utils/xgboost_omp.h +++ b/utils/xgboost_omp.h @@ -10,7 +10,7 @@ #if defined(_OPENMP) #include #else -#warning "OpenMP is not available, compile to single thread code" +//#warning "OpenMP is not available, compile to single thread code" inline int omp_get_thread_num() { return 0; } inline int omp_get_num_threads() { return 1; } inline void omp_set_num_threads(int nthread) {} diff --git a/utils/xgboost_random.h b/utils/xgboost_random.h index 3d5f164a3..6a64c7bff 100644 --- a/utils/xgboost_random.h +++ b/utils/xgboost_random.h @@ -136,7 +136,10 @@ namespace xgboost{ } /*! \brief return a real number uniform in [0,1) */ inline double RandDouble( void ){ - return static_cast( rand_r( &rseed ) ) / (static_cast( RAND_MAX )+1.0); + + // return static_cast( rand_( &rseed ) ) / (static_cast( RAND_MAX )+1.0); + return static_cast(rand()) / (static_cast(RAND_MAX)+1.0); + } // random number seed unsigned rseed;