void svm_train_and_test(const std::vector<feature_t> &feats, const std::vector<int> &labels, const char * model_file, std::ofstream &ofs) { svm_model * model; if(_access(model_file, 0) == -1) { auto param = svm_fill_parameter(); auto prob = svm_fill_problem(feats, labels); model = svm_train(prob, param); svm_save_model(model_file, model); auto acc = svm_test_acc(prob, model); svm_destroy_param(param); svm_free_problem(prob); std::cout<<model_file<<" acc: "<<acc*100<<std::endl; ofs<<model_file<<" acc: "<<acc*100<<std::endl; } else { model = svm_load_model(model_file); auto acc = svm_test_acc(feats, labels, model); std::cout<<model_file<<" acc: "<<acc*100<<std::endl; ofs<<model_file<<" acc: "<<acc*100<<std::endl; } //free svm_free_and_destroy_model(&model); }
int main(int argc, char **argv) { char input_file_name[1024]; char model_file_name[1024]; parse_command_line(argc, argv, input_file_name, model_file_name); /* // TODO in distant future const char *error_msg; error_msg = llsvm_check_parameter(&prob,¶m); if(error_msg) { fprintf(stderr,"ERROR: %s\n",error_msg); exit(1); } */ // llsvm will save its model by itself trainLLSVM (input_file_name, model_file_name, param.scale, param.kMeansperLabel, param.kNN, param.svmIterations, param.distanceCoefficient, param.jointClustering); svm_destroy_param(¶m); free(line); return 0; }
bool SvmClassifier::Train(const Mat &feats, const vector<int> &labels) { if (svm_model_ != NULL) { svm_free_and_destroy_model(&svm_model_); } // Calculate the normalization parameters TrainNormalize(feats, &normA_, &normB_); // Normalize the features Mat feats_norm; Normalize(feats, &feats_norm); // Prepare the input for SVM svm_parameter param; svm_problem problem; svm_node* x_space = NULL; PrepareParameter(feats_norm.cols, ¶m); PrepareProblem(feats_norm, labels, &problem, x_space); // Train the SVM model svm_set_print_string_function(&PrintNull); // Close the training output svm_model_ = svm_train(&problem, ¶m); // Release the parameters for training svm_destroy_param(¶m); return true; }
int main(int argc, char **argv) { char input_file_name[1024]; char model_file_name[1024]; const char *error_msg; parse_command_line(argc, argv, input_file_name, model_file_name); read_problem(input_file_name); error_msg = svm_check_parameter(&prob,¶m); if(error_msg) { fprintf(stderr,"Error: %s\n",error_msg); exit(1); } if(cross_validation) { do_cross_validation(); } else { model = svm_train(&prob,¶m); svm_save_model(model_file_name,model); svm_destroy_model(model); } svm_destroy_param(¶m); free(prob.y); free(prob.x); free(x_space); return 0; }
void LibSVMRunner::arma_prediction(SVMConfiguration& config) { struct svm_model* m; struct svm_node ** train; svm_parameter *params; int training_examples = config.getDataExamplesNumber(); params = configuration_to_problem(config); m = load_model_from_config(config, params); // TODO: READ MODEL FROM PARAMETERS if(config.isSparse()) { train = ArmaSpMatToSvmNode(config.sparse_data); } else { train = armatlib(config.data); } double* ret = Malloc(double, training_examples); for (int i = 0; i < training_examples; i++) ret[i] = svm_predict(m, train[i],config.log); arma::vec ret_vec(ret, training_examples); config.result = ret_vec; /* TODO: CLEAN MEMORY IN BETTER WAY THINK OF OTHER PARAMETERS * Clean memory: * -array matrix * -model */ for (int i = 0; i < training_examples; i++) free(train[i]); free(train); //TODO: THIS SHOULD WORK WITH PREDICTIONS 2X, now it's not working // svm_free_and_destroy_model(&m); svm_destroy_param(params,config.log); free(ret); }
libSVMWrapper::~libSVMWrapper() { // destroy everything svm_free_and_destroy_model(&libSVM_Model); svm_destroy_param(&libSVM_Parameter); free(libSVM_Problem.y); free(libSVM_Problem.x); free(libSVM_x_space); }
//destructor ML2::~ML2(){ if(model!=NULL){ //free pointers and space svm_free_and_destroy_model(&model); } svm_destroy_param(¶m); free(prob.y); free(prob.x); free(x_space); free(line); }
SVModel::~SVModel(){ if (assegnato){ delete [] prob.x; delete [] prob.y; delete [] x_space; } if (addestrato){ delete [] model; } if (assegnatoParam) svm_destroy_param(¶m); }
double svm_train_and_test(const std::vector<feature_t> &train_feats, const std::vector<int> &train_labels, const std::vector<feature_t> &test_feats, const std::vector<int> &test_labels, const char * model_file) { svm_model * model; auto param = svm_fill_parameter(); auto prob = svm_fill_problem(train_feats, train_labels); model = svm_train(prob, param); svm_save_model(model_file, model); auto acc = svm_test_acc(test_feats, test_labels, model); svm_destroy_param(param); svm_free_problem(prob); //free svm_free_and_destroy_model(&model); return acc; }
void UseSVM_CleanUp(svm_model* &model,svm_parameter& param,svm_problem& prob,svm_node* &x_space) { delete[] prob.y; prob.y = NULL; delete[] prob.x; prob.x = NULL; delete[] x_space; x_space=NULL; prob.l = 0; if(model) { svm_destroy_model(model); model = NULL; } svm_destroy_param(¶m); }
int main(int argc, char **argv) { char input_file_name[1024]; char model_file_name[1024]; const char *error_msg; parse_command_line(argc, argv, input_file_name, model_file_name); read_problem(input_file_name); #ifdef CUSTOM_SOLVER if (param.svm_type == ONE_CLASS) { param.strong_footlier_indexes = filter_strong_footliers(input_file_name); } #endif error_msg = svm_check_parameter(&prob,¶m); if(error_msg) { fprintf(stderr,"Error: %s\n",error_msg); exit(1); } if(cross_validation) { if(param.svm_type == R2 || param.svm_type == R2q) fprintf(stderr, "\"R^2\" cannot do cross validation.\n"); else do_cross_validation(); } else { model = svm_train(&prob,¶m); if(param.svm_type == R2 || param.svm_type == R2q) fprintf(stderr, "\"R^2\" does not generate model.\n"); else if(svm_save_model(model_file_name,model)) { fprintf(stderr, "can't save model to file %s\n", model_file_name); exit(1); } svm_free_and_destroy_model(&model); } svm_destroy_param(¶m); free(prob.y); free(prob.x); free(x_space); free(line); return 0; }
int main(int argc, char **argv) { char input_file_name[1024]; char model_file_name[1024]; const char *error_msg; parse_command_line(argc, argv, input_file_name, model_file_name); read_problem(input_file_name); error_msg = svm_check_parameter(&prob,¶m); if(error_msg) { fprintf(stderr,"ERROR: %s\n",error_msg); exit(1); } if(cross_validation) { if (nr_fold <= 10) { do_cross_validation(); } else { double cv; nr_fold = nr_fold - 10; cv = binary_class_cross_validation(&prob, ¶m, nr_fold); printf("Cross Validation = %g%%\n",100.0*cv); } } else { model = svm_train(&prob,¶m); if(svm_save_model(model_file_name,model)) { fprintf(stderr, "can't save model to file %s\n", model_file_name); exit(1); } svm_free_and_destroy_model(&model); } svm_destroy_param(¶m); free(prob.y); free(prob.x); free(x_space); free(line); return 0; }
bool CmySvmArth::Train( char* path) { const char *error_msg; error_msg = svm_check_parameter(&prob,¶m); if(error_msg) { fprintf(stderr,"ERROR: %s\n",error_msg); free(prob.y); free(prob.x); free(x_space); free(line); line = NULL; x_space = NULL; return false; } if(cross_validation) { do_cross_validation(); } else { model = svm_train(&prob,¶m); if(path!=NULL&&svm_save_model(path,model)) { fprintf(stderr, "can't save model to file %s\n", path); return false; } if(path!=NULL) { svm_free_and_destroy_model(&model); } } svm_destroy_param(¶m); free(prob.y); free(prob.x); if(path!=NULL) { free(x_space); free(line); line = NULL; x_space = NULL; } return true; }
int main(int argc, char **argv) { char input_file_name[1024]; char model_file_name[1024]; const char *error_msg; parse_command_line(argc, argv, input_file_name, model_file_name); read_problem(input_file_name); error_msg = svm_check_parameter(&prob,¶m); if(error_msg) { fprintf(stderr,"ERROR: %s\n",error_msg); exit(1); } if(cross_validation) { do_cross_validation(); } else { model = svm_train(&prob,¶m); if(svm_save_model(model_file_name,model)) { fprintf(stderr, "can't save model to file %s\n", model_file_name); exit(1); } svm_free_and_destroy_model(&model); } svm_destroy_param(¶m); free(prob.y); #ifdef _DENSE_REP for (int i = 0; i < prob.l; ++i) free((prob.x+i)->values); #else free(x_space); #endif free(prob.x); free(line); return 0; }
int SVMTrainModel::train(double &RecRate, std::vector<int> &ConfusionTable) { if((!have_input_file_name) || (!have_model_file_name)) { fprintf(stderr,"ERROR: Set Input and Model files first!\n"); exit(1); } const char *error_msg; readProblem(input_file_name); error_msg = svm_check_parameter(&prob,¶m); if(error_msg) { fprintf(stderr,"ERROR: %s\n",error_msg); exit(1); } if(cross_validation) { do_cross_validation(RecRate,ConfusionTable); } else { model = svm_train(&prob,¶m); if(svm_save_model(model_file_name,model)) { fprintf(stderr, "can't save model to file %s\n", model_file_name); exit(1); } svm_free_and_destroy_model(&model); } svm_destroy_param(¶m); free(prob.y); free(prob.x); free(x_space); free(line); return 0; }
void SVM::train(char* pInputSampleFileName, char* OutputModelFilename, double &dRetTrainError, double &dRetCrossValError) { struct svm_parameter strSvmParameters; struct svm_problem strSvmProblem; struct svm_model *pstrSvmModel; const char *error_msg; double dCrossValError = -1; double dTrainError = -1; //set parameters this->setParameters(strSvmParameters); //read sample file this->read_problem(pInputSampleFileName, strSvmProblem, strSvmParameters); //check parameters error_msg = svm_check_parameter(&strSvmProblem, &strSvmParameters); //train model pstrSvmModel = svm_train(&strSvmProblem, &strSvmParameters); //do cross validation check dCrossValError = this->crossValidationSamples(strSvmProblem, strSvmParameters, 5); //save trained model svm_save_model(OutputModelFilename, pstrSvmModel); //test trained model with training set -> train error cout << "test model " << OutputModelFilename << " with the training set " << pInputSampleFileName << std::endl; this->test(pInputSampleFileName, OutputModelFilename, dTrainError); //clean up svm_destroy_model(pstrSvmModel); svm_destroy_param(&strSvmParameters); free(strSvmProblem.y); free(strSvmProblem.x); dRetTrainError = dTrainError; dRetCrossValError = dCrossValError; }
int svmtrain(int argc, char **argv) { char input_file_name[1024]; char model_file_name[1024]; const char *error_msg; parse_command_line(argc, argv, input_file_name, model_file_name); read_problem(input_file_name); error_msg = svm_check_parameter(&prob,¶m); if(error_msg) { LOGD("ERROR: %s\n",error_msg); exit(1); } if(cross_validation) { do_cross_validation(); } else { modelt = svm_train(&prob,¶m); if(svm_save_model(model_file_name,modelt)) { LOGD("can't save model to file %s\n", model_file_name); exit(1); } svm_free_and_destroy_model(&modelt); } svm_destroy_param(¶m); free(prob.y); free(prob.x); free(x_space); free(line); return 0; }
// Interface function of matlab // now assume prhs[0]: label prhs[1]: features void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { const char *error_msg; // fix random seed to have same results for each run // (for cross validation and probability estimation) srand(1); if(nlhs > 1) { exit_with_help(); fake_answer(nlhs, plhs); return; } // Transform the input Matrix to libsvm format if(nrhs > 1 && nrhs < 4) { int err; if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) { mexPrintf("Error: label vector and instance matrix must be double\n"); fake_answer(nlhs, plhs); return; } if(parse_command_line(nrhs, prhs, NULL)) { exit_with_help(); svm_destroy_param(¶m); fake_answer(nlhs, plhs); return; } if(mxIsSparse(prhs[1])) { if(param.kernel_type == PRECOMPUTED) { // precomputed kernel requires dense matrix, so we make one mxArray *rhs[1], *lhs[1]; rhs[0] = mxDuplicateArray(prhs[1]); if(mexCallMATLAB(1, lhs, 1, rhs, "full")) { mexPrintf("Error: cannot generate a full training instance matrix\n"); svm_destroy_param(¶m); fake_answer(nlhs, plhs); return; } err = read_problem_dense(prhs[0], lhs[0]); mxDestroyArray(lhs[0]); mxDestroyArray(rhs[0]); } else err = read_problem_sparse(prhs[0], prhs[1]); } else err = read_problem_dense(prhs[0], prhs[1]); // svmtrain's original code error_msg = svm_check_parameter(&prob, ¶m); if(err || error_msg) { if (error_msg != NULL) mexPrintf("Error: %s\n", error_msg); svm_destroy_param(¶m); free(prob.y); free(prob.x); free(x_space); fake_answer(nlhs, plhs); return; } if(cross_validation) { double *ptr; plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL); ptr = mxGetPr(plhs[0]); ptr[0] = do_cross_validation(); } else { int nr_feat = (int)mxGetN(prhs[1]); const char *error_msg; model = svm_train(&prob, ¶m); error_msg = model_to_matlab_structure(plhs, nr_feat, model); if(error_msg) mexPrintf("Error: can't convert libsvm model to matrix structure: %s\n", error_msg); svm_free_and_destroy_model(&model); } svm_destroy_param(¶m); free(prob.y); free(prob.x); free(x_space); } else { exit_with_help(); fake_answer(nlhs, plhs); return; } }
bool LibSVMRunner::save_model_to_config(SVMConfiguration& config, svm_parameter* param, svm_problem& problem) { const char *error_msg; error_msg = svm_check_parameter(&prob, param, config.log); if (error_msg) { LOG(config.log, LogLevel::ERR_LEVEL, "ERROR: " + to_string(error_msg)) return false; } //int* nr = Malloc(int, 1); int* nclasses = Malloc(int, 1); model = svm_train(&prob, param, config.log); //*nr = config.support_vectors.n_rows; //support vectors *nclasses = model->nr_class; config.nr_class = model->nr_class; LOG(config.log, LogLevel::TRACE_LEVEL, "save_model_to_config writing down alphas, nclasses= " + svm_to_str(config.nr_class)); int nr_support_vectors = model->l; //conversion vec->SpCol arma::vec alpha_y_tmp = arma::vec(model->sv_coef[0], nr_support_vectors); //not my fault. Arma fault :) config.alpha_y = arma::zeros(nr_support_vectors); for(int i=0;i<nr_support_vectors;++i){ if(alpha_y_tmp(i) != 0){ config.alpha_y(i) = alpha_y_tmp(i); } } if(config.nr_class != 2) { throw std::invalid_argument( "Code is not implemented for more than 2 classes right now"); } config.b = -model->rho[0]; config.iter = model->iter; // memcpy(config.rho, , // config.nr_class * (config.nr_class - 1) / 2 * sizeof(double)); //config.sv_indices = (int*) malloc(config.l * sizeof(int)); //svm_get_sv_indices(model, config.sv_indices, config.log); int dim = config.getDataDim(); ASSERT(dim > 0); //config.support_vectors = SvmUtils::libtoarma(model->SV, nr_support_vectors, dim); // LOG(config.log, LogLevel::TRACE_LEVEL, "save_model_to_config writing down SV, n_SV = " + svm_to_str(nr_support_vectors)); config.support_vectors = SvmUtils::SvmNodeToArmaSpMat(model->SV, nr_support_vectors, dim); LOG(config.log, LogLevel::TRACE_LEVEL, "save_model_to_config wrote down SV, n_SV = " + svm_to_str(config.support_vectors.n_cols)); LOG(config.log, LogLevel::TRACE_LEVEL, "save_model_to_config wrote down SV, dim = " + svm_to_str(config.support_vectors.n_rows)); // TODO: WTF!!!!!??? if (config.svm_type < 2) { config.label = (int *) malloc(*nclasses * sizeof(int)); config.nSV = (int *) malloc(*nclasses * sizeof(int)); memcpy(config.label, model->label, *nclasses * sizeof(int)); memcpy(config.nSV, model->nSV, *nclasses * sizeof(int)); } config.neg_target = model->label[1]; config.pos_target = model->label[0]; svm_destroy_param(param,config.log); svm_free_and_destroy_model(&model,config.log); return true; }
SVMParameter::~SVMParameter(){ svm_destroy_param(this); }
int main(int argc, char** argv) { struct svm_parameter param; // set by parse_command_line struct svm_problem prob; // set by read_problem // Instantiate a ModelManager: ModelManager manager("Test SVM"); // Parse command-line: if (manager.parseCommandLine((const int)argc, (const char**)argv, "", 0, 0) == false) return(1); manager.start(); //default paramaters param.svm_type = C_SVC; param.kernel_type = RBF; param.degree = 3; param.gamma = 0; // 1/k param.coef0 = 0; param.nu = 0.5; param.cache_size = 100; param.C = 1; param.eps = 1e-3; param.p = 0.1; param.shrinking = 1; param.probability = 0; param.nr_weight = 0; param.weight_label = NULL; param.weight = NULL; read_problem("tests/train.1.scale", prob, param); struct svm_model *model = svm_train(&prob,¶m); //if((model=svm_load_model(argv[i+1]))==0) //{ // fprintf(stderr,"can't open model file %s\n",argv[i+1]); // exit(1); //} //svm_save_model(model_file_name,model); //svm_destroy_model(model); //predict LINFO("Predicting"); predict("tests/test.1.scale", model); LINFO("Done"); svm_destroy_param(¶m); free(prob.y); free(prob.x); //free(x_space); // stop all our ModelComponents manager.stop(); // all done! return 0; }
/* @return -> cross validation error * -1 -> could not open statistic_filename * */ int SVM::crossValidationParameters(char* pSampleFilename, char* pFixedTestFilename, char* pStatisticsFilename) { struct svm_parameter strSvmParameters; struct svm_problem strSvmProblem; struct svm_model* pstrSvmModel; const char *error_msg; double dCrossValError = -1; double dFixTestSetError = -1; double dTrainError = -1; ofstream outputStatisticFile; char *pTempModelFilename = "tempSvmModel.tmp"; double dGamma = -1; double dC = -1; char tempText[1000]; //open statistic file outputStatisticFile.open(pStatisticsFilename, std::ios::trunc); if(!outputStatisticFile.is_open()) { cout << "can not open statistic file " << pStatisticsFilename << std::endl; return -1; } //initial parameters this->setParameters(strSvmParameters); this->read_problem(pSampleFilename, strSvmProblem, strSvmParameters); error_msg = svm_check_parameter(&strSvmProblem, &strSvmParameters); outputStatisticFile << "\% " << pSampleFilename << std::endl; outputStatisticFile << "\% Gamma \t C \t TrainError \t CrossValError \t FixedTestError" << std::endl; cout << "\% Gamma \t C \t TrainError \t CrossValError \t FixedTestError" << std::endl; for(int iExponentGamma = -25; iExponentGamma <= -2; ++iExponentGamma) { dGamma = pow(2.0, iExponentGamma); strSvmParameters.gamma = dGamma; for(int iExponentC = -1; iExponentC <= 15; ++iExponentC) { dC = pow(2.0, iExponentC); strSvmParameters.C = dC; //train pstrSvmModel = svm_train(&strSvmProblem, &strSvmParameters); svm_save_model(pTempModelFilename, pstrSvmModel); svm_destroy_model(pstrSvmModel); //test with train-set this->test(pSampleFilename, pTempModelFilename, dTrainError); //crossval dCrossValError = this->crossValidationSamples(strSvmProblem, strSvmParameters, 5); //test with fixed test-set this->test(pFixedTestFilename, pTempModelFilename, dFixTestSetError); remove(pTempModelFilename); sprintf(tempText, "%.15lf\t%10lf\t%10lf\t%10lf\t%10lf", dGamma, dC, dTrainError, dCrossValError, dFixTestSetError); outputStatisticFile << tempText << std::endl; cout << "\n#######################################################################" << std::endl; cout << tempText << std::endl; cout << "#######################################################################\n" << std::endl; } } //clean up outputStatisticFile.close(); svm_destroy_param(&strSvmParameters); free(strSvmProblem.y); free(strSvmProblem.x); //free(pTempModelFilename); return 0; }
int main(int argc, char **argv) { #ifdef WIN32 // Send all reports to STDOUT _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE ); _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT ); _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE ); _CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDOUT ); _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE ); _CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDOUT ); // enable the options SET_CRT_DEBUG_FIELD( _CRTDBG_DELAY_FREE_MEM_DF ); SET_CRT_DEBUG_FIELD( _CRTDBG_LEAK_CHECK_DF ); #endif printf("int %d, short int %d, char %d, double %d, float %d, node %d\n",sizeof(int),sizeof(short int), sizeof(char), sizeof(double), sizeof(float), sizeof(svm_node)); char input_file_name[FILENAME_LEN]; char model_file_name[FILENAME_LEN]; const char *error_msg; parse_command_line(argc, argv, input_file_name, model_file_name); read_problem(input_file_name); param.modelFile = model_file_name; printf ("Finish reading input files!\n"); error_msg = svm_check_parameter(&prob,¶m); #ifdef WIN32 assert(_CrtCheckMemory()); #endif if(error_msg) { fprintf(stderr,"Error: %s\n",error_msg); exit(1); } double duration; double start = getRunTime(); if(cross_validation) { do_cross_validation(); } else { printf("kernel: %d\n",param.kernel_type); model = svm_train(&prob,¶m); double finish = getRunTime(); duration = (double)(finish - start); #ifdef WIN32 assert(_CrtCheckMemory()); #endif svm_save_model(model_file_name,model); svm_destroy_model(model); } printf("CPU Time = %f second\n", duration); FILE* fModel = fopen(model_file_name, "a+t"); // append mode fprintf(fModel, "CPU Time = %f second\n", duration); fclose(fModel); svm_destroy_param(¶m); free(prob.y); free(prob.x); free(x_space); #ifdef WIN32 assert(_CrtCheckMemory()); #endif return 0; }
SVM::~SVM() { svm_free_and_destroy_model(&model); svm_destroy_param(¶m); }
void ParameterDeleter::operator()(struct svm_parameter *param) const { svm_destroy_param(param); delete param; }
int svmTrain (double gamma, std::vector<std::vector<svm_node> > data, std::vector<int> labels) { struct svm_model* model; struct svm_problem prob; struct svm_node* x_space; struct svm_parameter param; // Make sure we have data if (data.empty ()) { CORE_ERROR ("No training data\n"); return (-1); } prob.l = data.size (); prob.y = Malloc (double, prob.l); prob.x = Malloc (struct svm_node* , prob.l); x_space = Malloc (struct svm_node, data.size () * data[0].size ()); int j = 0; for (int i = 0; i < prob.l; ++i) { prob.y[i] = *(labels.begin () + i); prob.x[i] = &x_space[j]; for (std::vector<svm_node>::iterator it = data[i].begin (); it != data[i].end (); ++it) { x_space[j].index = it->index; x_space[j].value = it->value; ++j; } } param.svm_type = C_SVC; param.kernel_type = RBF; param.gamma = gamma; param.degree = 3; param.coef0 = 0; param.nu = 0.5; param.cache_size = 100; param.C = 1; param.eps = 1e-3; param.p = 0.1; param.shrinking = 1; param.probability = 0; param.nr_weight = 0; param.weight_label = NULL; param.weight = NULL; // Check whether the parameters are within a feasible range of the problem const char* error_msg = svm_check_parameter (&prob, ¶m); if (error_msg) { CORE_ERROR ("Error checking SVM parameters: %s\n", error_msg); return (-1); } model = svm_train (&prob, ¶m); if (svm_save_model ("model.txt", model)) { CORE_ERROR ("Save SVM model failed\n"); return (-1); } svm_free_and_destroy_model (&model); svm_destroy_param (¶m); free (prob.y); free (prob.x); free (x_space); return (0); }
/* @return: 0: reading of input-file successful * -1: error during opening input-file * -2: format error in input-file */ int SVM::read_problem(const char *filename, struct svm_problem &strSvmProblem, struct svm_parameter &strParameters) { int elements, max_index, inst_max_index, i, j; int max_line_len; FILE *fp = fopen(filename,"r"); char *endptr; char *idx, *val, *label; char *line = NULL; if(fp == NULL) { fprintf(stderr,"can't open input file %s\n",filename); return -1; } strSvmProblem.l = 0; elements = 0; max_line_len = 1024; while((line = this->read_line(fp, max_line_len)) !=NULL) { char *p = strtok(line," \t"); // label // features while(1) { p = strtok(NULL," \t"); if(p == NULL || *p == '\n') // check '\n' as ' ' may be after the last feature break; ++elements; } ++elements; ++strSvmProblem.l; free(line); } rewind(fp); strSvmProblem.y = Malloc(double,strSvmProblem.l); strSvmProblem.x = Malloc(struct svm_node *,strSvmProblem.l); this->_svmNodeXSpace = Malloc(struct svm_node,elements); max_index = 0; j=0; for(i=0;i<strSvmProblem.l;i++) { inst_max_index = -1; // strtol gives 0 if wrong format, and precomputed kernel has <index> start from 0 line = this->read_line(fp,1024); strSvmProblem.x[i] = &this->_svmNodeXSpace[j]; label = strtok(line," \t"); strSvmProblem.y[i] = strtod(label,&endptr); if(endptr == label) { fprintf(stderr,"Wrong input format at line %d\n", i+1); return -2; } while(1) { idx = strtok(NULL,":"); val = strtok(NULL," \t"); if(val == NULL) break; errno = 0; this->_svmNodeXSpace[j].index = (int) strtol(idx,&endptr,10); if(endptr == idx || errno != 0 || *endptr != '\0' || this->_svmNodeXSpace[j].index <= inst_max_index) { fprintf(stderr,"Wrong input format at line %d\n", i+1); return -2; } else inst_max_index = this->_svmNodeXSpace[j].index; errno = 0; this->_svmNodeXSpace[j].value = strtod(val,&endptr); if(endptr == val || errno != 0 || (*endptr != '\0' && !isspace(*endptr))) { fprintf(stderr,"Wrong input format at line %d\n", i+1); return -2; } ++j; } if(inst_max_index > max_index) max_index = inst_max_index; this->_svmNodeXSpace[j++].index = -1; free(line); } if(strParameters.gamma == 0 && max_index > 0) strParameters.gamma = 1.0/max_index; if(strParameters.kernel_type == PRECOMPUTED) for(i=0;i<strSvmProblem.l;i++) { if (strSvmProblem.x[i][0].index != 0) { fprintf(stderr,"Wrong input format: first column must be 0:sample_serial_number\n"); return -2; } if ((int)strSvmProblem.x[i][0].value <= 0 || (int)strSvmProblem.x[i][0].value > max_index) { fprintf(stderr,"Wrong input format: sample_serial_number out of range\n"); return -2; } } fclose(fp); svm_destroy_param(&strParameters); return 0; }
/********************************************** do the prediction based on correlation and a variation of the numbers of top voxels input: the array of the numbers of top voxels, the number of subjects, the number of blocks, the blocks, the number of test samples, the raw activation matrix array, quiet mode output: the results are displayed on the screen ***********************************************/ void CorrelationBasedClassification(int* tops, int ntops, int nSubs, int nTrials, Trial* trials, int nTests, RawMatrix** r_matrices, int is_quiet_mode) { int i, j, k; int col = r_matrices[0]->col; float* simMatrix = new float[nTrials*nTrials]; for (i=0; i<ntops; i++) { //float* simMatrix = GetInnerSimMatrix(tops[i], col, nSubs, nTrials, trials, r_matrices); for (j=0; j<nTrials*nTrials; j++) simMatrix[j] = 0.0; int sr = 0, rowLength = 100; while (sr<tops[i]) { if (rowLength >= tops[i] - sr) { rowLength = tops[i] - sr; } float* tempSimMatrix = GetPartialInnerSimMatrix(tops[i], col, nSubs, nTrials, sr, rowLength, trials, r_matrices); for (j=0; j<nTrials*nTrials; j++) simMatrix[j] += tempSimMatrix[j]; //cout<<i<<" "<<sr<<" "<<tempSimMatrix[0]<<" "<<tempSimMatrix[1]<<endl; delete[] tempSimMatrix; sr += rowLength; } SVMParameter* param = SetSVMParameter(4); // precomputed SVMProblem* prob = GetSVMTrainingSet(simMatrix, nTrials, trials, nTrials-nTests); struct svm_model *model = svm_train(prob, param); int nTrainings = nTrials-nTests; SVMNode* x = new SVMNode[nTrainings+2]; int result = 0; double predict_distances[nTrials-nTrainings]; bool predict_correctness[nTrials-nTrainings]; for (j=nTrainings; j<nTrials; j++) { x[0].index = 0; x[0].value = j-nTrainings+1; for (k=0; k<nTrainings; k++) { x[k+1].index = k+1; x[k+1].value = simMatrix[j*nTrials+k]; } x[k+1].index = -1; predict_distances[j-nTrainings] = svm_predict_distance(model, x); int predict_label = predict_distances[j-nTrainings]>0?0:1; if (trials[j].label == predict_label) { result++; predict_correctness[j-nTrainings] = true; } else { predict_correctness[j-nTrainings] = false; } } cout<<tops[i]<<": "<<result<<"/"<<nTrials-nTrainings<<"="<<result*1.0/(nTrials-nTrainings)<<endl; if (!is_quiet_mode) { cout<<"blocking testing confidence:"<<endl; for (j=nTrainings; j<nTrials; j++) { cout<<fabs(predict_distances[j-nTrainings])<<" ("; if (predict_correctness[j-nTrainings]) { cout<<"Correct) "; } else { cout<<"Incorrect) "; } } cout<<endl; } svm_free_and_destroy_model(&model); delete[] x; delete[] prob->y; for (j=0; j<nTrainings; j++) { delete prob->x[j]; } delete[] prob->x; delete prob; svm_destroy_param(param); } delete simMatrix; }
/********************************************** do the prediction based on activation and a variation of the numbers of top voxels input: the array of the numbers of top voxels, the number of blocks, the blocks, the number of test samples, and the raw activation matrix array, quiet mode output: the results are displayed on the screen ***********************************************/ void ActivationBasedClassification(int* tops, int ntops, int nTrials, Trial* trials, int nTests, RawMatrix** avg_matrices, int is_quiet_mode) { int i, j, k; int nTrainings = nTrials-nTests; SVMParameter* param = SetSVMParameter(0); // linear SVMProblem* prob = new SVMProblem(); prob->l = nTrainings; prob->y = new double[nTrainings]; prob->x = new SVMNode*[nTrainings]; for (i=0; i<ntops; i++) { for (j=0; j<nTrainings; j++) { int sid = trials[j].sid; prob->y[j] = trials[j].label; prob->x[j] = new SVMNode[tops[i]+1]; for (k=0; k<tops[i]; k++) { prob->x[j][k].index = k+1; int col = avg_matrices[sid]->col; int offset = trials[j].tid_withinsubj; prob->x[j][k].value = avg_matrices[sid]->matrix[k*col+offset]; } prob->x[j][k].index = -1; } struct svm_model *model = svm_train(prob, param); SVMNode* x = new SVMNode[tops[i]+1]; int result = 0; double predict_distances[nTrials-nTrainings]; bool predict_correctness[nTrials-nTrainings]; for (j=nTrainings; j<nTrials; j++) { int sid = trials[j].sid; for (k=0; k<tops[i]; k++) { x[k].index = k+1; int col = avg_matrices[sid]->col; int offset = trials[j].tid_withinsubj; x[k].value = avg_matrices[sid]->matrix[k*col+offset]; } x[k].index = -1; predict_distances[j-nTrainings] = svm_predict_distance(model, x); int predict_label = predict_distances[j-nTrainings]>0?0:1; if (trials[j].label == predict_label) { result++; predict_correctness[j-nTrainings] = true; } else { predict_correctness[j-nTrainings] = false; } } cout<<tops[i]<<": "<<result<<"/"<<nTrials-nTrainings<<"="<<result*1.0/(nTrials-nTrainings)<<endl; if (!is_quiet_mode) { cout<<"blocking testing confidence:"<<endl; for (j=nTrainings; j<nTrials; j++) { cout<<fabs(predict_distances[j-nTrainings])<<" ("; if (predict_correctness[j-nTrainings]) { cout<<"Correct) "; } else { cout<<"Incorrect) "; } } cout<<endl; } svm_free_and_destroy_model(&model); delete[] x; } delete prob->y; for (j=0; j<nTrainings; j++) { delete prob->x[j]; } delete prob->x; delete prob; svm_destroy_param(param); }
MarginActiveLearningSVM::~MarginActiveLearningSVM() { svm_destroy_param(¶m); free(weight); };