From 539d633fe91ee44c5b5be77b2eaded2f1868c4ca Mon Sep 17 00:00:00 2001 From: Joe Zhao Date: Tue, 28 Apr 2015 10:21:25 +0800 Subject: CMC auditing & using std in main --- main.cpp | 59 +++++++++++++++++++++++++++++++++++------------------- model/rankaccu.cpp | 31 ++++++++++++++++++++++++++++ model/rankaccu.h | 30 +++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 21 deletions(-) diff --git a/main.cpp b/main.cpp index 434437f..5c977d2 100644 --- a/main.cpp +++ b/main.cpp @@ -10,6 +10,7 @@ INITIALIZE_EASYLOGGINGPP using namespace Eigen; +using namespace std; namespace po = boost::program_options; po::variables_map vm; @@ -18,7 +19,7 @@ typedef int (*mainFunc)(DataProvider &dp); int train(DataProvider &dp) { RSVM *rsvm; - rsvm = RSVM::loadModel(vm["model"].as()); + rsvm = RSVM::loadModel(vm["model"].as()); dp.open(); DataList D; @@ -27,31 +28,39 @@ int train(DataProvider &dp) { dp.getAllData(D); LOG(INFO)<<"Read "<train(D); - std::vector L; + vector L; rsvm->predict(D,L); - rank_accu(D,L); - LOG(INFO)<<"Training finished,saving model"; dp.close(); - rsvm->saveModel(vm["output"].as().c_str()); + rsvm->saveModel(vm["output"].as().c_str()); delete rsvm; return 0; } int predict(DataProvider &dp) { RSVM *rsvm; - rsvm = RSVM::loadModel(vm["model"].as().c_str()); + rsvm = RSVM::loadModel(vm["model"].as().c_str()); dp.open(); DataList D; - std::vector L; + vector L; + CMC cmc; LOG(INFO)<<"Prediction started"; - std::ofstream fout; + ofstream fout; if (vm.count("output")) - fout.open(vm["output"].as().c_str()); + fout.open(vm["output"].as().c_str()); + + ostream* ot; + + if (vm.count("output")) { + fout.open(vm["output"].as().c_str()); + ot=&fout; + } + else + ot=&cout; while (!dp.EOFile()) { @@ -62,17 +71,24 @@ int predict(DataProvider &dp) { if (vm.count("validate")) { rank_accu(D,L); + if (vm.count("cmc")) + rank_CMC(D,L,cmc); } - if (vm.count("output")) - for (int i=0; i cur = cmc.getAcc(); + for (int i = 0;i(), "set input model file") - ("output,o", po::value(), "set output model/prediction file") - ("feature,i", po::value(), "set input feature file"); + ("cmc,C", "enable cmc auditing") + ("model,m", po::value(), "set input model file") + ("output,o", po::value(), "set output model/prediction file") + ("feature,i", po::value(), "set input feature file"); // Parsing program options po::store(po::parse_command_line(argc, argv, desc), vm); @@ -98,7 +115,7 @@ int main(int argc, char **argv) { // Print help if necessary if (vm.count("help") || !(vm.count("train") || vm.count("validate") || vm.count("predict"))) { - std::cout << desc; + cout << desc; return 0; } mainFunc mainf; @@ -110,10 +127,10 @@ int main(int argc, char **argv) { } else return 0; DataProvider* dp; - if (vm["feature"].as().find(".rid") == std::string::npos) - dp = new FileDP(vm["feature"].as()); + if (vm["feature"].as().find(".rid") == string::npos) + dp = new FileDP(vm["feature"].as()); else - dp = new RidFileDP(vm["feature"].as()); + dp = new RidFileDP(vm["feature"].as()); mainf(*dp); delete dp; return 0; diff --git a/model/rankaccu.cpp b/model/rankaccu.cpp index 069e245..1763e0f 100644 --- a/model/rankaccu.cpp +++ b/model/rankaccu.cpp @@ -112,4 +112,35 @@ int rank_accu(DataList &D,const vector pred) ++i; } LOG(INFO)<<"over "<< cnt<< " queries. "<<"Average nDGC: "<< accu_nDCG/cnt<< " Average AP: "< pred,CMC & cmc) { + unsigned long n = D.getSize(); + vector orig_rank(n),pred_rank(n),C(n); + vector orig(n); + int i,j; + for (i=0;irank; + } + int cnt=0; + i=j=0; + while (iqid!=D.getData()[i+1]->qid) + { + ranksort(j,i,pred_rank,pred,orig); + for (int k=j;k<=i;++k) + if (orig[pred_rank[k]]>0) + { + cmc.addEntry(k-j); + break; // account only for the first match; + } + j = i+1; + ++cnt; + } + ++i; + } } \ No newline at end of file diff --git a/model/rankaccu.h b/model/rankaccu.h index 3fe5379..adf1a1f 100644 --- a/model/rankaccu.h +++ b/model/rankaccu.h @@ -8,6 +8,36 @@ #include #include"../tools/dataProvider.h" +#define CMC_MAX 100 + +class CMC +{ +private: + std::vector acc; + int cnt; +public: + void clear(){for (int i=0;i getAcc() { + std::vector res; + res.reserve(CMC_MAX); + double cumu = 0; + for (int i=0;i pred,CMC & cmc); + int rank_accu(DataList &D,const std::vector pred); #endif //RANKSVM_RANKACCU_H -- cgit v1.2.3-70-g09d2