-
Notifications
You must be signed in to change notification settings - Fork 0
/
svm_struct_latent_cccp.c
executable file
·802 lines (651 loc) · 22.6 KB
/
svm_struct_latent_cccp.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
/* linear structural SVM with latent variables */
/* 30 September 2008 */
#include <stdio.h>
#include <assert.h>
//#include "./svm_light/svm_common.h"
//#include "svm_struct_latent_api_types.h"
#include "svm_struct_latent_api.h"
#include<time.h>
#define ALPHA_THRESHOLD 1E-14
#define IDLE_ITER 20
#define CLEANUP_CHECK 100
#define STOP_PREC 1E-2
#define UPDATE_BOUND 3
#define MAX_OUTER_ITER 400
#define MAX(x,y) ((x) < (y) ? (y) : (x))
#define MIN(x,y) ((x) > (y) ? (y) : (x))
#define DEBUG_LEVEL 1
/* mosek interface */
int mosek_qp_optimize(double**, double*, double*, long, double, double*, double);
void my_read_input_parameters(int argc, char* argv[], char *trainfile, char *modelfile,
LEARN_PARM *learn_parm, KERNEL_PARM *kernel_parm, STRUCT_LEARN_PARM *struct_parm);
void my_wait_any_key();
int resize_cleanup(int size_active, int *idle, double *alpha, double *delta, double *gammaG0, double *proximal_rhs, double **G, DOC **dXc, double *cut_error);
double sprod_nn(double *a, double *b, long n) {
double ans=0.0;
long i;
for (i=1;i<n+1;i++) {
ans+=a[i]*b[i];
}
return(ans);
}
void add_vector_nn(double *w, double *dense_x, long n, double factor) {
long i;
for (i=1;i<n+1;i++) {
w[i]+=factor*dense_x[i];
}
}
double* add_list_nn(SVECTOR *a, long totwords)
/* computes the linear combination of the SVECTOR list weighted
by the factor of each SVECTOR. assumes that the number of
features is small compared to the number of elements in the
list */
{
SVECTOR *f;
long i;
double *sum;
sum=create_nvector(totwords);
for(i=0;i<=totwords;i++)
sum[i]=0;
for(f=a;f;f=f->next)
add_vector_ns(sum,f,f->factor);
return(sum);
}
SVECTOR* find_cutting_plane(EXAMPLE *ex, SVECTOR **fycache, double *margin, long m, STRUCTMODEL *sm, STRUCT_LEARN_PARM *sparm, char *tmpdir, char *trainfile) {
long i;
SVECTOR *f, *fy, *fybar, *lhs;
LABEL ybar;
LATENT_VAR hbar;
double lossval;
double *new_constraint;
long l,k;
SVECTOR *fvec;
WORD *words;
LABEL *ybar_all = (LABEL*) malloc(sizeof(LABEL) * m);
LATENT_VAR *hbar_all = (LATENT_VAR*) malloc (sizeof(LATENT_VAR) * m);
time_t mv_start, mv_end;
time(&mv_start);
find_most_violated_constraint_marginrescaling_all(ybar_all, hbar_all, sm, sparm, m, tmpdir, trainfile);
time(&mv_end);
#if (DEBUG_LEVEL==1)
print_time(mv_start, mv_end, "Max violators");
#endif
/* find cutting plane */
lhs = NULL;
*margin = 0;
for (i=0;i<m;i++) {
//find_most_violated_constraint_marginrescaling(ex[i].x, ex[i].y, &ybar, &hbar, sm, sparm);
ybar = ybar_all[i];
hbar = hbar_all[i];
/* get difference vector */
fy = copy_svector(fycache[i]);
fybar = psi(ex[i].x,ybar,hbar,sm,sparm);
lossval = loss(ex[i].y,ybar,hbar,sparm);
free_label(ybar);
free_latent_var(hbar);
/* scale difference vector */
for (f=fy;f;f=f->next) {
f->factor*=1.0/m;
//f->factor*=ex[i].x.example_cost/m;
}
for (f=fybar;f;f=f->next) {
f->factor*=-1.0/m;
//f->factor*=-ex[i].x.example_cost/m;
}
/* add ybar to constraint */
append_svector_list(fy,lhs);
append_svector_list(fybar,fy);
lhs = fybar;
*margin+=lossval/m;
//*margin+=lossval*ex[i].x.example_cost/m;
}
free(ybar_all);
free(hbar_all);
/* compact the linear representation */
new_constraint = add_list_nn(lhs, sm->sizePsi);
free_svector(lhs);
l=0;
for (i=1;i<sm->sizePsi+1;i++) {
if (fabs(new_constraint[i])>1E-10) l++; // non-zero
}
words = (WORD*)my_malloc(sizeof(WORD)*(l+1));
assert(words!=NULL);
k=0;
for (i=1;i<sm->sizePsi+1;i++) {
if (fabs(new_constraint[i])>1E-10) {
words[k].wnum = i;
words[k].weight = new_constraint[i];
k++;
}
}
words[k].wnum = 0;
words[k].weight = 0.0;
fvec = create_svector(words,"",1);
free(words);
free(new_constraint);
return(fvec);
}
double cutting_plane_algorithm(double *w, long m, int MAX_ITER, double C, double epsilon, SVECTOR **fycache, EXAMPLE *ex, STRUCTMODEL *sm, STRUCT_LEARN_PARM *sparm, char *tmpdir, char *trainfile) {
long i,j;
double xi;
double *alpha;
double **G; /* Gram matrix */
DOC **dXc; /* constraint matrix */
double *delta; /* rhs of constraints */
SVECTOR *new_constraint;
double dual_obj, alphasum;
int iter, size_active;
double value;
int r;
int *idle; /* for cleaning up */
double margin;
double primal_obj;
double *proximal_rhs;
double *gammaG0=NULL;
double min_rho = 0.001;
double max_rho;
double serious_counter=0;
double rho = 1.0; /* temporarily set it to 1 first */
double expected_descent, primal_obj_b=-1, reg_master_obj;
int null_step=1;
double *w_b;
double kappa=0.1;
double temp_var;
double proximal_term, primal_lower_bound;
double v_k;
double obj_difference;
double *cut_error; // cut_error[i] = alpha_{k,i} at current center x_k
double sigma_k;
double m2 = 0.2;
double m3 = 0.9;
double gTd;
double last_sigma_k=0;
double initial_primal_obj;
int suff_decrease_cond=0;
double decrease_proportion = 0.2; // start from 0.2 first
double z_k_norm;
double last_z_k_norm=0;
w_b = create_nvector(sm->sizePsi);
clear_nvector(w_b,sm->sizePsi);
/* warm start */
for (i=1;i<sm->sizePsi+1;i++) {
w_b[i] = w[i];
}
iter = 0;
size_active = 0;
xi = 0.0;
alpha = NULL;
G = NULL;
dXc = NULL;
delta = NULL;
idle = NULL;
proximal_rhs = NULL;
cut_error = NULL;
new_constraint = find_cutting_plane(ex, fycache, &margin, m, sm, sparm, tmpdir, trainfile);
value = margin - sprod_ns(w, new_constraint);
primal_obj_b = 0.5*sprod_nn(w_b,w_b,sm->sizePsi)+C*value;
primal_obj = 0.5*sprod_nn(w,w,sm->sizePsi)+C*value;
primal_lower_bound = 0;
expected_descent = -primal_obj_b;
initial_primal_obj = primal_obj_b;
max_rho = C;
printf("Running CCCP inner loop solver: "); fflush(stdout);
time_t iter_start, iter_end;
while ((!suff_decrease_cond)&&(expected_descent<-epsilon)&&(iter<MAX_ITER)) {
iter+=1;
size_active+=1;
time(&iter_start);
#if (DEBUG_LEVEL>0)
printf("ITER %d\n", iter);
#endif
printf("."); fflush(stdout);
/* add constraint */
dXc = (DOC**)realloc(dXc, sizeof(DOC*)*size_active);
assert(dXc!=NULL);
dXc[size_active-1] = (DOC*)malloc(sizeof(DOC));
dXc[size_active-1]->fvec = new_constraint;
dXc[size_active-1]->slackid = 1; // only one common slackid (one-slack)
dXc[size_active-1]->costfactor = 1.0;
delta = (double*)realloc(delta, sizeof(double)*size_active);
assert(delta!=NULL);
delta[size_active-1] = margin;
alpha = (double*)realloc(alpha, sizeof(double)*size_active);
assert(alpha!=NULL);
alpha[size_active-1] = 0.0;
idle = (int*)realloc(idle, sizeof(int)*size_active);
assert(idle!=NULL);
idle[size_active-1] = 0;
/* proximal point */
proximal_rhs = (double*)realloc(proximal_rhs, sizeof(double)*size_active);
assert(proximal_rhs!=NULL);
cut_error = (double*)realloc(cut_error, sizeof(double)*size_active);
assert(cut_error!=NULL);
// note g_i = - new_constraint
cut_error[size_active-1] = C*(sprod_ns(w_b, new_constraint) - sprod_ns(w, new_constraint));
cut_error[size_active-1] += (primal_obj_b - 0.5*sprod_nn(w_b,w_b,sm->sizePsi));
cut_error[size_active-1] -= (primal_obj - 0.5*sprod_nn(w,w,sm->sizePsi));
gammaG0 = (double*)realloc(gammaG0, sizeof(double)*size_active);
assert(gammaG0!=NULL);
/* update Gram matrix */
G = (double**)realloc(G, sizeof(double*)*size_active);
assert(G!=NULL);
G[size_active-1] = NULL;
for (j=0;j<size_active;j++) {
G[j] = (double*)realloc(G[j], sizeof(double)*size_active);
assert(G[j]!=NULL);
}
for (j=0;j<size_active-1;j++) {
G[size_active-1][j] = sprod_ss(dXc[size_active-1]->fvec, dXc[j]->fvec);
G[j][size_active-1] = G[size_active-1][j];
}
G[size_active-1][size_active-1] = sprod_ss(dXc[size_active-1]->fvec,dXc[size_active-1]->fvec);
/* update gammaG0 */
if (null_step==1) {
gammaG0[size_active-1] = sprod_ns(w_b, dXc[size_active-1]->fvec);
} else {
for (i=0;i<size_active;i++) {
gammaG0[i] = sprod_ns(w_b, dXc[i]->fvec);
}
}
/* update proximal_rhs */
for (i=0;i<size_active;i++) {
proximal_rhs[i] = delta[i] - rho/(1+rho)*gammaG0[i];
}
/* solve QP to update alpha */
dual_obj = 0;
time_t mosek_start, mosek_end;
time(&mosek_start);
r = mosek_qp_optimize(G, proximal_rhs, alpha, (long) size_active, C, &dual_obj,rho);
time(&mosek_end);
#if(DEBUG_LEVEL == 1)
print_time(mosek_start, mosek_end, "Mosek solver");
#endif
/* DEBUG */
//printf("r: %d\n", r); fflush(stdout);
/* END DEBUG */
clear_nvector(w,sm->sizePsi);
for (j=0;j<size_active;j++) {
if (alpha[j]>C*ALPHA_THRESHOLD) {
add_vector_ns(w,dXc[j]->fvec,alpha[j]/(1+rho));
}
}
z_k_norm = sqrt(sprod_nn(w,w,sm->sizePsi));
add_vector_nn(w, w_b, sm->sizePsi, rho/(1+rho));
/* detect if step size too small */
sigma_k = 0;
alphasum = 0;
for (j=0;j<size_active;j++) {
sigma_k += alpha[j]*cut_error[j];
alphasum+=alpha[j];
}
sigma_k/=C;
gTd = -C*(sprod_ns(w,new_constraint) - sprod_ns(w_b,new_constraint));
#if (DEBUG_LEVEL>0)
for (j=0;j<size_active;j++) {
printf("alpha[%d]: %.8g, cut_error[%d]: %.8g\n", j, alpha[j], j, cut_error[j]);
}
printf("sigma_k: %.8g\n", sigma_k);
printf("alphasum: %.8g\n", alphasum);
printf("g^T d: %.8g\n", gTd);
fflush(stdout);
#endif
/* update cleanup information */
for (j=0;j<size_active;j++) {
if (alpha[j]<ALPHA_THRESHOLD*C) {
idle[j]++;
} else {
idle[j]=0;
}
}
new_constraint = find_cutting_plane(ex, fycache, &margin, m, sm, sparm, tmpdir, trainfile);
value = margin - sprod_ns(w, new_constraint);
/* print primal objective */
primal_obj = 0.5*sprod_nn(w,w,sm->sizePsi)+C*value;
#if (DEBUG_LEVEL>0)
printf("ITER PRIMAL_OBJ %.4f\n", primal_obj); fflush(stdout);
#endif
temp_var = sprod_nn(w_b,w_b,sm->sizePsi);
proximal_term = 0.0;
for (i=1;i<sm->sizePsi+1;i++) {
proximal_term += (w[i]-w_b[i])*(w[i]-w_b[i]);
}
reg_master_obj = -dual_obj+0.5*rho*temp_var/(1+rho);
expected_descent = reg_master_obj - primal_obj_b;
v_k = (reg_master_obj - proximal_term*rho/2) - primal_obj_b;
primal_lower_bound = MAX(primal_lower_bound, reg_master_obj - 0.5*rho*(1+rho)*proximal_term);
#if (DEBUG_LEVEL>0)
printf("ITER REG_MASTER_OBJ: %.4f\n", reg_master_obj);
printf("ITER EXPECTED_DESCENT: %.4f\n", expected_descent);
printf("ITER PRIMLA_OBJ_B: %.4f\n", primal_obj_b);
printf("ITER RHO: %.4f\n", rho);
printf("ITER ||w-w_b||^2: %.4f\n", proximal_term);
printf("ITER PRIMAL_LOWER_BOUND: %.4f\n", primal_lower_bound);
printf("ITER V_K: %.4f\n", v_k);
#endif
obj_difference = primal_obj - primal_obj_b;
if (primal_obj<primal_obj_b+kappa*expected_descent) {
/* extra condition to be met */
if ((gTd>m2*v_k)||(rho<min_rho+1E-8)) {
#if (DEBUG_LEVEL>0)
printf("SERIOUS STEP\n");
#endif
/* update cut_error */
for (i=0;i<size_active;i++) {
cut_error[i] -= (primal_obj_b - 0.5*sprod_nn(w_b,w_b,sm->sizePsi));
cut_error[i] -= C*sprod_ns(w_b, dXc[i]->fvec);
cut_error[i] += (primal_obj - 0.5*sprod_nn(w,w,sm->sizePsi));
cut_error[i] += C*sprod_ns(w, dXc[i]->fvec);
}
primal_obj_b = primal_obj;
for (i=1;i<sm->sizePsi+1;i++) {
w_b[i] = w[i];
}
null_step = 0;
serious_counter++;
} else {
/* increase step size */
#if (DEBUG_LEVEL>0)
printf("NULL STEP: SS(ii) FAILS.\n");
#endif
serious_counter--;
rho = MAX(rho/10,min_rho);
}
} else { /* no sufficient decrease */
serious_counter--;
if ((cut_error[size_active-1]>m3*last_sigma_k)&&(fabs(obj_difference)>last_z_k_norm+last_sigma_k)) {
#if (DEBUG_LEVEL>0)
printf("NULL STEP: NS(ii) FAILS.\n");
#endif
rho = MIN(10*rho,max_rho);
}
#if (DEBUG_LEVEL>0)
else printf("NULL STEP\n");
#endif
}
/* update last_sigma_k */
last_sigma_k = sigma_k;
last_z_k_norm = z_k_norm;
/* break away from while loop if more than certain proportioal decrease in primal objective */
if (primal_obj_b/initial_primal_obj<1-decrease_proportion) {
suff_decrease_cond = 1;
}
/* clean up */
if (iter % CLEANUP_CHECK == 0) {
size_active = resize_cleanup(size_active, idle, alpha, delta, gammaG0, proximal_rhs, G, dXc, cut_error);
}
time(&iter_end);
#if (DEBUG_LEVEL==1)
char msg[20];
sprintf(msg,"ITER %d",iter);
print_time(iter_start, iter_end, msg);
#endif
} // end cutting plane while loop
printf(" Inner loop optimization finished.\n"); fflush(stdout);
/* free memory */
for (j=0;j<size_active;j++) {
free(G[j]);
free_example(dXc[j],0);
}
free(G);
free(dXc);
free(alpha);
free(delta);
free_svector(new_constraint);
free(idle);
free(gammaG0);
free(proximal_rhs);
free(cut_error);
/* copy and free */
for (i=1;i<sm->sizePsi+1;i++) {
w[i] = w_b[i];
}
free(w_b);
return(primal_obj_b);
}
int main(int argc, char* argv[]) {
double *w; /* weight vector */
int outer_iter;
long m, i;
double C, epsilon;
LEARN_PARM learn_parm;
KERNEL_PARM kernel_parm;
char trainfile[1024];
char modelfile[1024];
int MAX_ITER;
/* new struct variables */
SVECTOR **fycache, *diff, *fy;
EXAMPLE *ex;
SAMPLE sample;
STRUCT_LEARN_PARM sparm;
STRUCTMODEL sm;
double decrement;
double primal_obj, last_primal_obj;
double cooling_eps;
double stop_crit;
LATENT_VAR *imputed_h = NULL;
time_t time_start, time_end;
/* read input parameters */
my_read_input_parameters(argc, argv, trainfile, modelfile, &learn_parm, &kernel_parm, &sparm);
epsilon = learn_parm.eps;
C = learn_parm.svm_c;
MAX_ITER = learn_parm.maxiter;
/* read in examples */
//strcpy(trainfile, "dataset/reidel_trainSVM.small.data");
sample = read_struct_examples(trainfile,&sparm);
//test_print(sample);
ex = sample.examples;
m = sample.n;
/* initialization */
init_struct_model(sample,&sm,&sparm,&learn_parm,&kernel_parm);
// ex[2].h.mention_labels[1] = 5;
// ex[2].h.mention_labels[0] = 5;
// SVECTOR *fvec1 = psi(ex[2].x,ex[2].y,ex[2].h,&sm, &sparm);
// exit(0);
w = create_nvector(sm.sizePsi);
clear_nvector(w, sm.sizePsi);
sm.w = w; /* establish link to w, as long as w does not change pointer */
// Testing: infer_latent_variables(ex[0].x, ex[0].y ,&sm, &sparm);
// exit(0);
// Testing loss function
// LABEL y, ybar;
// y.num_relations = 0; //y.relations = (int*)malloc(sizeof(int)*3); y.relations[0] = 5; y.relations[1] = 7; y.relations[2] = 8;
// ybar.num_relations = 2; ybar.relations = (int*)malloc(sizeof(int)*2); ybar.relations[0] = 5; ybar.relations[1] = 7;
// LATENT_VAR hbar;
// double lossval = loss(y,ybar,hbar,&sparm);
// printf("Loss Val : %f\n", lossval);
// exit(0);
/* some training information */
printf("C: %.8g\n", C);
printf("epsilon: %.8g\n", epsilon);
printf("sample.n: %ld\n", sample.n);
printf("sm.sizePsi: %ld\n", sm.sizePsi); fflush(stdout);
printf("tmpfile directory : %s\n", learn_parm.tmpdir);
/* impute latent variable for first iteration */
// Ajay: Already initialised in read_struct_examples
//init_latent_variables(&sample,&learn_parm,&sm,&sparm);
/* prepare feature vector cache for correct labels with imputed latent variables */
fycache = (SVECTOR**)malloc(m*sizeof(SVECTOR*));
for (i=0;i<m;i++) {
fy = psi(ex[i].x, ex[i].y, ex[i].h, &sm, &sparm);
diff = add_list_ss(fy);
free_svector(fy);
fy = diff;
fycache[i] = fy;
}
/* time taken stats */
time(&time_start);
/* outer loop: latent variable imputation */
outer_iter = 0;
last_primal_obj = 0;
decrement = 0;
cooling_eps = 0.5*C*epsilon;
while ((outer_iter<2)||((!stop_crit)&&(outer_iter<MAX_OUTER_ITER))) {
printf("OUTER ITER %d\n", outer_iter); fflush(stdout);
/* cutting plane algorithm */
time_t cp_start, cp_end;
time(&cp_start);
primal_obj = cutting_plane_algorithm(w, m, MAX_ITER, C, cooling_eps, fycache, ex, &sm, &sparm, learn_parm.tmpdir, trainfile);
time(&cp_end);
#if(DEBUG_LEVEL==1)
char msg[20];
sprintf(msg,"OUTER ITER %d", outer_iter);
print_time(cp_start, cp_end, msg);
#endif
/* compute decrement in objective in this outer iteration */
decrement = last_primal_obj - primal_obj;
last_primal_obj = primal_obj;
printf("primal objective: %.4f\n", primal_obj);
printf("decrement: %.4f\n", decrement); fflush(stdout);
stop_crit = (decrement<C*epsilon)&&(cooling_eps<0.5*C*epsilon+1E-8);
cooling_eps = -decrement*0.01;
cooling_eps = MAX(cooling_eps, 0.5*C*epsilon);
printf("cooling_eps: %.8g\n", cooling_eps);
/* impute latent variable using updated weight vector */
for(i = 0; i < m; i ++)
free_latent_var(ex[i].h);
if(imputed_h != NULL)
free(imputed_h);
imputed_h = (LATENT_VAR*)malloc(sizeof(LATENT_VAR) * m);
infer_latent_variables_all(imputed_h, &sm, &sparm, m, learn_parm.tmpdir, trainfile);
for (i=0;i<m;i++) {
// free_latent_var(ex[i].h);
// ex[i].h = infer_latent_variables(ex[i].x, ex[i].y, &sm, &sparm); // ILP for Pr (Z | Y_i, X_i) in our case
ex[i].h = imputed_h[i];
}
/* re-compute feature vector cache */
for (i=0;i<m;i++) {
free_svector(fycache[i]);
fy = psi(ex[i].x, ex[i].y, ex[i].h, &sm, &sparm);
diff = add_list_ss(fy);
free_svector(fy);
fy = diff;
fycache[i] = fy;
}
outer_iter++;
} // end outer loop
/* write structural model */
write_struct_model(modelfile, &sm, &sparm);
// skip testing for the moment
/* free memory */
free_struct_sample(sample);
free_struct_model(sm, &sparm);
for(i=0;i<m;i++) {
free_svector(fycache[i]);
}
free(fycache);
time(&time_end);
#if (DEBUG_LEVEL==1)
print_time(time_start, time_end, "Total time");
#endif
return(0);
}
void print_time(time_t time_start, time_t time_end, char *msg){
double time_taken = (double)(time_end - time_start)/60;
printf("%s: %f mins\n", msg, time_taken);
fflush(stdout);
}
void my_read_input_parameters(int argc, char *argv[], char *trainfile, char* modelfile,
LEARN_PARM *learn_parm, KERNEL_PARM *kernel_parm, STRUCT_LEARN_PARM *struct_parm) {
long i;
/* set default */
learn_parm->maxiter=20000;
learn_parm->svm_maxqpsize=100;
learn_parm->svm_c=100.0;
//learn_parm->eps=0.001;
learn_parm->eps=0.1; //AJAY: Changing for faster convergence
learn_parm->biased_hyperplane=12345; /* store random seed */
learn_parm->remove_inconsistent=10;
kernel_parm->kernel_type=0;
kernel_parm->rbf_gamma=0.05;
kernel_parm->coef_lin=1;
kernel_parm->coef_const=1;
kernel_parm->poly_degree=3;
struct_parm->custom_argc=0;
for(i=1;(i<argc) && ((argv[i])[0] == '-');i++) {
switch ((argv[i])[1]) {
case 'c': i++; learn_parm->svm_c=atof(argv[i]); break;
case 'e': i++; learn_parm->eps=atof(argv[i]); break;
case 's': i++; learn_parm->svm_maxqpsize=atol(argv[i]); break;
case 'g': i++; kernel_parm->rbf_gamma=atof(argv[i]); break;
case 'd': i++; kernel_parm->poly_degree=atol(argv[i]); break;
case 'r': i++; learn_parm->biased_hyperplane=atol(argv[i]); break;
case 't': i++; kernel_parm->kernel_type=atol(argv[i]); break;
case 'n': i++; learn_parm->maxiter=atol(argv[i]); break;
case 'p': i++; learn_parm->remove_inconsistent=atol(argv[i]); break;
case 'f': i++; strcpy(learn_parm->tmpdir,argv[i]); printf("Tmp file is %s\n",learn_parm->tmpdir); break;
case '-': strcpy(struct_parm->custom_argv[struct_parm->custom_argc++],argv[i]);i++; strcpy(struct_parm->custom_argv[struct_parm->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");
my_wait_any_key();
exit(0);
}
strcpy (trainfile, argv[i]);
if((i+1)<argc) {
strcpy (modelfile, argv[i+1]);
}
parse_struct_parameters(struct_parm);
}
void my_wait_any_key()
{
printf("\n(more)\n");
(void)getc(stdin);
}
int resize_cleanup(int size_active, int *idle, double *alpha, double *delta, double *gammaG0, double *proximal_rhs, double **G, DOC **dXc, double *cut_error) {
int i,j, new_size_active;
long k;
i=0;
while ((i<size_active)&&(idle[i]<IDLE_ITER)) i++;
j=i;
while((j<size_active)&&(idle[j]>=IDLE_ITER)) j++;
while (j<size_active) {
/* copying */
alpha[i] = alpha[j];
delta[i] = delta[j];
gammaG0[i] = gammaG0[j];
cut_error[i] = cut_error[j];
free(G[i]);
G[i] = G[j];
G[j] = NULL;
free_example(dXc[i],0);
dXc[i] = dXc[j];
dXc[j] = NULL;
i++;
j++;
while((j<size_active)&&(idle[j]>=IDLE_ITER)) j++;
}
for (k=i;k<size_active;k++) {
if (G[k]!=NULL) free(G[k]);
if (dXc[k]!=NULL) free_example(dXc[k],0);
}
new_size_active = i;
alpha = (double*)realloc(alpha, sizeof(double)*new_size_active);
delta = (double*)realloc(delta, sizeof(double)*new_size_active);
gammaG0 = (double*)realloc(gammaG0, sizeof(double)*new_size_active);
proximal_rhs = (double*)realloc(proximal_rhs, sizeof(double)*new_size_active);
G = (double**)realloc(G, sizeof(double*)*new_size_active);
dXc = (DOC**)realloc(dXc, sizeof(DOC*)*new_size_active);
cut_error = (double*)realloc(cut_error, sizeof(double)*new_size_active);
/* resize G and idle */
i=0;
while ((i<size_active)&&(idle[i]<IDLE_ITER)) i++;
j=i;
while((j<size_active)&&(idle[j]>=IDLE_ITER)) j++;
while (j<size_active) {
idle[i] = idle[j];
for (k=0;k<new_size_active;k++) {
G[k][i] = G[k][j];
}
i++;
j++;
while((j<size_active)&&(idle[j]>=IDLE_ITER)) j++;
}
idle = (int*)realloc(idle, sizeof(int)*new_size_active);
for (k=0;k<new_size_active;k++) {
G[k] = (double*)realloc(G[k], sizeof(double)*new_size_active);
}
return(new_size_active);
}