www.pudn.com > svm_multiClass.rar > svm_struct_classify.c


/***********************************************************************/ 
/*                                                                     */ 
/*   svm_struct_classify.c                                             */ 
/*                                                                     */ 
/*   Classification module of SVM-struct.                              */ 
/*                                                                     */ 
/*   Author: Thorsten Joachims                                         */ 
/*   Date: 03.07.04                                                    */ 
/*                                                                     */ 
/*   Copyright (c) 2004  Thorsten Joachims - All rights reserved       */ 
/*                                                                     */ 
/*   This software is available for non-commercial use only. It must   */ 
/*   not be modified and distributed without prior permission of the   */ 
/*   author. The author is not responsible for implications from the   */ 
/*   use of this software.                                             */ 
/*                                                                     */ 
/************************************************************************/ 
 
#include  
#include "../svm_light/svm_common.h" 
#include "../svm_struct_api.h" 
 
char testfile[200]; 
char modelfile[200]; 
char predictionsfile[200]; 
 
void read_input_parameters(int, char **, char *, char *, char *, long *); 
void print_help(void); 
 
 
int main (int argc, char* argv[]) 
{ 
  long max_docs,max_words_doc,lld; 
  long correct=0,incorrect=0,no_accuracy=0; 
  long i; 
  double t1,runtime=0; 
  double avgloss=0,l; 
  FILE *predfl; 
  STRUCTMODEL model;  
  STRUCT_LEARN_PARM sparm; 
  STRUCT_TEST_STATS teststats; 
  SAMPLE testsample; 
  LABEL y; 
 
  read_input_parameters(argc,argv,testfile,modelfile,predictionsfile, 
			&verbosity); 
 
  nol_ll(testfile,&max_docs,&max_words_doc,&lld); /* scan size of input file */ 
  max_words_doc+=2; 
  lld+=2; 
 
  if(verbosity>=1) { 
    printf("Reading model..."); fflush(stdout); 
  } 
  model=read_struct_model(modelfile,&sparm); 
  if(verbosity>=1) { 
    fprintf(stdout, "done.\n"); 
  } 
 
  if(model.svm_model->kernel_parm.kernel_type == LINEAR) { /* linear kernel */ 
    /* compute weight vector */ 
    add_weight_vector_to_linear_model(model.svm_model); 
    model.w=model.svm_model->lin_weights; 
  } 
   
  if(verbosity>=2) { 
    printf("Reading test examples.."); fflush(stdout); 
  } 
  testsample=read_struct_examples(testfile,&sparm); 
  if(verbosity>=2) { 
    printf("done.\n"); fflush(stdout); 
  } 
 
  if(verbosity>=2) { 
    printf("Classifying test examples.."); fflush(stdout); 
  } 
 
  if ((predfl = fopen (predictionsfile, "w")) == NULL) 
  { perror (predictionsfile); exit (1); } 
 
  for(i=0;i=2) { 
      if((i+1) % 100 == 0) { 
	printf("%ld..",i); fflush(stdout); 
      } 
    } 
    free_label(y); 
  }   
  avgloss/=testsample.n; 
  fclose(predfl); 
  free_struct_sample(testsample); 
  free_struct_model(model); 
 
  if(verbosity>=2) { 
    printf("done\n"); 
    printf("Runtime (without IO) in cpu-seconds: %.2f\n", 
	   (float)(runtime/100.0));     
  } 
  if((!no_accuracy) && (verbosity>=1)) { 
    printf("Average loss on test set: %.4f\n",(float)avgloss); 
    printf("Zero/one-error on test set: %.2f%% (%ld correct, %ld incorrect, %d total)\n",(float)100.0*incorrect/testsample.n,correct,incorrect,testsample.n); 
  } 
  print_struct_testing_stats(testsample,&model,&sparm,&teststats); 
  return(0); 
} 
 
void read_input_parameters(int argc, char **argv, char *testfile,  
			   char *modelfile, char *predictionsfile,  
			   long int *verbosity) 
{ 
  long i; 
   
  /* set default */ 
  strcpy (modelfile, "svm_model"); 
  strcpy (predictionsfile, "svm_predictions");  
  (*verbosity)=2; 
 
  for(i=1;(i=argc) { 
    printf("\nNot enough input parameters!\n\n"); 
    print_help(); 
    exit(0); 
  } 
  strcpy (testfile, argv[i]); 
  strcpy (modelfile, argv[i+1]); 
  if((i+2) this help\n"); 
  printf("         -v [0..3]  -> verbosity level (default 2)\n"); 
  printf("         -f [0,1]   -> 0: old output format of V1.0\n"); 
  printf("                    -> 1: output the value of decision function (default)\n\n"); 
}