svm.cpp
来自「Ball Vector Machine (BVM)支撑向量机C++程序项目代码」· C++ 代码 · 共 3,208 行 · 第 1/5 页
CPP
3,208 行
double sum = 0; int si = start[i]; int sj = start[j]; int ci = model->nSV[i]; int cj = model->nSV[j]; int k; double *coef1 = model->sv_coef[j-1]; double *coef2 = model->sv_coef[i];
for(k=0;k<ci;k++)
{ sum += coef1[si+k] * kvalue[si+k];
}
for(k=0;k<cj;k++)
{ sum += coef2[sj+k] * kvalue[sj+k];
}
sum -= model->rho[p++]; dec_values[pos++] = sum;
} free(kvalue); free(start); }}double svm_predict(const svm_model *model, const svm_node *x){ if(model->param.svm_type == ONE_CLASS || model->param.svm_type == EPSILON_SVR || model->param.svm_type == NU_SVR || model->param.svm_type == CVDD || model->param.svm_type == CVR ) { double res; svm_predict_values(model, x, &res); if(model->param.svm_type == ONE_CLASS || model->param.svm_type == CVDD) return (res>0)?1:-1; else return res; } else { int i; int nr_class = model->nr_class; double *dec_values = Malloc(double, nr_class*(nr_class-1)/2); svm_predict_values(model, x, dec_values); int *vote = Malloc(int,nr_class); for(i=0;i<nr_class;i++) vote[i] = 0; int pos=0; for(i=0;i<nr_class;i++) for(int j=i+1;j<nr_class;j++) { if(dec_values[pos++] > 0) ++vote[i]; else ++vote[j]; } int vote_max_idx = 0; for(i=1;i<nr_class;i++) if(vote[i] > vote[vote_max_idx]) vote_max_idx = i; free(vote); free(dec_values); return model->label[vote_max_idx]; }}
double svm_predict_rank(const svm_model *model, const svm_node *x)
{
if(model->param.svm_type == ONE_CLASS )
{
double res;
svm_predict_values(model, x, &res);
return res;
}
else if (model->param.svm_type == C_SVC || model->param.svm_type == NU_SVC
|| model->param.svm_type == BVM || model->param.svm_type == BVM_2
|| model->param.svm_type == CVM || model->param.svm_type == CVM_LS)
{
int nr_class = model->nr_class;
if (nr_class == 2)
{
double dec_values;
svm_predict_values(model, x, &dec_values);
return dec_values; // assume we only have +1 and -1 classes
}
else return -100;
}
else return -100;
}
double svm_predict_probability( const svm_model *model, const svm_node *x, double *prob_estimates){ if ((model->param.svm_type == C_SVC || model->param.svm_type == NU_SVC) && model->probA!=NULL && model->probB!=NULL) { int i; int nr_class = model->nr_class; double *dec_values = Malloc(double, nr_class*(nr_class-1)/2); svm_predict_values(model, x, dec_values); double min_prob=1e-7; double **pairwise_prob=Malloc(double *,nr_class); for(i=0;i<nr_class;i++) pairwise_prob[i]=Malloc(double,nr_class); int k=0; for(i=0;i<nr_class;i++) for(int j=i+1;j<nr_class;j++) { pairwise_prob[i][j]=min(max(sigmoid_predict(dec_values[k],model->probA[k],model->probB[k]),min_prob),1-min_prob); pairwise_prob[j][i]=1-pairwise_prob[i][j]; k++; } multiclass_probability(nr_class,pairwise_prob,prob_estimates); int prob_max_idx = 0; for(i=1;i<nr_class;i++) if(prob_estimates[i] > prob_estimates[prob_max_idx]) prob_max_idx = i; for(i=0;i<nr_class;i++) free(pairwise_prob[i]); free(dec_values); free(pairwise_prob); return model->label[prob_max_idx]; } else return svm_predict(model, x);}const char *svm_type_table[] ={ "c_svc","nu_svc","one_class","epsilon_svr","nu_svr","cvdd","cvc","cvc_ls","cvr","bvm","bvm_2",NULL};const char *kernel_type_table[]={ "linear","polynomial","rbf","sigmoid","precomputed","exp","normalized_poly","inverse_dist","inverse_sqdist",NULL};int svm_save_model(const char *model_file_name, const svm_model *model){ FILE *fp = fopen(model_file_name,"w"); if(fp==NULL) return -1; const svm_parameter& param = model->param; fprintf(fp,"svm_type %s\n", svm_type_table[param.svm_type]); fprintf(fp,"kernel_type %s\n", kernel_type_table[param.kernel_type]); if(param.kernel_type == POLY || param.kernel_type == NORMAL_POLY) fprintf(fp,"degree %d\n", param.degree); if(param.kernel_type == POLY || param.kernel_type == RBF || param.kernel_type == SIGMOID || param.kernel_type == EXP || param.kernel_type == NORMAL_POLY || param.kernel_type == INV_SQDIST || param.kernel_type == INV_DIST) fprintf(fp,"gamma %g\n", param.gamma); if(param.kernel_type == POLY || param.kernel_type == SIGMOID || param.kernel_type == EXP || param.kernel_type == NORMAL_POLY) fprintf(fp,"coef0 %g\n", param.coef0); int nr_class = model->nr_class; int l = model->l; fprintf(fp, "nr_class %d\n", nr_class); fprintf(fp, "total_sv %d\n",l); { fprintf(fp, "rho"); for(int i=0;i<nr_class*(nr_class-1)/2;i++) fprintf(fp," %g",model->rho[i]); fprintf(fp, "\n"); } if(model->param.svm_type == CVDD) { fprintf(fp, "cNorm"); fprintf(fp, " %g\n", model->cNorm[0]); } if(model->label) { fprintf(fp, "label"); for(int i=0;i<nr_class;i++) fprintf(fp," %d",model->label[i]); fprintf(fp, "\n"); } if(model->probA) // regression has probA only { fprintf(fp, "probA"); for(int i=0;i<nr_class*(nr_class-1)/2;i++) fprintf(fp," %g",model->probA[i]); fprintf(fp, "\n"); } if(model->probB) { fprintf(fp, "probB"); for(int i=0;i<nr_class*(nr_class-1)/2;i++) fprintf(fp," %g",model->probB[i]); fprintf(fp, "\n"); } if(model->nSV) { fprintf(fp, "nr_sv"); for(int i=0;i<nr_class;i++) fprintf(fp," %d",model->nSV[i]); fprintf(fp, "\n"); } fprintf(fp, "SV\n"); const double * const *sv_coef = model->sv_coef; const svm_node * const *SV = model->SV; for(int i=0;i<l;i++) { for(int j=0;j<nr_class-1;j++) fprintf(fp, "%.16g ",sv_coef[j][i]); const svm_node *p = SV[i]; if(param.kernel_type == PRECOMPUTED) fprintf(fp,"0:%d ",(int)(p->value)); else while(p->index != -1) {
#ifdef INT_FEAT
fprintf(fp,"%d:%d ",(int)p->index,(int)p->value);
#else fprintf(fp,"%d:%.8g ",p->index,p->value);
#endif p++; } fprintf(fp, "\n"); } fclose(fp); return 0;}svm_model *svm_load_model(const char *model_file_name){ FILE *fp = fopen(model_file_name,"rb"); if(fp==NULL) return NULL; // read parameters svm_model *model = Malloc(svm_model,1); svm_parameter& param = model->param; model->rho = NULL; model->probA = NULL; model->probB = NULL; model->label = NULL; model->nSV = NULL; char cmd[81]; while(1) { fscanf(fp,"%80s",cmd); if(strcmp(cmd,"svm_type")==0) { fscanf(fp,"%80s",cmd); int i; for(i=0;svm_type_table[i];i++) { if(strcmp(svm_type_table[i],cmd)==0) { param.svm_type=i; break; } } if(svm_type_table[i] == NULL) { fprintf(stderr,"unknown svm type.\n"); free(model->rho); free(model->label); free(model->nSV); free(model); return NULL; } } else if(strcmp(cmd,"kernel_type")==0) { fscanf(fp,"%80s",cmd); int i; for(i=0;kernel_type_table[i];i++) { if(strcmp(kernel_type_table[i],cmd)==0) { param.kernel_type=i; break; } } if(kernel_type_table[i] == NULL) { fprintf(stderr,"unknown kernel function.\n"); free(model->rho); free(model->label); free(model->nSV); free(model); return NULL; } } else if(strcmp(cmd,"degree")==0) fscanf(fp,"%d",¶m.degree); else if(strcmp(cmd,"gamma")==0) fscanf(fp,"%lf",¶m.gamma); else if(strcmp(cmd,"coef0")==0) fscanf(fp,"%lf",¶m.coef0); else if(strcmp(cmd,"nr_class")==0) fscanf(fp,"%d",&model->nr_class); else if(strcmp(cmd,"total_sv")==0) fscanf(fp,"%d",&model->l); else if(strcmp(cmd,"rho")==0) { int n = model->nr_class * (model->nr_class-1)/2; model->rho = Malloc(double,n); for(int i=0;i<n;i++) fscanf(fp,"%lf",&model->rho[i]); } else if (strcmp(cmd,"cNorm")==0) { int n = model->nr_class * (model->nr_class-1)/2; model->cNorm = Malloc(double,n); for(int i=0;i<n;i++) fscanf(fp,"%lf",&model->cNorm[i]); } else if(strcmp(cmd,"label")==0) { int n = model->nr_class; model->label = Malloc(int,n); for(int i=0;i<n;i++) fscanf(fp,"%d",&model->label[i]); } else if(strcmp(cmd,"probA")==0) { int n = model->nr_class * (model->nr_class-1)/2; model->probA = Malloc(double,n); for(int i=0;i<n;i++) fscanf(fp,"%lf",&model->probA[i]); } else if(strcmp(cmd,"probB")==0) { int n = model->nr_class * (model->nr_class-1)/2; model->probB = Malloc(double,n); for(int i=0;i<n;i++) fscanf(fp,"%lf",&model->probB[i]); } else if(strcmp(cmd,"nr_sv")==0) { int n = model->nr_class; model->nSV = Malloc(int,n); for(int i=0;i<n;i++) fscanf(fp,"%d",&model->nSV[i]); } else if(strcmp(cmd,"SV")==0) { while(1) { int c = getc(fp); if(c==EOF || c=='\n') break; } break; }/*
else { fprintf(stderr,"unknown text in model file\n"); free(model->rho); free(model->label); free(model->nSV); free(model); return NULL; }*/
} // read sv_coef and SV int elements = 0; long pos = ftell(fp); while(1) { int c = fgetc(fp); switch(c) { case '\n': // count the '-1' element case ':': ++elements; break; default: ; }
if (c == EOF)
break; } fseek(fp,pos,SEEK_SET); int m = model->nr_class - 1; int l = model->l; model->sv_coef = Malloc(double *,m); int i; for(i=0;i<m;i++) model->sv_coef[i] = Malloc(double,l); model->SV = Malloc(svm_node*,l); svm_node *x_space=NULL; if(l>0) x_space = Malloc(svm_node,elements); int j=0; for(i=0;i<l;i++) { model->SV[i] = &x_space[j]; for(int k=0;k<m;k++) fscanf(fp,"%lf",&model->sv_coef[k][i]); while(1) { int c; do { c = getc(fp); if(c=='\n') goto out2; } while(isspace(c)); ungetc(c,fp);
#ifdef INT_FEAT
int tmpindex;
int tmpvalue;
fscanf(fp,"%d:%d",&(tmpindex),&(tmpvalue));
x_space[j].index = tmpindex;
x_space[j].value = tmpvalue;
#else fscanf(fp,"%d:%lf",&(x_space[j].index),&(x_space[j].value));
#endif ++j; } out2: x_space[j++].index = -1; } fclose(fp); model->free_sv = 1; // XXX return model;}void svm_destroy_model(svm_model* model){ if(model->free_sv && model->l > 0) free((void *)(model->SV[0])); for(int i=0;i<model->nr_class-1;i++) free(model->sv_coef[i]); free(model->SV); free(model->sv_coef); free(model->rho); free(model->label); free(model->probA); free(model->probB); free(model->nSV); free(model);}void svm_destroy_param(svm_parameter* param){ free(param->weight_label); free(param->weight);}const char *svm_check_parameter(const svm_problem *prob, const svm_parameter *param){ // svm_type int svm_type = param->svm_type; if(svm_type != C_SVC && svm_type != NU_SVC && svm_type != ONE_CLASS && svm_type != EPSILON_SVR && svm_type != NU_SVR &&
svm_type != BVM &&
svm_type != BVM_2 &&
svm_type != CVM && svm_type != CVM_LS && svm_type != CVDD && svm_type != CVR ) return "unknown svm type"; // kernel_type, degree int kernel_type = param->kernel_type; if(kernel_type != LINEAR && kernel_type != POLY && kernel_type != RBF && kernel_type != SIGMOID && kernel_type != PRECOMPUTED &&
kernel_type != EXP &&
kernel_type != NORMAL_POLY && kernel_type != INV_SQDIST && kernel_type != INV_DIST) return "unknown kernel type"; if((kernel_type != RBF && kernel_type != EXP && kernel_type != NORMAL_POLY && kernel_type != INV_SQDIST && kernel_type != INV_DIST)
&& (svm_type == BVM || svm_type == BVM_2))
return "BVM supports isotropic kernels such as RBF";
if(param->degree < 0) return "degree of polynomial kernel < 0"; // cache_size,eps,C,nu,p,shrinking if(param->cache_size <= 0) return "cache_size <= 0"; if((param->svm_type != CVDD && param->svm_type != CVM && param->svm_type != CVM_LS && param->svm_type != CVR && param->svm_type != BVM && param->svm_type != BVM_2) && param->eps <= 0) return "eps <= 0"; if(svm_type == C_SVC || svm_type == EPSILON_SVR || svm_type == NU_SVR) if(param->C <= 0.0) return "C <= 0"; if(svm_type == NU_SVC || svm_type == ONE_CLASS || svm_type == NU_SVR) if(param->nu <= 0 || param->nu > 1) return "nu <= 0 or nu > 1"; if(svm_type == CVR || svm_type == CVM_LS) if(param->mu < 0.0) return "mu < 0"; if(svm_type == EPSILON_SVR) if(param->p < 0) return "p < 0"; if(param->shrinking != 0 && param->shrinking != 1) return "shrinking != 0 and shrinking != 1"; if(param->probability != 0 && param->probability != 1) return "probability != 0 and probability != 1"; if(param->probability == 1 && svm_type == ONE_CLASS) return "one-class SVM probability output not supported yet"; if(param->sample_size <= 0)
return "sample_size <= 0";
// check whether nu-svc is feasible if(svm_type == NU_SVC) { int l = prob->l; int max_nr_class = 16; int nr_class = 0; int *label = Malloc(int,max_nr_class); int *count = Malloc(int,max_nr_class); int i; for(i=0;i<l;i++) { int this_label = (int)prob->y[i]; int j; for(j=0;j<nr_class;j++) if(this_label == label[j]) { ++count[j]; break; } if(j == nr_class) { if(nr_class == max_nr_class) { max_nr_class *= 2; label = (int *)realloc(label,max_nr_class*sizeof(int)); count = (int *)realloc(count,max_nr_class*sizeof(int)); } label[nr_class] = this_label; count[nr_class] = 1; ++nr_class; } } for(i=0;i<nr_class;i++) { int n1 = count[i]; for(int j=i+1;j<nr_class;j++) { int n2 = count[j]; if(param->nu*(n1+n2)/2 > min(n1,n2)) { free(label); free(count); return "specified nu is infeasible"; } } } free(label); free(count); } return NULL;}int svm_check_probability_model(const svm_model *model){ return ((model->param.svm_type == C_SVC || model->param.svm_type == NU_SVC) && model->probA!=NULL && model->probB!=NULL) || ((model->param.svm_type == EPSILON_SVR || model->param.svm_type == NU_SVR) && model->probA!=NULL);}
void displayInfoAboutModel(const struct svm_model* model)
{
if(model->param.svm_type >= BVM)
{
int i;
int nr_class = model->nr_class;
int l = model->l;
info("Information section\n-------------------\n");
info(" class no.: %d\n", nr_class);
info(" sample no.: %d\n", l);
int *start = Malloc(int,nr_class);
start[0] = 0;
for(i=1;i<nr_class;i++)
start[i] = start[i-1]+mode
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?