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{
@ -199,7 +201,7 @@ namespace xgboost{
const DMatrix::Info &info) const{ const DMatrix::Info &info) const{
if (info.group_ptr.size() <= 1) return 0; if (info.group_ptr.size() <= 1) return 0;
float acc = 0; float acc = 0;
std::vector<std::pair<float, float>> pairs_sort; std::vector< std::pair<float, float> > pairs_sort;
for (int i = 0; i < info.group_ptr.size() - 1; i++){ 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++){ 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])); pairs_sort.push_back(std::make_pair(preds[j], info.labels[j]));
@ -222,16 +224,23 @@ namespace xgboost{
} }
private: private:
float NDCG(std::vector<std::pair<float, float>> pairs_sort) const{ /*\brief Obtain NDCG given the list of labels and predictions
std::sort(pairs_sort.begin(), pairs_sort.end(), std::greater<float>()); * \param pairs_sort the first field is prediction and the second is label
*/
float NDCG(std::vector< std::pair<float, float> > pairs_sort) const{
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;
} }
float DCG(std::vector<std::pair<float, float>> pairs_sort) const{ float DCG(std::vector< std::pair<float, float> > pairs_sort) const{
std::vector<float> labels; std::vector<float> labels;
for (int i = 1; i < pairs_sort.size(); i++){ for (int i = 1; i < pairs_sort.size(); i++){
labels.push_back(std::get<1>(pairs_sort[i])); labels.push_back(std::get<1>(pairs_sort[i]));
@ -263,8 +272,13 @@ namespace xgboost{
} }
private: private:
float average_precision(std::vector<std::pair<float,float>> pairs_sort) const{ /*\brief Obtain average precision given the list of labels and predictions
std::sort(pairs_sort.begin(), pairs_sort.end(), std::greater<float>()); * \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{
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;