⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ginitest.cpp

📁 SVM经典调试程序,内有说明,应用简便,可用做回归分类方面的计算
💻 CPP
字号:
/*****************************************************************************/// NAME  :        svmtest.cpp// // DESCRIPTION :  A sample svmtest file that demonstrate the usage of//                GINI SVM interface./////*****************************************************************************/#define _GINI_COUT_PRESENT_#define _HMMSVM_DEBUGON_#include<ginisvm.h>#include<stdio.h>#include<math.h>#include<string.h>#include<stdlib.h>//---------------------------------------------------------------------------// Print the Help for this code.//---------------------------------------------------------------------------void PrintHelp(){   printf("#------------------------------------------------------------------#\n");   printf("#         GINISVM VERSION 1.2                                      #\n");   printf("#   ( Center for Language and Speech Processing,JHU )              #\n");    printf("#------------------------------------------------------------------#\n");   printf("#------------------------------------------------------------------#\n");   printf("# Usage: ginitest [cmdline] <inputfile> <configfile> <outfile>     #\n");   printf("#                                                                  #\n");   printf("# Command Line arguments :                                         #\n");   printf("# -k <ktype>     0 -> Polynomial Kernel (a+b(x.y))^c               #\n");   printf("#                1 -> Gaussian Kernel  exp(-a ||x-y||^2)           #\n");   printf("#                2 -> DTK string kernel exp(-a*Leven(x,y))         #\n");   printf("#                                    ( b -> ins/del  penalty )     #\n");   printf("#                                    ( c -> same ins  penalty )    #\n");   printf("#                                    ( d -> subst  penalty )       #\n");   printf("#                3 -> Tanh Kernel  tanh(a(x.y))                    #\n");   printf("# -sp            sparse data format ( default: non-sparse )        #\n");   printf("# -cflag         Flag to indicate weights in training file         #\n");   printf("# -label         Flag to indicate labels are present in file       #\n");   printf("#------------------------------------------------------------------#\n");   printf("# <inputfile>   Input training file                                #\n");   printf("# <configfile> File where training parameters are stored           #\n");   printf("# <outfile>   File where probabilities are stored                  #\n");   printf("#------------------------------------------------------------------#\n");   printf("# Sample Usage :                                                   #\n");   printf("# ginitest -k 0 test.dat train.cfg test.out                        #\n");   printf("#------------------------------------------------------------------#\n");   printf("# Bug Reports: shantanu@jhu.edu                                    #\n");   printf("#------------------------------------------------------------------#\n");}// -------------------------------------------------------------------------//   Main Routine.// -------------------------------------------------------------------------int main(int argc, char** argv){   GINISVMKernelType ktype = GINISVMPOLY;   GINI_double p1 = 0;   GINI_double p2 = 1;   GINI_double p3 = 1;   GINI_double p4 = 1;   GINI_bool   sp = GINI_FALSE;   GINI_bool   cflag = GINI_FALSE;   GINI_bool   labelval = GINI_FALSE;   GINI_u32    SVM_DIMENSION;   GINI_u32    SVM_CLASS;   GINI_u32    SVM_DATA;   GINI_SVMKernel *kernel = (GINI_SVMKernel*)GINI_NULL;   GINI_int    count = 1;   // Check the number of input parameters.   if (( argc < 6 ) && ( argc > 7))   {      PrintHelp();      return 1;   }   GINI_bool validarg;   // Kernel definition goes here   while ( count < argc-3 )   {      validarg = GINI_FALSE;      if ( strcmp(argv[count],"-k") == 0 )      {         count++;         if ( strcmp(argv[count],"0") == 0 )		 ktype = GINISVMPOLY;	 else if ( strcmp(argv[count],"1") == 0 )		 ktype = GINISVMGAUSSIAN;	 else if ( strcmp(argv[count],"2") == 0 )		 ktype = GINISVMDTK;	 else if ( strcmp(argv[count],"3") == 0 )		 ktype = GINISVMTANH;	 else          {	     PrintHelp();	     return 1;         }	 validarg = GINI_TRUE;	 count++;      }         if ( strcmp(argv[count],"-p1") == 0 )      {         count++;	 p1 = atof(argv[count++]);	 validarg = GINI_TRUE;      }      if ( strcmp(argv[count],"-p2") == 0 )      {         count++;	 p2 = atof(argv[count++]);	 validarg = GINI_TRUE;      }       if ( strcmp(argv[count],"-p3") == 0 )      {         count++;	 p3 = atof(argv[count++]);	 validarg = GINI_TRUE;      }      if ( strcmp(argv[count],"-p4") == 0 )      {         count++;	 p4 = atof(argv[count++]);	 validarg = GINI_TRUE;      }      if ( strcmp(argv[count],"-sp") == 0 )      {         sp = GINI_TRUE;	 count++;	 validarg = GINI_TRUE;      }      if ( strcmp(argv[count],"-cflag") == 0 )      {         cflag = GINI_TRUE;	 count++;	 validarg = GINI_TRUE;      }      if ( strcmp(argv[count],"-label") == 0 )      {         labelval = GINI_TRUE;	 count++;	 validarg = GINI_TRUE;      }      if ( validarg == GINI_FALSE )      {         PrintHelp();	 return 1;      }   }   if ( count > argc - 3 )   {      PrintHelp();      return 1;   }   // Read the input training file and read the header.   // Read in the data from the file   FILE *fp = fopen(argv[argc-3],"r");   if ( fp == (FILE*) GINI_NULL )   {      printf("Test File not Present\n");      return 1;   }   FILE *fout = fopen(argv[argc-2],"r");   if ( fout == (FILE*) GINI_NULL )   {      printf("Cannot Open Configuration File for writing\n");      return 1;   }   FILE *fprob = fopen(argv[argc-1],"w");   if ( fprob == (FILE*) GINI_NULL )   {      printf("Cannot Open Probability File for writing\n");      return 1;   }   GINI_float value;   // First read in the dimensionality of the input   // vectors.   if ( ktype != GINISVMDTK )   {      fscanf(fp,"%f\n",&value);      SVM_DIMENSION = (GINI_u32)value;   }   else   {      SVM_DIMENSION = 0;   }   // Number of classes   fscanf(fp,"%f\n",&value);   SVM_CLASS = (GINI_u32)value;   // Total Number of training points   fscanf(fp,"%f\n",&value);   SVM_DATA = (GINI_u32)value;   // Initialize the kernel depending on the ktype.   switch (ktype)   {	   case GINISVMGAUSSIAN :		  kernel = new GINI_GaussianKernel(p1,sp);		  break;	   case GINISVMPOLY :		  kernel = new GINI_PolyKernel(p1,p2,(GINI_u32)p3,sp);		  break;	   case GINISVMDTK :		  kernel = new GINI_DTKKernel(p1,p2,p3,p4);		  SVM_DIMENSION = 0;		  break;	   case GINISVMTANH :		  kernel = new GINI_TanhKernel(p1,sp);		  break;   }   // Now define svm block and initialize it with the kernel   GINI_SVMBlock *svmmachine = new GINI_SVMBlock(kernel);   printf("Reading in GiniSVM parameters\n");   if ( svmmachine->Read(fout) == GINI_FALSE )   {      printf("SVM Initialization Failure: Check the kernel type and the data format\n");      return 1;   }   // Print out some the statistics.   printf("Total number of support vectors = %d\n",svmmachine->GetSize());   if ( svmmachine->GetDimension() != SVM_DIMENSION )   {      printf("Dimensionality of the SV differs from Test points !!!\n");      return 1;   }   if ( svmmachine->GetClasses() != SVM_CLASS )   {      printf("Number of classes differ between coefficients and Test points !!!\n");      return 1;   }   // Data structures to read in the training data.   GINI_double *testvec;   GINI_double *label;   GINI_double cval;   GINI_u32 currid,maxid;   GINI_double maxval;   GINI_u32 **confusion = (GINI_u32**) GINI_NULL;   currid = 0;   if ( labelval == GINI_TRUE )   {      confusion = new (GINI_u32*)[SVM_CLASS];      if ( confusion == ( GINI_u32**) GINI_NULL )      {         printf("Memory Allocation Failure\n");         return 1;      }      for ( GINI_u32 i = 0; i < SVM_CLASS; i++ )      {          confusion[i] = new (GINI_u32)[SVM_CLASS];          if ( confusion[i] == ( GINI_u32*) GINI_NULL )          {             printf("Memory Allocation Failure\n");             return 1;          }          for ( GINI_u32 j = 0; j < SVM_CLASS; j++ )          {               confusion[i][j] = 0;          }      }   }   // Allocate memory for the label vector   label = new GINI_double[SVM_CLASS];   for ( GINI_u32 i = 0; i < SVM_DATA; i++ )   {      if ( labelval == GINI_TRUE )      {         fscanf(fp,"%f\n",&value);         if ( value >= SVM_CLASS)         {            printf("Improper Label: Expected < %d, Got %d\n",SVM_CLASS,(GINI_u32)value);            return 1;         }         // Allocate memory for the label vector         for ( GINI_u32 j = 0; j< SVM_CLASS; j++ )         {            if ( j != value)            {               label[j]= 0;            }            else            {               label[j] = 1;	       currid = j;            }         }      }      cval = 1.0;         if ( cflag == GINI_TRUE )      {         // Read in the C value for this data point         fscanf(fp,"%f\n",&value);         cval = (GINI_double)value;      }      if ( sp == GINI_FALSE )      {         // If the data is in a non-sparse format	 if ( ktype == GINISVMDTK )         {            // If this is a DTK kernel then we have to	    // process variable length data.            fscanf(fp,"%f\n",&value);            testvec = new GINI_double[(GINI_u32)value+1];	    testvec[0] = (GINI_double)value;            for ( GINI_u32 j = 0; j< (GINI_u32)testvec[0]; j++ )            {               fscanf(fp,"%f\n",&value);               testvec[j+1] = (GINI_double)value;            }         }	 else         {            // Allocate memory for the training vector            testvec = new GINI_double[SVM_DIMENSION];            for ( GINI_u32 j = 0; j< SVM_DIMENSION; j++ )            {               fscanf(fp,"%f\n",&value);               testvec[j] = (GINI_double)value;            }         }      }      else      {         // If the data is in a sparse format.         fscanf(fp,"%f\n",&value);         testvec = new GINI_double[(GINI_u32)(2*value)+1];	 testvec[0] = (GINI_double)value;         for ( GINI_u32 j = 0; j< (GINI_u32)testvec[0]; j++ )         {            // Read in the Index and then the value.            fscanf(fp,"%f\n",&value);            testvec[2*j+1] = (GINI_double)value;            fscanf(fp,"%f\n",&value);            testvec[2*j+2] = (GINI_double)value;         }      }      // Compute the Probability      svmmachine->GiniProb(testvec,label);      // Print the probability output for the corresponding      // vector      //fprintf(fprob,"Data id = %5d, ",i);      for ( GINI_u32 j = 0; j<SVM_CLASS; j++ )      {          fprintf(fprob,"%1.4f  ",label[j]);      }      fprintf(fprob,"\n");      if (labelval == GINI_TRUE )      {         maxval = 0;         maxid = 0;         for (GINI_u32 j=0; j< SVM_CLASS; j++ )         {             if ( maxval < label[j] )             {                  maxval = label[j];                  maxid = j;             }         }         confusion[maxid][currid]++;      }      //printf("Dataid = %d, True Class = %d, Hypothesis = %d\n",i,currid,maxid);      delete [] testvec;   }   fclose(fp);   fclose(fprob);   if ( labelval == GINI_TRUE )   {      printf("---------------CONFUSION MATRIX------------\n");      GINI_u32 errrate = 0;      for ( GINI_u32 i = 0; i < SVM_CLASS; i++ )      {          for ( GINI_u32 j = 0; j < SVM_CLASS; j++ )          {              printf("%4d ",confusion[i][j]);              if ( i == j)              {                  errrate += confusion[i][j];              }          }          printf("\n");      }      printf("Error rate = %f\n",(GINI_float)(SVM_DATA-errrate)/SVM_DATA*100);   }   // Free all the memory.   delete svmmachine;   delete [] label;   delete kernel;   if ( labelval == GINI_TRUE )   {      for ( GINI_u32 i = 0; i< SVM_CLASS; i++ )      {         delete [] confusion[i];      }      delete [] confusion;   }   return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -