From 9f82b533664e139e31de7bbd0a0663ae88d9a1df Mon Sep 17 00:00:00 2001 From: tqchen Date: Tue, 6 May 2014 16:50:46 -0700 Subject: [PATCH] add regrank utils --- regrank/xgboost_regrank_utils.h | 43 +++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 regrank/xgboost_regrank_utils.h diff --git a/regrank/xgboost_regrank_utils.h b/regrank/xgboost_regrank_utils.h new file mode 100644 index 000000000..580cedb81 --- /dev/null +++ b/regrank/xgboost_regrank_utils.h @@ -0,0 +1,43 @@ +#ifndef XGBOOST_REGRANK_UTILS_H +#define XGBOOST_REGRANK_UTILS_H +/*! + * \file xgboost_regrank_utils.h + * \brief useful helper functions + * \author Tianqi Chen, Kailong Chen + */ +namespace xgboost{ + namespace regrank{ + // simple helper function to do softmax + inline static void Softmax( std::vector& rec ){ + float wmax = rec[0]; + for( size_t i = 1; i < rec.size(); ++ i ){ + wmax = std::max( rec[i], wmax ); + } + double wsum = 0.0f; + for( size_t i = 0; i < rec.size(); ++ i ){ + rec[i] = expf(rec[i]-wmax); + wsum += rec[i]; + } + for( size_t i = 0; i < rec.size(); ++ i ){ + rec[i] /= wsum; + } + } + // simple helper function to do softmax + inline static int FindMaxIndex( std::vector& rec ){ + size_t mxid = 0; + for( size_t i = 1; i < rec.size(); ++ i ){ + if( rec[i] > rec[mxid] ) mxid = i; + } + return (int)mxid; + } + inline static bool CmpFirst(const std::pair &a, const std::pair &b){ + return a.first > b.first; + } + inline static bool CmpSecond(const std::pair &a, const std::pair &b){ + return a.second > b.second; + } + }; +}; + +#endif +