#include <iostream> #include <Eigen/Dense> #include <boost/program_options.hpp> #include <list> #include "tools/easylogging++.h" #include "model/ranksvmtn.h" #include "tools/fileDataProvider.h" #include "model/rankaccu.h" INITIALIZE_EASYLOGGINGPP using namespace Eigen; namespace po = boost::program_options; po::variables_map vm; typedef int (*mainFunc)(DataProvider &dp); int train(DataProvider &dp) { RSVM *rsvm; rsvm = RSVM::loadModel(vm["model"].as<std::string>()); dp.open(); DataList D; LOG(INFO)<<"Training started"; dp.getDataSet(D); LOG(INFO)<<"Read "<<D.getSize()<<" entries with "<< D.getfSize()<<" features"; rsvm->train(D); std::vector<double> L; rsvm->predict(D,L); rank_accu(D,L); LOG(INFO)<<"Training finished,saving model"; dp.close(); rsvm->saveModel(vm["output"].as<std::string>().c_str()); delete rsvm; return 0; } int predict(DataProvider &dp) { RSVM *rsvm; rsvm = RSVM::loadModel(vm["model"].as<std::string>().c_str()); dp.open(); DataList D; std::vector<double> L; LOG(INFO)<<"Prediction started"; dp.getDataSet(D); LOG(INFO)<<"Read "<<D.getSize()<<" entries with "<< D.getfSize()<<" features"; rsvm->predict(D,L); if (vm.count("validate")) { rank_accu(D,L); } if (vm.count("output")) { LOG(INFO)<<"Finished,saving prediction"; std::ofstream fout(vm["output"].as<std::string>().c_str()); for (int i=0; i<L.size();++i) fout<<L[i]<<std::endl; fout.close(); } else if (!vm.count("validate")) { LOG(INFO)<<"Finished"; for (int i=0; i<L.size();++i) std::cout<<L[i]<<std::endl; } dp.close(); delete rsvm; return 0; } int main(int argc, char **argv) { // Defining program options po::options_description desc("Allowed options"); desc.add_options() ("help,h", "produce help message") ("train,T", "training model") ("validate,V", "validate model") ("predict,P", "use model for prediction") ("model,m", po::value<std::string>(), "set input model file") ("output,o", po::value<std::string>(), "set output model/prediction file") ("feature,i", po::value<std::string>(), "set input feature file"); // Parsing program options po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); // Print help if necessary if (vm.count("help") || !(vm.count("train") || vm.count("validate") || vm.count("predict"))) { std::cout << desc; return 0; } mainFunc mainf; if (vm.count("train")) { mainf = &train; } else if (vm.count("validate")||vm.count("predict")) { mainf = &predict; } else return 0; DataProvider* dp; dp = new FileDP(vm["feature"].as<std::string>()); mainf(*dp); delete dp; return 0; }