void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { struct svm_model *model; char *filename; const char *error_msg; int status; /* check input */ if(nrhs != 2) { mexPrintf("Usage: svm_savemodel(model, 'filename');\n"); fake_answer(plhs); return; } if(!mxIsStruct(prhs[0])) { mexPrintf("model file should be a struct array\n"); fake_answer(plhs); return; } if(!mxIsChar(prhs[1]) || mxGetM(prhs[1])!=1) { mexPrintf("filename should be given as char(s)\n"); fake_answer(plhs); return; } /* convert MATLAB struct to C struct */ model = matlab_matrix_to_model(prhs[0], &error_msg); if(model == NULL) { mexPrintf("Error: can't read model: %s\n", error_msg); fake_answer(plhs); return; } /* get filename */ filename = mxArrayToString(prhs[1]); /* save model to file */ status = svm_save_model(filename,model); if (status != 0) { mexWarnMsgTxt("Error occured while writing to file."); } /* destroy model */ svm_free_and_destroy_model(&model); mxFree(filename); /* return status value (0: success, -1: failure) */ plhs[0] = mxCreateDoubleScalar(status); return; }
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { struct svm_model *model; info = &mexPrintf; if(nlhs > 2 || nrhs != 2) { exit_with_help(); fake_answer(nlhs, plhs); return; } if(!mxIsDouble(prhs[0])) { mexPrintf("Error: label vector and instance matrix must be double\n"); fake_answer(nlhs, plhs); return; } uint64_t *address = (uint64_t *)mxGetPr(prhs[1]); model = (struct svm_model *)(*address); // struct timeval start,stop; // gettimeofday(&start,NULL); predict(nlhs, plhs, prhs, model); // gettimeofday(&stop,NULL); // mexPrintf("%lu\n",stop.tv_usec - start.tv_usec); plhs[1] = mxCreateDoubleMatrix(1, model->nr_class, mxREAL); double *ptr = mxGetPr(plhs[1]); for(int i = 0; i < model->nr_class; i++) ptr[i] = model->label[i]; return; }
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { if(nrhs == 1) { char filename[256]; mxGetString(prhs[0], filename, mxGetN(prhs[0]) + 1); if(filename == NULL) { mexPrintf("Error: filename is NULL\n"); return; } read_problem(filename, plhs); } else { exit_with_help(); fake_answer(plhs); return; } }
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { if(nlhs > 0) { exit_with_help(); fake_answer(nlhs, plhs); return; } // Transform the input Matrix to libsvm format if(nrhs == 3) { char filename[256]; if(!mxIsDouble(prhs[1]) || !mxIsDouble(prhs[2])) { mexPrintf("Error: label vector and instance matrix must be double\n"); return; } mxGetString(prhs[0], filename, mxGetN(prhs[0])+1); if(mxIsSparse(prhs[2])) libsvmwrite(filename, prhs[1], prhs[2]); else { mexPrintf("Instance_matrix must be sparse\n"); return; } } else { exit_with_help(); return; } }
// 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; } }
void do_predict(mxArray *plhs[], const mxArray *prhs[], struct model *model_, const int predict_probability_flag) { int label_vector_row_num, label_vector_col_num; int feature_number, testing_instance_number; int instance_index; double *ptr_label, *ptr_predict_label; double *ptr_prob_estimates, *ptr_dec_values, *ptr; struct feature_node *x; mxArray *pplhs[1]; // instance sparse matrix in row format int correct = 0; int total = 0; double error = 0; double sump = 0, sumt = 0, sumpp = 0, sumtt = 0, sumpt = 0; int nr_class=get_nr_class(model_); int nr_w; double *prob_estimates=NULL; if(nr_class==2 && model_->param.solver_type!=MCSVM_CS) nr_w=1; else nr_w=nr_class; // prhs[1] = testing instance matrix feature_number = get_nr_feature(model_); testing_instance_number = (int) mxGetM(prhs[1]); if(col_format_flag) { feature_number = (int) mxGetM(prhs[1]); testing_instance_number = (int) mxGetN(prhs[1]); } label_vector_row_num = (int) mxGetM(prhs[0]); label_vector_col_num = (int) mxGetN(prhs[0]); if(label_vector_row_num!=testing_instance_number) { mexPrintf("Length of label vector does not match # of instances.\n"); fake_answer(plhs); return; } if(label_vector_col_num!=1) { mexPrintf("label (1st argument) should be a vector (# of column is 1).\n"); fake_answer(plhs); return; } ptr_label = mxGetPr(prhs[0]); // transpose instance matrix if(col_format_flag) pplhs[0] = (mxArray *)prhs[1]; else { mxArray *pprhs[1]; pprhs[0] = mxDuplicateArray(prhs[1]); if(mexCallMATLAB(1, pplhs, 1, pprhs, "transpose")) { mexPrintf("Error: cannot transpose testing instance matrix\n"); fake_answer(plhs); return; } } prob_estimates = Malloc(double, nr_class); plhs[0] = mxCreateDoubleMatrix(testing_instance_number, 1, mxREAL); if(predict_probability_flag) plhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_class, mxREAL); else plhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_w, mxREAL); ptr_predict_label = mxGetPr(plhs[0]); ptr_prob_estimates = mxGetPr(plhs[2]); ptr_dec_values = mxGetPr(plhs[2]); x = Malloc(struct feature_node, feature_number+2); for(instance_index=0;instance_index<testing_instance_number;instance_index++) { int i; double target_label, predict_label; target_label = ptr_label[instance_index]; // prhs[1] and prhs[1]^T are sparse read_sparse_instance(pplhs[0], instance_index, x, feature_number, model_->bias); if(predict_probability_flag) { predict_label = predict_probability(model_, x, prob_estimates); ptr_predict_label[instance_index] = predict_label; for(i=0;i<nr_class;i++) ptr_prob_estimates[instance_index + i * testing_instance_number] = prob_estimates[i]; } else { double *dec_values = Malloc(double, nr_class); predict_label = predict_values(model_, x, dec_values); ptr_predict_label[instance_index] = predict_label; for(i=0;i<nr_w;i++) ptr_dec_values[instance_index + i * testing_instance_number] = dec_values[i]; free(dec_values); } if(predict_label == target_label) ++correct; error += (predict_label-target_label)*(predict_label-target_label); sump += predict_label; sumt += target_label; sumpp += predict_label*predict_label; sumtt += target_label*target_label; sumpt += predict_label*target_label; ++total; } if(model_->param.solver_type==L2R_L2LOSS_SVR || model_->param.solver_type==L2R_L1LOSS_SVR_DUAL || model_->param.solver_type==L2R_L2LOSS_SVR_DUAL) { mexPrintf("Mean squared error = %g (regression)\n",error/total); mexPrintf("Squared correlation coefficient = %g (regression)\n", ((total*sumpt-sump*sumt)*(total*sumpt-sump*sumt))/ ((total*sumpp-sump*sump)*(total*sumtt-sumt*sumt)) ); } //else //mexPrintf("Accuracy = %g%% (%d/%d)\n", (double) correct/total*100,correct,total); // return accuracy, mean squared error, squared correlation coefficient plhs[1] = mxCreateDoubleMatrix(3, 1, mxREAL); ptr = mxGetPr(plhs[1]); ptr[0] = (double)correct/total*100; ptr[1] = error/total; ptr[2] = ((total*sumpt-sump*sumt)*(total*sumpt-sump*sumt))/ ((total*sumpp-sump*sump)*(total*sumtt-sumt*sumt)); free(x); if(prob_estimates != NULL) free(prob_estimates); }
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { int prob_estimate_flag = 0; struct model *model_; char cmd[CMD_LEN]; col_format_flag = 0; if(nrhs > 5 || nrhs < 3) { exit_with_help(); fake_answer(plhs); return; } if(nrhs == 5) { mxGetString(prhs[4], cmd, mxGetN(prhs[4])+1); if(strcmp(cmd, "col") == 0) { col_format_flag = 1; } } if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) { mexPrintf("Error: label vector and instance matrix must be double\n"); fake_answer(plhs); return; } if(mxIsStruct(prhs[2])) { const char *error_msg; // parse options if(nrhs>=4) { int i, argc = 1; char *argv[CMD_LEN/2]; // put options in argv[] mxGetString(prhs[3], cmd, mxGetN(prhs[3]) + 1); if((argv[argc] = strtok(cmd, " ")) != NULL) while((argv[++argc] = strtok(NULL, " ")) != NULL) ; for(i=1;i<argc;i++) { if(argv[i][0] != '-') break; if(++i>=argc) { exit_with_help(); fake_answer(plhs); return; } switch(argv[i-1][1]) { case 'b': prob_estimate_flag = atoi(argv[i]); break; default: mexPrintf("unknown option\n"); exit_with_help(); fake_answer(plhs); return; } } } model_ = Malloc(struct model, 1); error_msg = matlab_matrix_to_model(model_, prhs[2]); if(error_msg) { mexPrintf("Error: can't read model: %s\n", error_msg); free_and_destroy_model(&model_); fake_answer(plhs); return; } if(prob_estimate_flag) { if(!check_probability_model(model_)) { mexPrintf("probability output is only supported for logistic regression\n"); prob_estimate_flag=0; } } if(mxIsSparse(prhs[1])) do_predict(plhs, prhs, model_, prob_estimate_flag); else { mexPrintf("Testing_instance_matrix must be sparse; " "use sparse(Testing_instance_matrix) first\n"); fake_answer(plhs); } // destroy model_ free_and_destroy_model(&model_); } else {
void predict(mxArray *plhs[], const mxArray *prhs[], struct svm_model *model, const int predict_probability) { int label_vector_row_num, label_vector_col_num; int feature_number, testing_instance_number; int instance_index; double *ptr_instance, *ptr_label, *ptr_predict_label; double *ptr_prob_estimates, *ptr_dec_values, *ptr; struct svm_node *x; mxArray *pplhs[1]; // transposed instance sparse matrix int correct = 0; int total = 0; double error = 0; double sump = 0, sumt = 0, sumpp = 0, sumtt = 0, sumpt = 0; int svm_type=svm_get_svm_type(model); int nr_class=svm_get_nr_class(model); double *prob_estimates=NULL; // prhs[1] = testing instance matrix feature_number = (int)mxGetN(prhs[1]); testing_instance_number = (int)mxGetM(prhs[1]); label_vector_row_num = (int)mxGetM(prhs[0]); label_vector_col_num = (int)mxGetN(prhs[0]); if(label_vector_row_num!=testing_instance_number) { mexPrintf("Length of label vector does not match # of instances.\n"); fake_answer(plhs); return; } if(label_vector_col_num!=1) { mexPrintf("label (1st argument) should be a vector (# of column is 1).\n"); fake_answer(plhs); return; } ptr_instance = mxGetPr(prhs[1]); ptr_label = mxGetPr(prhs[0]); // transpose instance matrix if(mxIsSparse(prhs[1])) { if(model->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 full testing instance matrix\n"); fake_answer(plhs); return; } ptr_instance = mxGetPr(lhs[0]); mxDestroyArray(rhs[0]); } else { mxArray *pprhs[1]; pprhs[0] = mxDuplicateArray(prhs[1]); if(mexCallMATLAB(1, pplhs, 1, pprhs, "transpose")) { mexPrintf("Error: cannot transpose testing instance matrix\n"); fake_answer(plhs); return; } } } if(predict_probability) { if(svm_type==NU_SVR || svm_type==EPSILON_SVR) mexPrintf("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=%g\n",svm_get_svr_probability(model)); else prob_estimates = (double *) malloc(nr_class*sizeof(double)); } plhs[0] = mxCreateDoubleMatrix(testing_instance_number, 1, mxREAL); if(predict_probability) { // prob estimates are in plhs[2] if(svm_type==C_SVC || svm_type==NU_SVC) plhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_class, mxREAL); else plhs[2] = mxCreateDoubleMatrix(0, 0, mxREAL); } else { // decision values are in plhs[2] if(svm_type == ONE_CLASS || svm_type == EPSILON_SVR || svm_type == NU_SVR) plhs[2] = mxCreateDoubleMatrix(testing_instance_number, 1, mxREAL); else plhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_class*(nr_class-1)/2, mxREAL); } ptr_predict_label = mxGetPr(plhs[0]); ptr_prob_estimates = mxGetPr(plhs[2]); ptr_dec_values = mxGetPr(plhs[2]); x = (struct svm_node*)malloc((feature_number+1)*sizeof(struct svm_node) ); for(instance_index=0;instance_index<testing_instance_number;instance_index++) { int i; double target_label, predict_label; target_label = ptr_label[instance_index]; if(mxIsSparse(prhs[1]) && model->param.kernel_type != PRECOMPUTED) // prhs[1]^T is still sparse read_sparse_instance(pplhs[0], instance_index, x); else { for(i=0;i<feature_number;i++) { x[i].index = i+1; x[i].value = ptr_instance[testing_instance_number*i+instance_index]; } x[feature_number].index = -1; } if(predict_probability) { if(svm_type==C_SVC || svm_type==NU_SVC) { predict_label = svm_predict_probability(model, x, prob_estimates); ptr_predict_label[instance_index] = predict_label; for(i=0;i<nr_class;i++) ptr_prob_estimates[instance_index + i * testing_instance_number] = prob_estimates[i]; } else { predict_label = svm_predict(model,x); ptr_predict_label[instance_index] = predict_label; } } else { predict_label = svm_predict(model,x); ptr_predict_label[instance_index] = predict_label; if(svm_type == ONE_CLASS || svm_type == EPSILON_SVR || svm_type == NU_SVR) { double res; svm_predict_values(model, x, &res); ptr_dec_values[instance_index] = res; } else { double *dec_values = (double *) malloc(sizeof(double) * nr_class*(nr_class-1)/2); svm_predict_values(model, x, dec_values); for(i=0;i<(nr_class*(nr_class-1))/2;i++) ptr_dec_values[instance_index + i * testing_instance_number] = dec_values[i]; free(dec_values); } } if(predict_label == target_label) ++correct; error += (predict_label-target_label)*(predict_label-target_label); sump += predict_label; sumt += target_label; sumpp += predict_label*predict_label; sumtt += target_label*target_label; sumpt += predict_label*target_label; ++total; } if(svm_type==NU_SVR || svm_type==EPSILON_SVR) { mexPrintf("Mean squared error = %g (regression)\n",error/total); mexPrintf("Squared correlation coefficient = %g (regression)\n", ((total*sumpt-sump*sumt)*(total*sumpt-sump*sumt))/ ((total*sumpp-sump*sump)*(total*sumtt-sumt*sumt)) ); } /* else */ /* mexPrintf("Accuracy = %g%% (%d/%d) (classification)\n", */ /* (double)correct/total*100,correct,total); */ // return accuracy, mean squared error, squared correlation coefficient plhs[1] = mxCreateDoubleMatrix(3, 1, mxREAL); ptr = mxGetPr(plhs[1]); ptr[0] = (double)correct/total*100; ptr[1] = error/total; ptr[2] = ((total*sumpt-sump*sumt)*(total*sumpt-sump*sumt))/ ((total*sumpp-sump*sump)*(total*sumtt-sumt*sumt)); free(x); if(prob_estimates != NULL) free(prob_estimates); }
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { int prob_estimate_flag = 0; struct svm_model *model; if(nrhs > 4 || nrhs < 3) { exit_with_help(); fake_answer(plhs); return; } if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) { mexPrintf("Error: label vector and instance matrix must be double\n"); fake_answer(plhs); return; } if(mxIsStruct(prhs[2])) { const char *error_msg; // parse options if(nrhs==4) { int i, argc = 1; char cmd[CMD_LEN], *argv[CMD_LEN/2]; // put options in argv[] mxGetString(prhs[3], cmd, mxGetN(prhs[3]) + 1); if((argv[argc] = strtok(cmd, " ")) != NULL) while((argv[++argc] = strtok(NULL, " ")) != NULL) ; for(i=1;i<argc;i++) { if(argv[i][0] != '-') break; if(++i>=argc) { exit_with_help(); fake_answer(plhs); return; } switch(argv[i-1][1]) { case 'b': prob_estimate_flag = atoi(argv[i]); break; default: mexPrintf("Unknown option: -%c\n", argv[i-1][1]); exit_with_help(); fake_answer(plhs); return; } } } model = matlab_matrix_to_model(prhs[2], &error_msg); if (model == NULL) { mexPrintf("Error: can't read model: %s\n", error_msg); fake_answer(plhs); return; } if(prob_estimate_flag) { if(svm_check_probability_model(model)==0) { mexPrintf("Model does not support probabiliy estimates\n"); fake_answer(plhs); svm_destroy_model(model); return; } } else { if(svm_check_probability_model(model)!=0) printf("Model supports probability estimates, but disabled in predicton.\n"); } predict(plhs, prhs, model, prob_estimate_flag); // destroy model svm_destroy_model(model); } else { mexPrintf("model file should be a struct array\n"); fake_answer(plhs); } return; }
void do_predict(mxArray *plhs[], const mxArray *prhs[], struct model *model_, const int predict_probability_flag) { int label_vector_row_num, label_vector_col_num; int feature_number, testing_instance_number; int instance_index; double *ptr_instance, *ptr_label, *ptr_predict_label; double *ptr_prob_estimates, *ptr_dec_values, *ptr; struct feature_node *x; mxArray *pplhs[1]; // instance sparse matrix in row format int correct = 0; int total = 0; int nr_class=get_nr_class(model_); int nr_classifier; double *prob_estimates=NULL; if(nr_class==2) nr_classifier=1; else nr_classifier=nr_class; // prhs[1] = testing instance matrix feature_number = mxGetN(prhs[1]); testing_instance_number = mxGetM(prhs[1]); if(col_format_flag) { feature_number = mxGetM(prhs[1]); testing_instance_number = mxGetN(prhs[1]); } label_vector_row_num = mxGetM(prhs[0]); label_vector_col_num = mxGetN(prhs[0]); if(label_vector_row_num!=testing_instance_number) { mexPrintf("Length of label vector does not match # of instances.\n"); fake_answer(plhs); return; } if(label_vector_col_num!=1) { mexPrintf("label (1st argument) should be a vector (# of column is 1).\n"); fake_answer(plhs); return; } ptr_instance = mxGetPr(prhs[1]); ptr_label = mxGetPr(prhs[0]); // transpose instance matrix if(mxIsSparse(prhs[1])) { if(col_format_flag) { pplhs[0] = (mxArray *)prhs[1]; } else { mxArray *pprhs[1]; pprhs[0] = mxDuplicateArray(prhs[1]); if(mexCallMATLAB(1, pplhs, 1, pprhs, "transpose")) { mexPrintf("Error: cannot transpose testing instance matrix\n"); fake_answer(plhs); return; } } } else mexPrintf("Testing_instance_matrix must be sparse\n"); prob_estimates = Malloc(double, nr_class); plhs[0] = mxCreateDoubleMatrix(testing_instance_number, 1, mxREAL); if(predict_probability_flag) plhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_class, mxREAL); else plhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_classifier, mxREAL); ptr_predict_label = mxGetPr(plhs[0]); ptr_prob_estimates = mxGetPr(plhs[2]); ptr_dec_values = mxGetPr(plhs[2]); x = Malloc(struct feature_node, feature_number+2); for(instance_index=0;instance_index<testing_instance_number;instance_index++) { int i; double target,v; target = ptr_label[instance_index]; // prhs[1] and prhs[1]^T are sparse read_sparse_instance(pplhs[0], instance_index, x, feature_number, model_->bias); if(predict_probability_flag) { v = predict_probability(model_, x, prob_estimates); ptr_predict_label[instance_index] = v; for(i=0;i<nr_class;i++) ptr_prob_estimates[instance_index + i * testing_instance_number] = prob_estimates[i]; } else { double *dec_values = Malloc(double, nr_class); v = predict(model_, x); ptr_predict_label[instance_index] = v; predict_values(model_, x, dec_values); for(i=0;i<nr_classifier;i++) ptr_dec_values[instance_index + i * testing_instance_number] = dec_values[i]; } if(v == target) ++correct; ++total; } mexPrintf("Accuracy = %g%% (%d/%d)\n", (double)correct/total*100,correct,total); // return accuracy, mean squared error, squared correlation coefficient plhs[1] = mxCreateDoubleMatrix(1, 1, mxREAL); ptr = mxGetPr(plhs[1]); ptr[0] = (double)correct/total*100; free(x); if(prob_estimates != NULL) free(prob_estimates); }
void predict(int nlhs, mxArray *plhs[], const mxArray *prhs[], const int predict_probability, const int verbose_flag, const int exact_flag, const int approx_type){ //read the data and get the parameters int label_vector_row_num, label_vector_col_num; int feat_dim, num_feats; Double *ptr_instance; Double *ptr_dec_values; fiksvm_exact_classifier *exact_classifier = NULL; fiksvm_approx_classifier *approx_classifier = NULL; feat_dim = mxGetN(prhs[1]); num_feats = mxGetM(prhs[1]); label_vector_row_num = mxGetM(prhs[0]); label_vector_col_num = mxGetN(prhs[0]); if(label_vector_row_num!=num_feats){ mexPrintf("# of labels (# of column in 1st argument) does not match # of instances (# of rows in 2nd argument).\n"); fake_answer(plhs); return; } if(label_vector_col_num!=1){ mexPrintf("label (1st argument) should be a vector (# of column is 1).\n"); fake_answer(plhs); return; } plhs[0] = mxCreateNumericMatrix(num_feats, 1, mxDOUBLE_CLASS_ID, mxREAL); ptr_dec_values = (Double *)mxGetPr(plhs[0]); ptr_instance = (Double *)mxGetPr(prhs[1]); if(exact_flag) exact_classifier = new fiksvm_exact_classifier(prhs[2]); else approx_classifier = new fiksvm_approx_classifier(prhs[2]); if(mxIsSparse(prhs[1])){ mexPrintf("Error : sparse data format not supported..\n"); fake_answer(plhs); return; } Double *feat_data = (Double*)malloc(feat_dim*sizeof(Double)); for(int i=0; i<num_feats; i++){ for(int j=0; j<feat_dim ; j++){ //matlab arrays go from down first and then sideways feat_data[j] = (Double)ptr_instance[num_feats*j + i]; } if(exact_flag) ptr_dec_values[i] = exact_classifier->predict(feat_data,predict_probability); else{ if(approx_type == 0) ptr_dec_values[i] = approx_classifier->pwc_predict(feat_data,predict_probability); else ptr_dec_values[i] = approx_classifier->pwl_predict(feat_data,predict_probability); } } if(exact_flag) delete exact_classifier; else delete approx_classifier; free(feat_data); }
// 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=NULL; // transform the input matrix to libspline format if(nrhs > 0 && nrhs < 5){ int err=0; if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) { mexPrintf("Error: label vector and instance matrix must be double\n"); fake_answer(plhs); return; } if(parse_command_line(nrhs, prhs, NULL)){ exit_with_help(); fake_answer(plhs); return; } if(!mxIsSparse(prhs[1])) err = read_problem_dense(prhs[0], prhs[1]); else{ mexPrintf("Error: test_instance_matrix must be dense\n"); fake_answer(plhs); return; } if(err || error_msg){ if (error_msg != NULL) mexPrintf("Error: %s\n", error_msg); fake_answer(plhs); delete [] x; return; } //initialize an empty model model = new additiveModel(); error_msg = matlab_matrix_to_model(model,prhs[2]); if(error_msg){ mexPrintf("Error: can't read model: %s\n", error_msg); delete [] x; delete model; fake_answer(plhs); return; } //compute predictions if(do_predict(plhs) < 0){ delete [] x; delete [] model; fake_answer(plhs); return; } delete [] x; delete model; }else{ exit_with_help(); fake_answer(plhs); return; } }
// Interface function of matlab // now assume prhs[0]: label prhs[1]: features prhs[2]: source models 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) srand(1); // Transform the input Matrix to libsvm format if(nrhs > 1 && nrhs < 7) { int err=0; if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) { mexPrintf("Error: label vector and instance matrix must be double\n"); fake_answer(plhs); return; } if(parse_command_line(nrhs, prhs, NULL)) { exit_with_help(); destroy_param(¶m); fake_answer(plhs); return; } //read in the source models const mxArray *cellModels = prhs[2]; mwSize num_ms = mxGetNumberOfElements(cellModels); param.num_src = (int) num_ms; if(param.num_src > 0) src_models = Malloc(struct model*, param.num_src); for(int i=0; i< param.num_src; i++) { const mxArray *src_model_mat = mxGetCell(cellModels, i); src_models[i] = Malloc(struct model, 1); if((matlab_matrix_to_model(src_models[i], src_model_mat)) != NULL){ mexPrintf("can't load source model\n"); fake_answer(plhs); return; } } //read in the weight of the source models if(!mxIsDouble(prhs[3])) { mexPrintf("Error: weight vector must be double\n"); fake_answer(plhs); return; } // weight of source models if (param.num_src > 0) { int num_row_src_weight = (int) mxGetM(prhs[3]); int num_col_src_weight = (int) mxGetN(prhs[3]); int dim_src_weight = (int) max(num_row_src_weight, num_col_src_weight); if(dim_src_weight != param.num_src) { mexPrintf("Error: lenght of weight vector must be equal to the number of source models!"); fake_answer(plhs); return; } double *src_model_weight = (double *)mxGetPr(prhs[3]); param.src_weights = Malloc(double, param.num_src); for(int i=0; i< param.num_src; i++) param.src_weights[i] = src_model_weight[i]; }
// 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) srand(1); // Transform the input Matrix to libsvm format if(nrhs > 2 && nrhs <= 6) { int err=0; if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1]) ) { mexPrintf("Error: weight vector, label vector matrix must be double\n"); fake_answer(plhs); return; } if(!mxIsSingle(prhs[2])) { mexPrintf("Error: instance matrix must be single\n"); fake_answer(plhs); return; } if(parse_command_line(nrhs, prhs, NULL)) { exit_with_help(); destroy_param(¶m); fake_answer(plhs); return; } #ifdef _DENSE_REP if(!mxIsSparse(prhs[2])) err = read_problem_sparse(prhs[0], prhs[1],prhs[2]); else { mexPrintf("Training_instance_matrix must be dense\n"); destroy_param(¶m); fake_answer(plhs); return; } #else if(mxIsSparse(prhs[2])) err = read_problem_sparse(prhs[0], prhs[1],prhs[2]); else { mexPrintf("Training_instance_matrix must be sparse\n"); destroy_param(¶m); fake_answer(plhs); return; } #endif // xren: delete the input instance matrix to free up space if (nrhs==6) { mxArray* var=(mxArray*)prhs[5]; int status=mexCallMATLAB(0,NULL,1, &var, "clear"); if (status!=0) mexPrintf("Failed to delete variable %s\n",mxArrayToString(prhs[5])); //mxDestroyArray( (mxArray*)prhs[1] ); } // train's original code error_msg = check_parameter(&prob, ¶m); if(err || error_msg) { if (error_msg != NULL) mexPrintf("Error: %s\n", error_msg); destroy_param(¶m); free(prob.y); free(prob.x); if (!use_existing_space) free(x_space); fake_answer(plhs); return; } if(cross_validation_flag) { double *ptr; plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL); ptr = mxGetPr(plhs[0]); ptr[0] = do_cross_validation(); } else { const char *error_msg; model_ = train(&prob, ¶m); error_msg = model_to_matlab_structure(plhs, model_); if(error_msg) mexPrintf("Error: can't convert libsvm model to matrix structure: %s\n", error_msg); free_and_destroy_model(&model_); } destroy_param(¶m); free(prob.y); free(prob.x); free(prob.W); if (!use_existing_space) free(x_space); } else { exit_with_help(); fake_answer(plhs); return; } }
/* read in a problem (in svmlight format) */ void read_problem(const char *filename, mxArray *plhs[]) { int elements, max_index, min_index, i, k; FILE *fp = fopen(filename,"r"); int l = 0; mwIndex *ir, *jc; /* double *labels, *samples; */ double *samples; if(fp == NULL) { mexPrintf("can't open input file %s\n",filename); fake_answer(plhs); return; } max_index = 0; min_index = 1; /* our index starts from 1 */ elements = 0; int index; double value; while(1) { /* label */ /* int index; double value; fscanf(fp,"%lf",&value); */ /* features */ while(1) { int c; do { c = getc(fp); if(c=='\n') goto out; if(c==EOF) goto eof; } while(isspace(c)); ungetc(c,fp); fscanf(fp,"%d:%lf",&index, &value); if (index < min_index) min_index = index; elements++; } out: if(index > max_index) max_index = index; l++; } eof: rewind(fp); /* y */ /* plhs[0] = mxCreateDoubleMatrix(l, 1, mxREAL); */ /* x^T */ if (min_index <= 0) /* plhs[1] = mxCreateSparse(max_index-min_index+1, l, elements, mxREAL); */ plhs[0] = mxCreateSparse(max_index-min_index+1, l, elements, mxREAL); else /* plhs[1] = mxCreateSparse(max_index, l, elements, mxREAL); */ plhs[0] = mxCreateSparse(max_index, l, elements, mxREAL); /* labels = mxGetPr(plhs[0]); */ /* samples = mxGetPr(plhs[1]); */ samples = mxGetPr(plhs[0]); /* ir = mxGetIr(plhs[1]); jc = mxGetJc(plhs[1]); */ ir = mxGetIr(plhs[0]); jc = mxGetJc(plhs[0]); k=0; for(i=0;i<l;i++) { jc[i] = k; /* fscanf(fp,"%lf",&labels[i]); */ while(1) { int c, index; do { c = getc(fp); if(c=='\n') goto out2; } while(isspace(c)); ungetc(c,fp); fscanf(fp,"%d:%lf",&index,&samples[k]); ir[k] = index - min_index; /* precomputed kernel has <index> start from 0 */ ++k; } out2: ; } jc[l] = k; fclose(fp); /* { mxArray *rhs[1], *lhs[1]; rhs[0] = plhs[0]; if(mexCallMATLAB(1, lhs, 1, rhs, "transpose")) { mexPrintf("Error: cannot transpose problem\n"); return; } plhs[0] = lhs[0]; } */ }
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { const char *error_msg; srand(1); if(nrhs == 7) /* force alphas_in and w_in to be initialized */ { int err=0; if(!mxIsClass(prhs[0], "single") || !mxIsClass(prhs[1], "single") || !mxIsClass(prhs[4], "single") || !mxIsClass(prhs[4], "single") || !mxIsClass(prhs[6], "single")) { mexPrintf("Error: label vector, instance matrix and alphas_in must be float\n"); fake_answer(plhs); return; } if(parse_command_line(nrhs, prhs, NULL)) { exit_with_help(); destroy_param(¶m); fake_answer(plhs); return; } #ifdef _DENSE_REP if(!mxIsSparse(prhs[1])) err = read_problem_sparse(prhs[0], prhs[1], prhs[4], prhs[5], prhs[6]); else { mexPrintf("Training_instance_matrix must be dense\n"); destroy_param(¶m); fake_answer(plhs); return; } #else if(mxIsSparse(prhs[1])) err = read_problem_sparse(prhs[0], prhs[1], prhs[4]); else { mexPrintf("Training_instance_matrix must be sparse\n"); destroy_param(¶m); fake_answer(plhs); return; } #endif error_msg = check_parameter(&prob, ¶m); if(err || error_msg) { if (error_msg != NULL) mexPrintf("Error: %s\n", error_msg); destroy_param(¶m); free(prob.y); free(prob.x); free(prob.alphas_in); free(prob.w_in); /*free(x_space);*/ fake_answer(plhs); return; } if(cross_validation_flag) { float *ptr; plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL); ptr = (float*) mxGetPr(plhs[0]); ptr[0] = do_cross_validation(); } else { const char *error_msg; model_ = train(&prob, ¶m); error_msg = model_to_matlab_structure(plhs, model_); if(error_msg) mexPrintf("Error: can't convert libsvm model to matrix structure: %s\n", error_msg); destroy_model(model_); } destroy_param(¶m); free(prob.y); free(prob.x); free(prob.alphas_in); free(prob.w_in); /*free(x_space);*/ } else { exit_with_help(); fake_answer(plhs); return; } }
// 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) srand(1); // Transform the input Matrix to libsvm format if(nrhs > 0 && nrhs < 5) { int err=0; if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) { mexPrintf("Error: label vector and instance matrix must be double\n"); fake_answer(plhs); return; } if(parse_command_line(nrhs, prhs, NULL)) { exit_with_help(); destroy_param(¶m); fake_answer(plhs); return; } if(mxIsSparse(prhs[1])) err = read_problem_sparse(prhs[0], prhs[1]); else { mexPrintf("Training_instance_matrix must be sparse\n"); destroy_param(¶m); fake_answer(plhs); return; } // train's original code error_msg = check_parameter(&prob, ¶m); if(err || error_msg) { if (error_msg != NULL) mexPrintf("Error: %s\n", error_msg); destroy_param(¶m); free(prob.y); free(prob.x); free(x_space); fake_answer(plhs); return; } if(cross_validation_flag) { double *ptr; plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL); ptr = mxGetPr(plhs[0]); ptr[0] = do_cross_validation(); } else { const char *error_msg; model_ = train(&prob, ¶m); error_msg = model_to_matlab_structure(plhs, model_); if(error_msg) mexPrintf("Error: can't convert libsvm model to matrix structure: %s\n", error_msg); destroy_model(model_); } destroy_param(¶m); free(prob.y); free(prob.x); free(x_space); } else { exit_with_help(); fake_answer(plhs); return; } }
// read in a problem (in libsvm format) void read_problem(const char *filename, mxArray *plhs[]) { int max_index, min_index, inst_max_index, i; long elements, k; FILE *fp = fopen(filename,"r"); int l = 0; char *endptr; mwIndex *ir, *jc; double *labels, *samples; if(fp == NULL) { mexPrintf("can't open input file %s\n",filename); fake_answer(plhs); return; } max_line_len = 1024; line = (char *) malloc(max_line_len*sizeof(char)); max_index = 0; min_index = 1; // our index starts from 1 elements = 0; while(readline(fp) != NULL) { char *idx, *val; // features int index = 0; inst_max_index = -1; // strtol gives 0 if wrong format, and precomputed kernel has <index> start from 0 strtok(line," \t"); // label while (1) { idx = strtok(NULL,":"); // index:value val = strtok(NULL," \t"); if(val == NULL) break; errno = 0; index = (int) strtol(idx,&endptr,10); if(endptr == idx || errno != 0 || *endptr != '\0' || index <= inst_max_index) { mexPrintf("Wrong input format at line %d\n",l+1); fake_answer(plhs); return; } else inst_max_index = index; min_index = min(min_index, index); elements++; } max_index = max(max_index, inst_max_index); l++; } rewind(fp); // y plhs[0] = mxCreateDoubleMatrix(l, 1, mxREAL); // x^T if (min_index <= 0) plhs[1] = mxCreateSparse(max_index-min_index+1, l, elements, mxREAL); else plhs[1] = mxCreateSparse(max_index, l, elements, mxREAL); labels = mxGetPr(plhs[0]); samples = mxGetPr(plhs[1]); ir = mxGetIr(plhs[1]); jc = mxGetJc(plhs[1]); k=0; for(i=0;i<l;i++) { char *idx, *val, *label; jc[i] = k; readline(fp); label = strtok(line," \t\n"); if(label == NULL) { mexPrintf("Empty line at line %d\n",i+1); fake_answer(plhs); return; } labels[i] = strtod(label,&endptr); if(endptr == label || *endptr != '\0') { mexPrintf("Wrong input format at line %d\n",i+1); fake_answer(plhs); return; } // features while(1) { idx = strtok(NULL,":"); val = strtok(NULL," \t"); if(val == NULL) break; ir[k] = (mwIndex) (strtol(idx,&endptr,10) - min_index); // precomputed kernel has <index> start from 0 errno = 0; samples[k] = strtod(val,&endptr); if (endptr == val || errno != 0 || (*endptr != '\0' && !isspace(*endptr))) { mexPrintf("Wrong input format at line %d\n",i+1); fake_answer(plhs); return; } ++k; } } jc[l] = k; fclose(fp); free(line); { mxArray *rhs[1], *lhs[1]; rhs[0] = plhs[1]; if(mexCallMATLAB(1, lhs, 1, rhs, "transpose")) { mexPrintf("Error: cannot transpose problem\n"); fake_answer(plhs); return; } plhs[1] = lhs[0]; } }
//outputs the predictions void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { int prob_estimate_flag = 0; int verbose_flag = 0; int approx_type = 1; int exact_flag = 0; if(nrhs > 4 || nrhs < 3) { exit_with_help(); fake_answer(plhs); return; } if(mxIsStruct(prhs[2])) { if(nrhs==4) { int i, argc = 1; char cmd[CMD_LEN], *argv[CMD_LEN/2]; // put options in argv[] mxGetString(prhs[3], cmd, mxGetN(prhs[3]) + 1); if((argv[argc] = strtok(cmd, " ")) != NULL) while((argv[++argc] = strtok(NULL, " ")) != NULL) ; for(i=1;i<argc;i++) { if(argv[i][0] != '-') break; if(++i>=argc) { exit_with_help(); fake_answer(plhs); return; } switch(argv[i-1][1]) { case 'b': prob_estimate_flag = atoi(argv[i]); break; case 'v': verbose_flag = atoi(argv[i]); break; case 'a': approx_type = atoi(argv[i]); break; case 'e': exact_flag = atoi(argv[i]); break; default: mexPrintf("unknown option\n"); exit_with_help(); fake_answer(plhs); return; } } } predict(nlhs, plhs, prhs,prob_estimate_flag,verbose_flag,exact_flag,approx_type); } else { mexPrintf("model file should be a struct array\n"); fake_answer(plhs); } return; }