fix some bugs in linux

This commit is contained in:
kalenhaha 2014-05-02 00:16:12 +08:00
parent b836b1123e
commit f17d400fd3
4 changed files with 33 additions and 16 deletions

View File

@ -13,6 +13,8 @@
#include "../utils/xgboost_omp.h" #include "../utils/xgboost_omp.h"
#include "../utils/xgboost_random.h" #include "../utils/xgboost_random.h"
#include "xgboost_regrank_data.h" #include "xgboost_regrank_data.h"
#include <functional>
#include <tuple>
namespace xgboost{ namespace xgboost{
namespace regrank{ namespace regrank{
@ -222,10 +224,17 @@ namespace xgboost{
} }
private: private:
/*\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<float, float> > pairs_sort) const{ float NDCG(std::vector< std::pair<float, float> > pairs_sort) const{
std::sort(pairs_sort.begin(), pairs_sort.end(), std::greater<float>()); std::sort(pairs_sort.begin(), pairs_sort.end(), [](std::pair<float, float> a, std::pair<float, float> b){
return std::get<0>(a) > std::get<0>(b);
});
float dcg = DCG(pairs_sort); float dcg = DCG(pairs_sort);
std::sort(pairs_sort.begin(), pairs_sort.end(), std::greater<float>()); std::sort(pairs_sort.begin(), pairs_sort.end(), [](std::pair<float, float> a, std::pair<float, float> b){
return std::get<1>(a) > std::get<1>(b);
});
float IDCG = DCG(pairs_sort); float IDCG = DCG(pairs_sort);
if (IDCG == 0) return 0; if (IDCG == 0) return 0;
return dcg / IDCG; return dcg / IDCG;
@ -263,8 +272,13 @@ namespace xgboost{
} }
private: private:
/*\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<float,float> > pairs_sort) const{ float average_precision(std::vector< std::pair<float,float> > pairs_sort) const{
std::sort(pairs_sort.begin(), pairs_sort.end(), std::greater<float>()); std::sort(pairs_sort.begin(), pairs_sort.end(), [](std::pair<float, float> a, std::pair<float, float> b){
return std::get<0>(a) > std::get<0>(b);
});
float hits = 0; float hits = 0;
float average_precision = 0; float average_precision = 0;
for (int j = 0; j < pairs_sort.size(); j++){ for (int j = 0; j < pairs_sort.size(); j++){

View File

@ -245,6 +245,12 @@ namespace xgboost{
} }
private: 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 /* \brief Sorted tuples of a group by the predictions, and
* the fields in the return tuples successively are predicions, * the fields in the return tuples successively are predicions,
* labels, and the index of the instance * labels, and the index of the instance
@ -400,13 +406,7 @@ namespace xgboost{
return pred_diff_exp / pow(1 + pred_diff_exp, 2); 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_;
}; };
}; };
}; };

View File

@ -10,7 +10,7 @@
#if defined(_OPENMP) #if defined(_OPENMP)
#include <omp.h> #include <omp.h>
#else #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_thread_num() { return 0; }
inline int omp_get_num_threads() { return 1; } inline int omp_get_num_threads() { return 1; }
inline void omp_set_num_threads(int nthread) {} inline void omp_set_num_threads(int nthread) {}

View File

@ -136,7 +136,10 @@ namespace xgboost{
} }
/*! \brief return a real number uniform in [0,1) */ /*! \brief return a real number uniform in [0,1) */
inline double RandDouble( void ){ inline double RandDouble( void ){
return static_cast<double>( rand_r( &rseed ) ) / (static_cast<double>( RAND_MAX )+1.0);
// return static_cast<double>( rand_( &rseed ) ) / (static_cast<double>( RAND_MAX )+1.0);
return static_cast<double>(rand()) / (static_cast<double>(RAND_MAX)+1.0);
} }
// random number seed // random number seed
unsigned rseed; unsigned rseed;