-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvm_struct_latent_spl.c
executable file
·1269 lines (1070 loc) · 34.2 KB
/
svm_struct_latent_spl.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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************************/
/* */
/* svm_struct_latent_spl.c */
/* */
/* Main Optimization Code for Latent SVM^struct using Self-Paced */
/* Learning. NOTE: This implementation modifies the CCCP code by */
/* Chun-Nam Yu, specifically the file svm_struct_latent_cccp.c, */
/* which is a part of the Latent SVM^struct package available on */
/* Chun-Nam Yu's webpage. */
/* */
/* Authors: M. Pawan Kumar and Ben Packer */
/* */
/* 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 <stdio.h>
#include <assert.h>
#include <float.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "svm_struct_latent_api.h"
#include "./svm_light/svm_learn.h"
#define ALPHA_THRESHOLD 1E-14
#define IDLE_ITER 20
#define CLEANUP_CHECK 50
#define STOP_PREC 1E-2
#define UPDATE_BOUND 3
#define MAX_CURRICULUM_ITER 10
#define MAX_OUTER_ITER 400
#define MAX(x,y) ((x) < (y) ? (y) : (x))
#define MIN(x,y) ((x) > (y) ? (y) : (x))
#define DEBUG_LEVEL 0
int mosek_qp_optimize(double**, double*, double*, long, double, double*);
void my_read_input_parameters(int argc, char* argv[], char *trainfile, char *modelfile, char *init_modelfile, char *objfile,
LEARN_PARM *learn_parm, KERNEL_PARM *kernel_parm, STRUCT_LEARN_PARM *struct_parm,
double *init_spl_weight, double *spl_factor);
void my_wait_any_key();
int resize_cleanup(int size_active, int **ptr_idle, double **ptr_alpha, double **ptr_delta, DOC ***ptr_dXc,
double ***ptr_G, int *mv_iter);
void approximate_to_psd(double **G, int size_active, double eps);
void Jacobi_Cyclic_Method(double eigenvalues[], double *eigenvectors, double *A, int n);
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);
}
double current_obj_val(EXAMPLE *ex, long m, STRUCTMODEL *sm, STRUCT_LEARN_PARM *sparm, double C, int *valid_examples) {
long i;
SVECTOR *f, *fy, *fybar, *lhs;
LABEL ybar;
LATENT_VAR hbar;
double lossval, margin;
double *new_constraint;
double obj = 0.0;
/* find cutting plane */
lhs = NULL;
margin = 0;
for (i=0;i<m;i++) {
if(!valid_examples[i])
continue;
find_most_violated_constraint_marginrescaling(&ex[i].x, ex[i].y, &ex[i].h, &ybar, &hbar, sm, sparm);
/* get difference vector */
//fy = copy_svector(fycache[i]);
fy = psi(ex[i].x, ex[i].y, ex[i].h, sm, sparm);
fybar = psi(ex[i].x,ybar,hbar,sm,sparm);
lossval = loss(ex[i].y,ybar,hbar,sparm);
// added by aseem
//free_label(ybar);
free_latent_var(hbar, ex[i].x);
/* 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;
}
/* compact the linear representation */
new_constraint = add_list_nn(lhs, sm->sizePsi);
free_svector(lhs);
obj = margin;
for(i = 1; i < sm->sizePsi+1; i++)
obj -= new_constraint[i]*sm->w[i];
if(obj < 0.0)
obj = 0.0;
obj *= C;
for(i = 1; i < sm->sizePsi+1; i++)
obj += 0.5*sm->w[i]*sm->w[i];
free(new_constraint);
return obj;
}
int compar(const void *a, const void *b)
{
sortStruct *c = (sortStruct *) a;
sortStruct *d = (sortStruct *) b;
if(c->val < d->val)
return -1;
if(c->val > d->val)
return 1;
return 0;
}
SVECTOR* find_cutting_plane(EXAMPLE *ex, double *margin, long m, STRUCTMODEL *sm, STRUCT_LEARN_PARM *sparm,
int *valid_examples) {
long i;
SVECTOR *f, *fy, *fybar, *lhs;
LABEL ybar;
LATENT_VAR hbar;
double lossval;
double *new_constraint;
long valid_count = 0;
long l,k;
SVECTOR *fvec;
WORD *words;
/* find cutting plane */
lhs = NULL;
*margin = 0;
for (i=0;i<m;i++) {
if (valid_examples[i]) {
valid_count++;
}
}
for (i=0;i<m;i++) {
if (!valid_examples[i]) {
continue;
}
find_most_violated_constraint_marginrescaling(&ex[i].x, ex[i].y, &ex[i].h, &ybar, &hbar, sm, sparm);
/* get difference vector */
//fy = copy_svector(fycache[i]);
fy = psi(ex[i].x, ex[i].y, ex[i].h, sm, sparm);
fybar = psi(ex[i].x,ybar,hbar,sm,sparm);
lossval = loss(ex[i].y,ybar,hbar,sparm);
free_label(ybar);
free_latent_var(hbar, ex[i].x);
/* scale difference vector */
for (f=fy;f;f=f->next) {
//f->factor*=1.0/m;
//f->factor*=ex[i].x.example_cost/m;
f->factor*=ex[i].x.example_cost/valid_count;
}
for (f=fybar;f;f=f->next) {
//f->factor*=-1.0/m;
//f->factor*=-ex[i].x.example_cost/m;
f->factor*=-ex[i].x.example_cost/valid_count;
}
/* 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;
*margin+=lossval*ex[i].x.example_cost/valid_count;
}
/* 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, EXAMPLE *ex,
STRUCTMODEL *sm, STRUCT_LEARN_PARM *sparm, int *valid_examples) {
long i,j;
double *alpha;
DOC **dXc; /* constraint matrix */
double *delta; /* rhs of constraints */
SVECTOR *new_constraint;
int iter, size_active;
double value;
double threshold = 0.0;
double margin;
double primal_obj, cur_obj;
double *cur_slack = NULL;
int mv_iter;
int *idle = NULL;
double **G = NULL;
SVECTOR *f;
int r;
/* set parameters for hideo solver */
LEARN_PARM lparm;
KERNEL_PARM kparm;
MODEL *svm_model=NULL;
lparm.biased_hyperplane = 0;
lparm.epsilon_crit = MIN(epsilon,0.001);
lparm.svm_c = C;
lparm.sharedslack = 1;
kparm.kernel_type = LINEAR;
lparm.remove_inconsistent=0;
lparm.skip_final_opt_check=0;
lparm.svm_maxqpsize=10;
lparm.svm_newvarsinqp=0;
lparm.svm_iter_to_shrink=-9999;
lparm.maxiter=100000;
lparm.kernel_cache_size=40;
lparm.eps = epsilon;
lparm.transduction_posratio=-1.0;
lparm.svm_costratio=1.0;
lparm.svm_costratio_unlab=1.0;
lparm.svm_unlabbound=1E-5;
lparm.epsilon_a=1E-10; /* changed from 1e-15 */
lparm.compute_loo=0;
lparm.rho=1.0;
lparm.xa_depth=0;
strcpy(lparm.alphafile,"");
kparm.poly_degree=3;
kparm.rbf_gamma=1.0;
kparm.coef_lin=1;
kparm.coef_const=1;
strcpy(kparm.custom,"empty");
iter = 0;
size_active = 0;
alpha = NULL;
dXc = NULL;
delta = NULL;
printf("Running structural SVM solver: "); fflush(stdout);
mine_negative_latent_variables(ex[0].x, &ex[0].h, sm);
new_constraint = find_cutting_plane(ex, &margin, m, sm, sparm, valid_examples);
value = margin - sprod_ns(w, new_constraint);
while((iter<MAX_ITER)) {
if(value <= (threshold+epsilon)){
mine_negative_latent_variables(ex[0].x, &ex[0].h, sm);
new_constraint = find_cutting_plane(ex, &margin, m, sm, sparm, valid_examples);
value = margin - sprod_ns(w, new_constraint);
if(value<=(threshold+epsilon)){
break;
}
}
iter+=1;
size_active+=1;
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;
// 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);
// hack: add a constant to the diagonal to make sure G is PSD
G[size_active-1][size_active-1] += 1e-6;
// solve QP to update alpha
r = mosek_qp_optimize(G, delta, alpha, (long) size_active, C, &cur_obj);
if(r >= 1293 && r <= 1296)
{
printf("r:%d. G might not be psd due to numerical errors.\n",r);
exit(1);
}
else if(r)
{
printf("Error %d in mosek_qp_optimize: Check ${MOSEKHOME}/${VERSION}/tools/platform/${PLATFORM}/h/mosek.h\n",r);
exit(1);
}
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]);
idle[j] = 0;
}
else
idle[j]++;
}
cur_slack = (double *) realloc(cur_slack,sizeof(double)*size_active);
for(i = 0; i < size_active; i++) {
cur_slack[i] = 0.0;
for(f = dXc[i]->fvec; f; f = f->next) {
j = 0;
while(f->words[j].wnum) {
cur_slack[i] += w[f->words[j].wnum]*f->words[j].weight;
j++;
}
}
if(cur_slack[i] >= delta[i])
cur_slack[i] = 0.0;
else
cur_slack[i] = delta[i]-cur_slack[i];
}
mv_iter = 0;
if(size_active > 1) {
for(j = 0; j < size_active; j++) {
if(cur_slack[j] >= cur_slack[mv_iter])
mv_iter = j;
}
}
if(size_active > 1)
threshold = cur_slack[mv_iter];
else
threshold = 0.0;
new_constraint = find_cutting_plane(ex, &margin, m, sm, sparm, valid_examples);
value = margin - sprod_ns(w, new_constraint);
if((iter % CLEANUP_CHECK) == 0)
{
printf("+"); fflush(stdout);
size_active = resize_cleanup(size_active, &idle, &alpha, &delta, &dXc, &G, &mv_iter);
}
} // end cutting plane while loop
primal_obj = current_obj_val(ex, m, sm, sparm, C, valid_examples);
printf(" Inner loop optimization finished.\n"); fflush(stdout);
/* free memory */
for (j=0;j<size_active;j++) {
free(G[j]);
free_example(dXc[j],1);
}
free(G);
free(dXc);
free(alpha);
free(delta);
free_svector(new_constraint);
free(cur_slack);
free(idle);
if (svm_model!=NULL) free_model(svm_model,0);
return(primal_obj);
}
int check_acs_convergence(int *prev_valid_examples, int *valid_examples, long m)
{
long i;
int converged = 1;
for (i=0;i<m;i++) {
if (prev_valid_examples[i] != valid_examples[i]) {
converged = 0;
break;
}
}
return converged;
}
int update_valid_examples(double *w, long m, double C, EXAMPLE *ex, STRUCTMODEL *sm, STRUCT_LEARN_PARM *sparm, int *valid_examples, double spl_weight)
{
long i, j;
/* if self-paced learning weight is non-positive, all examples are valid */
if(spl_weight <= 0.0) {
for (i=0;i<m;i++)
valid_examples[i] = 1;
return (m);
}
sortStruct *slack = (sortStruct *) malloc(m*sizeof(sortStruct));
LABEL ybar;
LATENT_VAR hbar;
SVECTOR *f, *fy, *fybar;
//double lossval;
double penalty = 1.0/spl_weight;
if(penalty < 0.0)
penalty = DBL_MAX;
for (i=0;i<m;i++) {
find_most_violated_constraint_marginrescaling(&ex[i].x, ex[i].y, &ex[i].h, &ybar, &hbar, sm, sparm);
//fy = copy_svector(fycache[i]);
fy = psi(ex[i].x, ex[i].y, ex[i].h, sm, sparm);
fybar = psi(ex[i].x,ybar,hbar,sm,sparm);
slack[i].index = i;
slack[i].val = loss(ex[i].y,ybar,hbar,sparm);
// added by aseem
//free_label(ybar);
//free_latent_var(hbar, ex[i].x);
for (f=fy;f;f=f->next) {
j = 0;
while (1) {
if(!f->words[j].wnum)
break;
slack[i].val -= sm->w[f->words[j].wnum]*f->words[j].weight;
j++;
}
}
for (f=fybar;f;f=f->next) {
j = 0;
while (1) {
if(!f->words[j].wnum)
break;
slack[i].val += sm->w[f->words[j].wnum]*f->words[j].weight;
j++;
}
}
free_svector(fy);
free_svector(fybar);
}
qsort(slack,m,sizeof(sortStruct),&compar);
int nValid = 0;
for (i=0;i<m;i++)
valid_examples[i] = 0;
for (i=0;i<m;i++) {
if(slack[i].val*C/m > penalty)
break;
valid_examples[slack[i].index] = 1;
nValid++;
}
free(slack);
return nValid;
}
double alternate_convex_search(double *w, long m, int MAX_ITER, double C, double epsilon, EXAMPLE *ex,
STRUCTMODEL *sm, STRUCT_LEARN_PARM *sparm, int *valid_examples, double spl_weight)
{
long i;
int iter = 0, converged, nValid;
double last_relaxed_primal_obj = DBL_MAX, relaxed_primal_obj, decrement;
int *prev_valid_examples = (int *) malloc(m*sizeof(int));
double *best_w = (double *) malloc((sm->sizePsi+1)*sizeof(double));
for (i=0;i<sm->sizePsi+1;i++)
best_w[i] = w[i];
nValid = update_valid_examples(w, m, C, ex, sm, sparm, valid_examples, spl_weight);
/*last_relaxed_primal_obj = current_obj_val(ex, m, sm, sparm, C, valid_examples);
if(nValid < m)
last_relaxed_primal_obj += (double)(m-nValid)/((double)spl_weight);*/
for (i=0;i<m;i++) {
prev_valid_examples[i] = 0;
}
for (iter=0;;iter++) {
nValid = update_valid_examples(w, m, C, ex, sm, sparm, valid_examples, spl_weight);
printf("ACS Iteration %d: number of examples = %d\n",iter,nValid); fflush(stdout);
converged = check_acs_convergence(prev_valid_examples,valid_examples,m);
if(converged)
break;
for (i=0;i<sm->sizePsi+1;i++)
w[i] = 0.0;
relaxed_primal_obj = cutting_plane_algorithm(w, m, MAX_ITER, C, epsilon, ex, sm, sparm, valid_examples);
/*if(nValid < m)
relaxed_primal_obj += (double)(m-nValid)/((double)spl_weight);
decrement = last_relaxed_primal_obj-relaxed_primal_obj;
printf("relaxed primal objective: %.4f\n", relaxed_primal_obj);
if (iter) {
printf("decrement: %.4f\n", decrement); fflush(stdout);
}
else {
printf("decrement: N/A\n"); fflush(stdout);
}
if (decrement>=0.0) {
for (i=0;i<sm->sizePsi+1;i++) {
best_w[i] = w[i];
}
}
if (decrement <= C*epsilon) {
break;
}
last_relaxed_primal_obj = relaxed_primal_obj;*/
for (i=0;i<sm->sizePsi+1;i++) {
best_w[i] = w[i];
}
for (i=0;i<m;i++) {
prev_valid_examples[i] = valid_examples[i];
}
}
for (i=0;i<m;i++) {
prev_valid_examples[i] = 1;
}
if (iter) {
for (i=0;i<sm->sizePsi+1;i++) {
w[i] = best_w[i];
}
}
//double primal_obj;
//primal_obj = current_obj_val(ex, m, sm, sparm, C, prev_valid_examples);
free(prev_valid_examples);
free(best_w);
return(relaxed_primal_obj);
}
long *randperm(long m)
{
long *perm = (long *) malloc(sizeof(long)*m);
long *map = (long *) malloc(sizeof(long)*m);
long i, j;
for(i = 0; i < m; i++)
map[i] = i;
srand(time(NULL));
for(i = 0; i < m; i++)
{
int r = (int) (((double) m-i)*((double) rand())/(RAND_MAX+1.0));
perm[i] = map[r];
for(j = r; j < m-1; j++)
map[j] = map[j+1];
}
free(map);
return perm;
}
SAMPLE generate_train_set(SAMPLE alldata, long *perm, int ntrain)
{
SAMPLE train;
train.n = ntrain;
long i;
train.examples = (EXAMPLE *) malloc(train.n*sizeof(EXAMPLE));
for(i = 0; i < train.n; i++)
{
train.examples[i] = alldata.examples[perm[i]];
}
return train;
}
SAMPLE generate_validation_set(SAMPLE alldata, long *perm, int ntrain)
{
SAMPLE val;
val.n = alldata.n - ntrain;
long i;
val.examples = (EXAMPLE *) malloc(val.n*sizeof(EXAMPLE));
for(i = 0; i < val.n; i++)
{
val.examples[i] = alldata.examples[perm[ntrain+i]];
}
return val;
}
/*aseem double compute_current_loss(SAMPLE val, STRUCTMODEL *sm, STRUCT_LEARN_PARM *sparm)
{
long i;
LABEL y;
LATENT_VAR h;
double cur_loss = 0.0;
double store;
for(i = 0; i < val.n; i++)
{
classify_struct_example(val.examples[i].x,&y,&h,sm,sparm);
store = loss(val.examples[i].y,y,h,sparm);
cur_loss += store;
}
cur_loss /= (double) val.n;
return cur_loss;
}*/
double negative_mine_loop(double *w, long m, int MAX_ITER, double C, double epsilon, EXAMPLE *ex,
STRUCTMODEL *sm, STRUCT_LEARN_PARM *sparm, int *valid_examples, double spl_weight)
{
double decrement;
double primal_obj, last_primal_obj;
double stop_crit;
int iter = 0;
last_primal_obj = DBL_MAX;
decrement = 0;
while ((iter<2)||(!stop_crit)) {
printf("NEG MINE ITER %d\n", iter); fflush(stdout);
mine_negative_latent_variables(ex[0].x, &ex[0].h, sm);
primal_obj = alternate_convex_search(w, m, MAX_ITER, C, epsilon, ex, sm, sparm, valid_examples, spl_weight);
decrement = last_primal_obj - primal_obj;
last_primal_obj = primal_obj;
printf("neg mine primal objective: %.4f\n", primal_obj);
if (iter) {
printf(" neg mine decrement: %.4f\n", decrement); fflush(stdout);
}
else {
printf("neg mine decrement: N/A\n"); fflush(stdout);
}
stop_crit = (decrement<C*epsilon);
iter++;
}
return(primal_obj);
}
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];
char init_modelfile[1024];
char objfile[1024];
int MAX_ITER;
/* new struct variables */
//SVECTOR **fycache, *diff, *fy;
EXAMPLE *ex;
SAMPLE alldata;
SAMPLE sample;
SAMPLE val;
STRUCT_LEARN_PARM sparm;
STRUCTMODEL sm;
double decrement;
double primal_obj, last_primal_obj;
double stop_crit;
char itermodelfile[2000];
/* self-paced learning variables */
double init_spl_weight;
double spl_weight;
double spl_factor;
int *valid_examples;
/* read input parameters */
my_read_input_parameters(argc, argv, trainfile, modelfile, init_modelfile, objfile, &learn_parm, &kernel_parm, &sparm,
&init_spl_weight, &spl_factor);
epsilon = learn_parm.eps;
C = learn_parm.svm_c;
MAX_ITER = learn_parm.maxiter;
/* read in examples */
alldata = read_struct_examples(trainfile,&sparm);
int ntrain = (int) round(1.0*alldata.n); /* no validation set */
if(ntrain < alldata.n)
{
long *perm = randperm(alldata.n);
sample = generate_train_set(alldata, perm, ntrain);
val = generate_validation_set(alldata, perm, ntrain);
free(perm);
}
else
{
sample = alldata;
}
ex = sample.examples;
m = sample.n;
/* initialization */
init_struct_model(alldata,&sm,&sparm,&learn_parm,&kernel_parm);
w = create_nvector(sm.sizePsi);
clear_nvector(w, sm.sizePsi);
// added by aseem
if (sparm.isInitByBinSVM){
sm = read_struct_model(init_modelfile, &sparm);
for (i=0;i<sm.sizePsi+1;i++)
w[i] = sm.w[i];
}// added by aseem
sm.w = w; /* establish link to w, as long as w does not change pointer */
/* some training information */
printf("C: %.8g\n", C);
printf("spl weight: %.8g\n",init_spl_weight);
printf("epsilon: %.8g\n", epsilon);
printf("sample.n: %d\n", sample.n);
printf("sm.sizePsi: %ld\n", sm.sizePsi); fflush(stdout);
/* impute latent variable for first iteration */
init_latent_variables(&sample,&learn_parm,&sm,&sparm);
// added by aseem. impute latent variable using updated weight vector
outer_iter = 0;
int latent_update = 0;
if (sparm.isInitByBinSVM){
//infer_latent_variables(ex[0].x, ex[0].y, &ex[0].h, &sm, &sparm);
infer_latent_variables(ex[0].x, ex[0].y, &ex[0].h, &sm, &sparm, sparm.initIter);
outer_iter = 1 + sparm.initIter;
latent_update = 1 + sparm.initIter;
}
// added by aseem
/* 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;
}*/
/* learn initial weight vector using all training examples */
valid_examples = (int *) malloc(m*sizeof(int));
if (init_spl_weight>0.0) {
printf("INITIALIZATION\n"); fflush(stdout);
for (i=0;i<m;i++) {
valid_examples[i] = 1;
}
int initIter;
for (initIter=0;initIter<2;initIter++) {
primal_obj = cutting_plane_algorithm(w, m, MAX_ITER, C, epsilon, ex, &sm, &sparm, valid_examples);
for (i=0;i<m;i++) {
//aseem free_latent_var(ex[i].h);
//aseem ex[i].h = infer_latent_variables(ex[i].x, ex[i].y, &sm, &sparm);
}
/*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 loop: latent variable imputation */
//aseem outer_iter = 0;
if (sparm.isInitByBinSVM){
update_valid_examples(w, m, C, ex, &sm, &sparm, valid_examples, init_spl_weight);
mine_negative_latent_variables(ex[0].x, &ex[0].h, &sm);
last_primal_obj = current_obj_val(ex, m, &sm, &sparm, C, valid_examples);
}
else{
last_primal_obj = DBL_MAX;
}
decrement = 0;
/* errors for validation set */
// aseem double cur_loss, best_loss = DBL_MAX;
// aseem int loss_iter;
/* initializations */
spl_weight = init_spl_weight;
while ((outer_iter<2)||((!stop_crit)&&(outer_iter<MAX_OUTER_ITER))) {
printf("OUTER ITER %d\n", outer_iter); fflush(stdout);
// cutting plane algorithm
//primal_obj = cutting_plane_algorithm(w, m, MAX_ITER, C, epsilon, fycache, ex, &sm, &sparm, valid_examples);
//primal_obj = cutting_plane_algorithm(w, m, MAX_ITER, C, epsilon, ex, &sm, &sparm, valid_examples);
// solve biconvex self-paced learning problem
//primal_obj = negative_mine_loop(w, m, MAX_ITER, C, epsilon, ex, &sm, &sparm, valid_examples, spl_weight);
primal_obj = alternate_convex_search(w, m, MAX_ITER, C, epsilon, ex, &sm, &sparm, valid_examples, spl_weight);
int nValid = 0;
for (i=0;i<m;i++) {
if(valid_examples[i]) {
nValid++;
}
}
// 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);
if (outer_iter) {
printf("decrement: %.4f\n", decrement); fflush(stdout);
}
else {
printf("decrement: N/A\n"); fflush(stdout);
}
stop_crit = (decrement<C*epsilon);
// additional stopping criteria
if(nValid < m)
stop_crit = 0;
if(!latent_update)
stop_crit = 0;
// impute latent variable using updated weight vector
if(nValid) {
for (i=0;i<m;i++) {
if(!stop_crit){
infer_latent_variables(ex[i].x, ex[i].y, &ex[i].h, &sm, &sparm, outer_iter);
}
}
latent_update++;
}
sprintf(itermodelfile,"%s.%04d",modelfile,outer_iter);
write_struct_model(itermodelfile, &sm, &sparm);
outer_iter++;
spl_weight /= spl_factor;
} // end outer loop*/
/* write structural model */
write_struct_model(modelfile, &sm, &sparm);
// skip testing for the moment
// write objective function value to file
FILE *objfl = fopen(objfile, "w");
if (objfl==NULL) {
printf("Cannot open model file %s for output!", objfile);
exit(1);
}
fprintf(objfl, "%0.7f\n", last_primal_obj);
fclose(objfl);
/* free memory */
free_struct_sample(alldata);
if(ntrain < alldata.n)
{
free(sample.examples);
free(val.examples);
}
free_struct_model(sm, &sparm);
/*for(i=0;i<m;i++) {
free_svector(fycache[i]);
}
free(fycache);*/
free(valid_examples);
return(0);
}
void my_read_input_parameters(int argc, char *argv[], char *trainfile, char* modelfile, char *init_modelfile, char *objfile,
LEARN_PARM *learn_parm, KERNEL_PARM *kernel_parm, STRUCT_LEARN_PARM *struct_parm,
double *init_spl_weight, double *spl_factor)
{
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->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;
/* default: no self-paced learning */
*init_spl_weight = 0.0;
*spl_factor = 1.3;
struct_parm->custom_argc=0;
struct_parm->min_area_ratios[0] = 70;
struct_parm->min_area_ratios[1] = 70;
struct_parm->min_area_ratios[2] = 65;
struct_parm->min_area_ratios[3] = 60;
struct_parm->min_area_ratios[4] = 55;
struct_parm->min_area_ratios[5] = 50;
for(i=1;(i<argc) && ((argv[i])[0] == '-');i++) {