-
Notifications
You must be signed in to change notification settings - Fork 0
/
svm_struct_latent_classify.c
92 lines (63 loc) · 2.14 KB
/
svm_struct_latent_classify.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* code for classification in SVM^struct latent */
/* November 9 2008 */
#include <stdio.h>
#include "svm_struct_latent_api.h"
void read_input_parameters(int argc, char **argv, char *testfile, char *modelfile, STRUCT_LEARN_PARM *sparm);
int main(int argc, char* argv[]) {
double avgloss,l;
long i, correct;
char testfile[1024];
char modelfile[1024];
STRUCTMODEL model;
STRUCT_LEARN_PARM sparm;
LEARN_PARM lparm;
KERNEL_PARM kparm;
SAMPLE testsample;
LABEL y;
LATENT_VAR h;
/* read input parameters */
read_input_parameters(argc,argv,testfile,modelfile,&sparm);
/* read model file */
printf("Reading model..."); fflush(stdout);
model = read_struct_model(modelfile, &sparm);
printf("done.\n");
/* read test examples */
printf("Reading test examples..."); fflush(stdout);
testsample = read_struct_examples(testfile,&sparm);
printf("done.\n");
init_struct_model(testsample,&model,&sparm,&lparm,&kparm);
avgloss = 0.0;
correct = 0;
for (i=0;i<testsample.n;i++) {
classify_struct_example(testsample.examples[i].x,&y,&h,&model,&sparm);
l = loss(testsample.examples[i].y,y,h,&sparm);
avgloss += l;
if (l==0) correct++;
free_label(y);
free_latent_var(h);
}
printf("Average loss on test set: %.4f\n", avgloss/testsample.n);
printf("Zero/one error on test set: %.4f\n", 1.0 - ((float) correct)/testsample.n);
free_struct_sample(testsample);
free_struct_model(model,&sparm);
return(0);
}
void read_input_parameters(int argc, char **argv, char *testfile, char *modelfile, STRUCT_LEARN_PARM *sparm) {
long i;
/* set default */
strcpy(modelfile, "svm_model");
sparm->custom_argc = 0;
for (i=1;(i<argc)&&((argv[i])[0]=='-');i++) {
switch ((argv[i])[1]) {
case '-': strcpy(sparm->custom_argv[sparm->custom_argc++],argv[i]);i++; strcpy(sparm->custom_argv[sparm->custom_argc++],argv[i]);break;
default: printf("\nUnrecognized option %s!\n\n",argv[i]); exit(0);
}
}
if (i>=argc) {
printf("\nNot enough input parameters!\n\n");
exit(0);
}
strcpy(testfile, argv[i]);
strcpy(modelfile, argv[i+1]);
parse_struct_parameters(sparm);
}