forked from fizx/libbow-osx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
active.c
2027 lines (1739 loc) · 60.2 KB
/
active.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
/* Weight-setting and scoring implementation for active learning */
/* Copyright (C) 1997, 1998, 1999 Andrew McCallum
Written by: Kamal Nigam <[email protected]>
This file is part of the Bag-Of-Words Library, `libbow'.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License
as published by the Free Software Foundation, version 2.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA */
#include <bow/libbow.h>
#include <math.h>
#include <argp/argp.h>
#include <stdlib.h>
#include <bow/em.h>
typedef enum
{
dkl,
length,
qbc,
randomly,
relevance,
skl,
sve,
uncertainty,
ve,
wkl
} active_selection_type;
typedef struct _active_scores {
int di; /* the doc barrel index of the doc */
double weight; /* weight used for selecting */
bow_score **scores; /* the scores of the doc */
} active_scores;
void active_select_length (bow_barrel *doc_barrel, active_scores *scores,
int num_to_add, int total_unknown, int committee_size);
void active_select_uncertain (bow_barrel *doc_barrel, active_scores *scores,
int num_to_add, int total_unknown, int committee_size);
void active_select_relevant (bow_barrel *doc_barrel, active_scores *scores,
int num_to_add, int total_unknown, int committee_size);
void active_select_random (bow_barrel *doc_barrel, active_scores *scores,
int num_to_add, int total_unknown, int committee_size);
void active_select_qbc (bow_barrel *doc_barrel, active_scores *scores,
int num_to_add, int total_unknown, int committee_size);
void active_select_weighted_kl (bow_barrel *doc_barrel, active_scores *scores,
int num_to_add, int total_unknown, int committee_size);
void active_select_dkl (bow_barrel *doc_barrel, active_scores *scores,
int num_to_add, int total_unknown, int committee_size);
void active_select_vote_entropy (bow_barrel *doc_barrel, active_scores *scores,
int num_to_add, int total_unknown, int committee_size);
void active_select_stream_ve (bow_barrel *doc_barrel, active_scores *scores,
int num_to_add, int total_unknown, int committee_size);
void active_select_stream_kl (bow_barrel *doc_barrel, active_scores *scores,
int num_to_add, int total_unknown, int committee_size);
void active_test (FILE *test_fp, bow_barrel *rainbow_doc_barrel,
bow_barrel *rainbow_class_barrel);
/* The variables that can be changed on the command line, with defaults: */
static int active_add_per_round = 4;
static int active_test_stats = 0;
static int active_committee_size = 1;
static active_selection_type active_selection_method = uncertainty;
static int active_num_rounds = 10;
static void (* active_select_docs)(bow_barrel *, active_scores *, int, int, int) =
active_select_uncertain;
static int active_binary_pos_ci = -1;
static char* active_binary_pos_classname = NULL;
static char* active_secondary_method = "naivebayes";
static int active_final_em = 0;
static int active_print_committee_matrices = 0;
static int active_qbc_low_kl = 0;
static int active_pr_print_stat_summary = 0;
static int active_pr_window_size = 20;
static int active_remap_scores_pr = 0;
static int active_no_final_em = 0;
static double active_alpha = 0.5;
static double active_beta = 5;
static double active_stream_epsilon = 0.3;
static int active_perturb_after_em = 0;
/* The integer or single char used to represent this command-line option.
Make sure it is unique across all libbow and rainbow. */
enum {
ACTIVE_ADD_PER_ROUND = 4000,
ACTIVE_TEST_STATS,
ACTIVE_SELECTION_METHOD,
ACTIVE_NUM_ROUNDS,
ACTIVE_BINARY_POS,
ACTIVE_SECONDARY_METHOD,
ACTIVE_COMMITTEE_SIZE,
ACTIVE_FINAL_EM,
ACTIVE_PRINT_COMMITTEE_MATRICES,
ACTIVE_QBC_LOW_KL,
ACTIVE_PR_PRINT_STAT_SUMMARY,
ACTIVE_PR_WINDOW_SIZE,
ACTIVE_REMAP_SCORES_PR,
ACTIVE_NO_FINAL_EM,
ACTIVE_BETA,
ACTIVE_STREAM_EPSILON,
ACTIVE_PERTURB_AFTER_EM,
};
static struct argp_option active_options[] =
{
{0,0,0,0,
"Active Learning options:", 70},
{"active-add-per-round", ACTIVE_ADD_PER_ROUND, "NUM", 0,
"Specify the number of documents to label each round. The default is 4."},
{"active-test-stats", ACTIVE_TEST_STATS, 0, 0,
"Generate output for test docs every n rounds."},
{"active-selection-method", ACTIVE_SELECTION_METHOD, "METHOD", 0,
"Specify the selection method for picking unlabeled docs. "
"One of uncertainty, relevance, qbc, random. "
"The default is 'uncertainty'."},
{"active-num-rounds", ACTIVE_NUM_ROUNDS, "NUM", 0,
"The number of active learning rounds to perform. The default is 10."},
{"active-binary-pos", ACTIVE_BINARY_POS, "CLASS", 0,
"The name of the positive class for binary classification. Required for"
"relevance sampling."},
{"active-secondary-method", ACTIVE_SECONDARY_METHOD, "METHOD", 0,
"The underlying method for active learning to use. The default is 'naivebayes'."},
{"active-committee-size", ACTIVE_COMMITTEE_SIZE, "NUM", 0,
"The number of committee members to use with QBC. Default is 1."},
{"active-final-em", ACTIVE_FINAL_EM, 0, 0,
"Finish with a full round of EM."},
{"active-print-committee-matrices", ACTIVE_PRINT_COMMITTEE_MATRICES, 0, 0,
"Print the confusion matrix for each committee member at each round."},
{"active-qbc-low-kl", ACTIVE_QBC_LOW_KL, 0, 0,
"Select documents with the lowest kl-divergence instead of the highest."},
{"active-pr-print-stat-summary", ACTIVE_PR_PRINT_STAT_SUMMARY, 0, 0,
"Print the precision recall curves used for score to probability remapping."},
{"active-pr-window-size", ACTIVE_PR_WINDOW_SIZE, "NUM", 0,
"Set the window size for precision-recall score to probability remapping."
"The default is 20."},
{"active-remap-scores-pr", ACTIVE_REMAP_SCORES_PR, 0, 0,
"Remap scores with sneaky precision-recall tricks."},
{"active-no-final-em", ACTIVE_NO_FINAL_EM, 0, 0,
"Finish without a full round of EM."},
{"active-beta", ACTIVE_BETA, "NUM", 0,
"Increase spread of document densities."},
{"active-stream-epsilon", ACTIVE_STREAM_EPSILON, "NUM", 0,
"The rate factor for selecting documents in stream sampling."},
{"active-perturb-after-em", ACTIVE_PERTURB_AFTER_EM, 0, 0,
"Perturb after running EM to create committee members."},
{0, 0}
};
error_t
active_parse_opt (int key, char *arg, struct argp_state *state)
{
switch (key)
{
case ACTIVE_ADD_PER_ROUND:
active_add_per_round = atoi(arg);
break;
case ACTIVE_TEST_STATS:
active_test_stats = 1;
break;
case ACTIVE_SELECTION_METHOD:
if (!strcmp(arg, "uncertainty"))
{
active_selection_method = uncertainty;
active_select_docs = active_select_uncertain;
}
else if (!strcmp(arg, "length"))
{
active_selection_method = length;
active_select_docs = active_select_length;
}
else if (!strcmp(arg, "relevance"))
{
active_selection_method = relevance;
active_select_docs = active_select_relevant;
}
else if (!strcmp(arg, "random"))
{
active_selection_method = randomly;
active_select_docs = active_select_random;
}
else if (!strcmp(arg, "qbc"))
{
active_selection_method = qbc;
active_select_docs = active_select_qbc;
}
else if (!strcmp(arg, "ve"))
{
active_selection_method = ve;
active_select_docs = active_select_vote_entropy;
}
else if (!strcmp(arg, "wkl"))
{
active_selection_method = wkl;
active_select_docs = active_select_weighted_kl;
}
else if (!strcmp(arg, "dkl"))
{
active_selection_method = dkl;
active_select_docs = active_select_dkl;
}
else if (!strcmp(arg, "sve"))
{
active_selection_method = sve;
active_select_docs = active_select_stream_ve;
}
else if (!strcmp(arg, "skl"))
{
active_selection_method = skl;
active_select_docs = active_select_stream_kl;
}
else
bow_error("Invalid argument for --active-selection-method");
break;
case ACTIVE_NUM_ROUNDS:
active_num_rounds = atoi(arg);
break;
case ACTIVE_BINARY_POS:
active_binary_pos_classname = arg;
break;
case ACTIVE_SECONDARY_METHOD:
active_secondary_method = arg;
break;
case ACTIVE_COMMITTEE_SIZE:
active_committee_size = atoi (arg);
break;
case ACTIVE_FINAL_EM:
active_final_em = 1;
break;
case ACTIVE_PRINT_COMMITTEE_MATRICES:
active_print_committee_matrices = 1;
break;
case ACTIVE_QBC_LOW_KL:
active_qbc_low_kl = 1;
break;
case ACTIVE_REMAP_SCORES_PR:
active_remap_scores_pr = 1;
break;
case ACTIVE_PR_WINDOW_SIZE:
active_pr_window_size = atoi (arg);
break;
case ACTIVE_PR_PRINT_STAT_SUMMARY:
active_pr_print_stat_summary = 1;
break;
case ACTIVE_NO_FINAL_EM:
active_no_final_em = 1;
break;
case ACTIVE_BETA:
active_beta = atof (arg);
break;
case ACTIVE_STREAM_EPSILON:
active_stream_epsilon = atof (arg);
break;
case ACTIVE_PERTURB_AFTER_EM:
active_perturb_after_em = 1;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static const struct argp active_argp =
{
active_options,
active_parse_opt
};
static struct argp_child active_argp_child =
{
&active_argp, /* This child's argp structure */
0, /* flags for child */
0, /* optional header in help message */
0 /* arbitrary group number for ordering */
};
/* End of command-line options specific to EM */
/* Given a fully-specified file path name (all the way from `/'),
return just the last filename part of it. */
static inline const char *
filename_to_classname (const char *filename)
{
const char *ret;
ret = strrchr (filename, '/');
if (ret)
return ret + 1;
return filename;
}
/* cheat and look at the unlabeled data and convert the scores into
true probabilities based on a window size. BUG: we're not resorting
the weights as we should be. */
void
active_remap_scores (bow_barrel *doc_barrel, active_scores *scores,
int total_unknown, int committee_size)
{
int num_classes = bow_barrel_num_classes(doc_barrel);
bow_em_pr_struct *pr_by_class[num_classes];
int member;
int ci;
int scorei;
int hi;
/* malloc some space for pr stats */
for (ci = 0; ci < num_classes; ci++)
pr_by_class[ci] = bow_malloc(sizeof(bow_em_pr_struct) * total_unknown);
for (member = 0; member < committee_size; member++)
{
/* arrange this members scores by class, and note correctness */
for (scorei = 0; scorei < total_unknown; scorei++)
{
bow_cdoc *cdoc = bow_array_entry_at_index (doc_barrel->cdocs,
scores[scorei].di);
for (hi = 0; hi < num_classes; hi++)
{
pr_by_class[scores[scorei].scores[member][hi].di][scorei].score =
scores[scorei].scores[member][hi].weight;
pr_by_class[scores[scorei].scores[member][hi].di][scorei].correct =
(cdoc->class == scores[scorei].scores[member][hi].di
? 1 : 0);
}
}
/* sort the scores for each class by descending score */
for (ci = 0; ci < num_classes; ci ++)
qsort(pr_by_class[ci], total_unknown, sizeof (bow_em_pr_struct),
bow_em_pr_struct_compare);
/* print out a summary of the stats */
if (active_pr_print_stat_summary)
{
for (ci = 0; ci < num_classes; ci++)
{
int pr_index;
int correct=0;
int count=0;
bow_verbosify(bow_progress, "%25s",
filename_to_classname
(bow_barrel_classname_at_index (doc_barrel, ci)));
for (pr_index = 0; pr_index < total_unknown; pr_index++)
{
if (pr_index % active_pr_window_size == 0)
{
if (pr_index != 0)
{
while (pr_index < total_unknown &&
pr_by_class[ci][pr_index-1].score ==
pr_by_class[ci][pr_index].score)
{
correct += pr_by_class[ci][pr_index].correct;
count++;
pr_index++;
}
bow_verbosify(bow_progress, " %3.0f (%1.3f)",
(float) correct * 100.0 / count,
pr_by_class[ci][pr_index].score);
if (!(pr_index < total_unknown))
break;
}
correct = 0;
count = 0;
}
correct += pr_by_class[ci][pr_index].correct;
count++;
if (pr_by_class[ci][pr_index].correct != 0 &&
pr_by_class[ci][pr_index].correct != 1)
bow_error("Big Problem");
}
bow_verbosify(bow_progress, "\n");
}
}
/* remap the scores to better probabilities */
for (scorei = 0; scorei < total_unknown; scorei++)
{
double prob_by_ci[100];
double total = 0.0;
assert(num_classes < 100);
/* set the class_probs by picking numbers from the pr
charts */
for (hi = 0; hi < num_classes; hi++)
{
double score = scores[scorei].scores[member][hi].weight;
int class = scores[scorei].scores[member][hi].di;
int pr_index_low;
int pr_index_high;
int pr_index = 0;
int correct_count = 0;
int num_docs_in_window = 0;
int pri;
while ((pr_index < total_unknown) &&
(pr_by_class[class][pr_index].score > score))
pr_index++;
pr_index_low = pr_index;
while ((pr_index < total_unknown) &&
pr_by_class[class][pr_index].score == score)
pr_index++;
pr_index_high = pr_index;
#if 0
if (10 > pr_index)
correct_count += 10 - pr_index;
#endif
/* note that we're including the test document here
in the stats... */
for (pri = MAX (0, MIN(pr_index_low,
((pr_index_low + pr_index_high -
active_pr_window_size) / 2)));
pri < MIN (MAX(pr_index_high,
((pr_index_high + pr_index_low +
active_pr_window_size) / 2)),
total_unknown);
pri++)
{
correct_count += pr_by_class[class][pri].correct;
num_docs_in_window++;
}
prob_by_ci[class] = (double) correct_count /
((double) num_docs_in_window);
}
/* normalize the probs to sum to one */
for (ci = 0; ci < num_classes; ci++)
total += prob_by_ci[ci];
for (hi = 0; hi < num_classes; hi++)
scores[scorei].scores[member][hi].weight =
prob_by_ci[scores[scorei].scores[member][hi].di] / total;
}
}
}
/* Return the entropy of the words in the document WV. */
float
active_document_entropy (bow_wv *wv)
{
float ret = 0;
float wv_word_count = 0;
int wvi;
float pr_w;
for (wvi = 0; wvi < wv->num_entries; wvi++)
wv_word_count += wv->entry[wvi].count;
for (wvi = 0; wvi < wv->num_entries; wvi++)
{
pr_w = wv->entry[wvi].count / wv_word_count;
ret -= pr_w * log (pr_w);
}
return ret;
}
/* select method routines */
/* comparison function for sorting on selection criteria */
int
active_scores_compare (const void *x, const void *y)
{
if (((active_scores *)x)->weight > ((active_scores *)y)->weight)
return -1;
else if (((active_scores *)x)->weight == ((active_scores *)y)->weight)
return 0;
else
return 1;
}
/* select docs with the highest kl-divergence to the mean */
void
active_select_qbc (bow_barrel *doc_barrel, active_scores *scores,
int num_to_add, int total_unknown,
int committee_size)
{
int num_classes = bow_barrel_num_classes (doc_barrel);
double *mean_class_dist;
double mean_class_sum;
int committee;
int class;
int k;
mean_class_dist = alloca (sizeof (double) * num_classes);
/* Calculate the entropy of the class labels, H(Class|d,Committee),
where Class and Committee are random varibles, and put this in
SCORES->WEIGHT. */
for (k = 0; k < total_unknown; k++)
{
scores[k].weight = 0;
/* Initialize the mean class distribution for this document. */
for (class = 0; class < num_classes; class++)
mean_class_dist[class] = 0;
for (committee = 0; committee < committee_size; committee++)
for (class = 0; class < num_classes; class++)
mean_class_dist[scores[k].scores[committee][class].di]
+= scores[k].scores[committee][class].weight;
mean_class_sum = 0;
for (class = 0; class < num_classes; class++)
mean_class_sum += mean_class_dist[class];
assert (mean_class_sum > committee_size * 0.999);
assert (mean_class_sum < committee_size * 1.001);
for (class = 0; class < num_classes; class++)
mean_class_dist[class] /= mean_class_sum;
/* Set WEIGHT to KL-divergence-to-the-mean averaged over all
committee members. */
for (committee = 0; committee < committee_size; committee++)
{
for (class = 0; class < bow_barrel_num_classes (doc_barrel); class++)
{
if (1e-100 < scores[k].scores[committee][class].weight)
{
scores[k].weight -=
((1.0 / committee_size)
* scores[k].scores[committee][class].weight
* log (mean_class_dist[scores[k].scores[committee][class].di]
/ scores[k].scores[committee][class].weight));
if (scores[k].weight < -0.1)
bow_error("scores[k].weight < -0.1: %.20f, %.20f", scores[k].weight,
log (mean_class_dist[scores[k].scores[committee][class].di]
/ scores[k].scores[committee][class].weight));
}
}
}
/* KL divergence must be greater than or equal to 0 */
if (scores[k].weight < -0.1)
bow_error("scores[k].weight < -0.1: %.20f", scores[k].weight);
}
/* reverse all weights if want lowest ones */
if (active_qbc_low_kl)
{
for (k = 0; k < total_unknown ; k++)
{
scores[k].weight = -1 * scores[k].weight;
}
}
/* Sort based on weight */
qsort (scores, total_unknown, sizeof (active_scores),
active_scores_compare);
/* Change doc types of those with highest entropy*/
for (k = 0; k < num_to_add; k++)
{
bow_cdoc *doc;
doc = bow_cdocs_di2doc (doc_barrel->cdocs, scores[k].di);
assert (doc);
assert (doc->type == bow_doc_unlabeled);
bow_verbosify (bow_progress, "Labeling %s, weight %f", doc->filename,
scores[k].weight);
for (committee=0; committee < committee_size; committee++)
bow_verbosify(bow_progress, " [(%d, %f) (%d, %f)]",
scores[k].scores[committee][0].di,
scores[k].scores[committee][0].weight,
scores[k].scores[committee][1].di,
scores[k].scores[committee][1].weight);
bow_verbosify(bow_progress, "\n");
doc->type = bow_doc_train;
}
return;
}
/* select docs with the highest weighted kl-divergence to the mean */
void
active_select_weighted_kl (bow_barrel *doc_barrel, active_scores *scores,
int num_to_add, int total_unknown,
int committee_size)
{
int num_classes = bow_barrel_num_classes (doc_barrel);
double mean_class_dist[num_classes];
double mean_class_sum;
double **nb_scores;
int committee;
int class;
int k;
bow_cdoc *cdoc;
double nb_scores_sum;
double nb_scores_max;
int si; /* an index into the sorted list of scores */
assert (num_to_add < total_unknown);
assert (em_cross_entropy == 1);
/* Allocate space to store Naive Bayes scores. */
nb_scores = alloca (sizeof (double*) * committee_size);
for (committee = 0; committee < committee_size; committee++)
nb_scores[committee] = alloca (sizeof(double) * num_classes);
/* Calculate the weighted KL divergence of the class labels
and put this in SCORES->WEIGHT. */
for (k = 0; k < total_unknown; k++)
{
/* Fill in the Naive Bayes scores array for this K'th document. */
cdoc = bow_array_entry_at_index (doc_barrel->cdocs, scores[k].di);
for (committee = 0; committee < committee_size; committee++)
{
/* Undo the document length normalization */
for (si = 0; si < num_classes; si++)
nb_scores[committee][scores[k].scores[committee][si].di] =
(scores[k].scores[committee][si].weight
* (cdoc->word_count + 1));
/* Rescale the scores */
nb_scores_max = -DBL_MAX;
for (class = 0; class < num_classes; class++)
if (nb_scores_max < nb_scores[committee][class])
nb_scores_max = nb_scores[committee][class];
for (class = 0; class < num_classes; class++)
nb_scores[committee][class] -= nb_scores_max;
/* Take the exponent of the scores to make them probabilities. */
for (class = 0; class < num_classes; class++)
nb_scores[committee][class] = exp (nb_scores[committee][class]);
/* Normalize them so they sum to one. */
nb_scores_sum = 0;
for (class = 0; class < num_classes; class++)
nb_scores_sum += nb_scores[committee][class];
assert (nb_scores_sum > 0);
for (class = 0; class < num_classes; class++)
nb_scores[committee][class] /= nb_scores_sum;
}
/* Initialize the mean class distribution for this document. */
for (class = 0; class < num_classes; class++)
mean_class_dist[class] = 0;
for (committee = 0; committee < committee_size; committee++)
for (class = 0; class < num_classes; class++)
mean_class_dist[class] += nb_scores[committee][class];
mean_class_sum = 0;
for (class = 0; class < num_classes; class++)
mean_class_sum += mean_class_dist[class];
assert (mean_class_sum > committee_size * 0.999);
assert (mean_class_sum < committee_size * 1.001);
for (class = 0; class < num_classes; class++)
mean_class_dist[class] /= mean_class_sum;
/* Set WEIGHT to KL-divergence-to-the-mean averaged over all
committee members. */
scores[k].weight = 0;
for (committee = 0; committee < committee_size; committee++)
{
for (si = 0; si < bow_barrel_num_classes (doc_barrel); si++)
{
class = scores[k].scores[committee][si].di;
if (1e-100 < nb_scores[committee][class])
{
/* xxx Change this back to regular old WKL! */
#define UNSUPERVISED_DENSITY 1
#if UNSUPERVISED_DENSITY
scores[k].weight -=
((1.0 / committee_size)
/* scale by kl-div of this document to this class */
* nb_scores[committee][class]
* log (mean_class_dist[class]
/ nb_scores[committee][class]));
#elif 1
/* Used for ICML submission */
scores[k].weight -=
((1.0 / committee_size)
/* scale by kl-div of this document to this class */
* exp (scores[k].scores[committee][si].weight
+ cdoc->normalizer)
* nb_scores[committee][class]
* log (mean_class_dist[class]
/ nb_scores[committee][class]));
#else
scores[k].weight -=
((1.0 / committee_size)
* (cdoc->word_count + 1)
/* scale by perplexity of this document in this class */
* exp (scores[k].scores[committee][si].weight)
* nb_scores[committee][class]
* log (mean_class_dist[class]
/ nb_scores[committee][class]));
#endif
}
}
}
#if UNSUPERVISED_DENSITY
/* Scale the score by the document density. */
scores[k].weight *= cdoc->prior;
#endif
/* KL divergence must be greater than or equal to 0 */
if (scores[k].weight < -0.1)
bow_error("scores[k].weight < -0.1: %.20f", scores[k].weight);
}
/* Sort based on weight */
qsort (scores, total_unknown, sizeof (active_scores),
active_scores_compare);
/* Change doc types of those with highest entropy*/
for (k = 0; k < num_to_add ; k++)
{
bow_cdoc *doc;
doc = bow_cdocs_di2doc (doc_barrel->cdocs, scores[k].di);
assert (doc);
assert (doc->type == bow_doc_unlabeled);
bow_verbosify (bow_progress, "Labeling %s, weight %f", doc->filename,
scores[k].weight);
for (committee=0; committee < committee_size; committee++)
bow_verbosify(bow_progress, " [(%d, %f) (%d, %f)]",
scores[k].scores[committee][0].di,
scores[k].scores[committee][0].weight,
scores[k].scores[committee][1].di,
scores[k].scores[committee][1].weight);
bow_verbosify(bow_progress, "\n");
doc->type = bow_doc_train;
}
return;
}
/* select docs with the highest weighted kl-divergence to the mean.
Needs crossentropy scores! */
void
active_select_dkl (bow_barrel *doc_barrel, active_scores *scores,
int num_to_add, int total_unknown,
int committee_size)
{
int num_classes = bow_barrel_num_classes (doc_barrel);
double mean_class_dist[num_classes];
double mean_class_sum;
double **nb_scores;
int committee;
int class;
int k;
bow_cdoc *cdoc;
double nb_scores_sum;
double nb_scores_max;
int si; /* an index into the sorted list of scores */
assert (num_to_add < total_unknown);
assert (em_cross_entropy == 1);
/* Allocate space to store Naive Bayes scores. */
nb_scores = alloca (sizeof (double*) * committee_size);
for (committee = 0; committee < committee_size; committee++)
nb_scores[committee] = alloca (sizeof(double) * num_classes);
/* Calculate the weighted KL divergence of the class labels
and put this in SCORES->WEIGHT. */
for (k = 0; k < total_unknown; k++)
{
/* Fill in the Naive Bayes scores array for this K'th document. */
cdoc = bow_array_entry_at_index (doc_barrel->cdocs, scores[k].di);
for (committee = 0; committee < committee_size; committee++)
{
/* Undo the document length normalization */
for (si = 0; si < num_classes; si++)
nb_scores[committee][scores[k].scores[committee][si].di] =
(scores[k].scores[committee][si].weight
* (cdoc->word_count + 1));
/* Rescale the scores */
nb_scores_max = -DBL_MAX;
for (class = 0; class < num_classes; class++)
if (nb_scores_max < nb_scores[committee][class])
nb_scores_max = nb_scores[committee][class];
for (class = 0; class < num_classes; class++)
nb_scores[committee][class] -= nb_scores_max;
/* Take the exponent of the scores to make them probabilities. */
for (class = 0; class < num_classes; class++)
nb_scores[committee][class] = exp (nb_scores[committee][class]);
/* Normalize them so they sum to one. */
nb_scores_sum = 0;
for (class = 0; class < num_classes; class++)
nb_scores_sum += nb_scores[committee][class];
assert (nb_scores_sum > 0);
for (class = 0; class < num_classes; class++)
nb_scores[committee][class] /= nb_scores_sum;
}
/* Initialize the mean class distribution for this document. */
for (class = 0; class < num_classes; class++)
mean_class_dist[class] = 0;
for (committee = 0; committee < committee_size; committee++)
for (class = 0; class < num_classes; class++)
mean_class_dist[class] += nb_scores[committee][class];
mean_class_sum = 0;
for (class = 0; class < num_classes; class++)
mean_class_sum += mean_class_dist[class];
assert (mean_class_sum > committee_size * 0.999);
assert (mean_class_sum < committee_size * 1.001);
for (class = 0; class < num_classes; class++)
mean_class_dist[class] /= mean_class_sum;
/* Set WEIGHT to KL-divergence-to-the-mean averaged over all
committee members. */
scores[k].weight = 0;
for (committee = 0; committee < committee_size; committee++)
{
for (si = 0; si < bow_barrel_num_classes (doc_barrel); si++)
{
class = scores[k].scores[committee][si].di;
if (1e-100 < nb_scores[committee][class])
{
scores[k].weight -=
((1.0 / committee_size)
/* scale by kl-div of this document to this class */
* nb_scores[committee][class]
* log (mean_class_dist[class]
/ nb_scores[committee][class]));
}
}
}
/* Scale the score by the document density. */
scores[k].weight *= cdoc->prior;
/* KL divergence must be greater than or equal to 0 */
if (scores[k].weight < -0.1)
bow_error("scores[k].weight < -0.1: %.20f", scores[k].weight);
}
/* Sort based on weight */
qsort (scores, total_unknown, sizeof (active_scores),
active_scores_compare);
/* Change doc types of those with highest entropy*/
for (k = 0; k < num_to_add ; k++)
{
bow_cdoc *doc;
doc = bow_cdocs_di2doc (doc_barrel->cdocs, scores[k].di);
assert (doc);
assert (doc->type == bow_doc_unlabeled);
bow_verbosify (bow_progress, "Labeling %s, weight %f", doc->filename,
scores[k].weight);
for (committee=0; committee < committee_size; committee++)
bow_verbosify(bow_progress, " [(%d, %f) (%d, %f)]",
scores[k].scores[committee][0].di,
scores[k].scores[committee][0].weight,
scores[k].scores[committee][1].di,
scores[k].scores[committee][1].weight);
bow_verbosify(bow_progress, "\n");
doc->type = bow_doc_train;
}
return;
}
/* select docs with the highest vote entropy (Dagan and Engelson) */
void
active_select_vote_entropy (bow_barrel *doc_barrel, active_scores *scores,
int num_to_add, int total_unknown, int committee_size)
{
int num_classes = bow_barrel_num_classes (doc_barrel);
double *mean_class_dist;
double mean_class_sum;
int committee;
int class;
int k;
int si;
mean_class_dist = alloca (sizeof (double) * num_classes);
/* Calculate the entropy of the class labels, H(Class|d,Committee),
where Class and Committee are random varibles, and put this in
SCORES->WEIGHT. */
for (k = 0; k < total_unknown; k++)
{
scores[k].weight = 0;
/* Initialize the scores to be 'votes' */
for (committee = 0; committee < committee_size; committee++)
{
scores[k].scores[committee][0].weight = 1.0;
for (si = 1; si < num_classes; si++)
scores[k].scores[committee][si].weight = 0.0;
}
/* Initialize the mean class distribution for this document. */
for (class = 0; class < num_classes; class++)
mean_class_dist[class] = 0;
for (committee = 0; committee < committee_size; committee++)
for (class = 0; class < num_classes; class++)
mean_class_dist[scores[k].scores[committee][class].di]
+= scores[k].scores[committee][class].weight;
mean_class_sum = 0;
for (class = 0; class < num_classes; class++)
mean_class_sum += mean_class_dist[class];
assert (mean_class_sum > committee_size * 0.999);
assert (mean_class_sum < committee_size * 1.001);
for (class = 0; class < num_classes; class++)
mean_class_dist[class] /= mean_class_sum;
/* Calculate the entropy of the mean class distribution */
for (class = 0; class < bow_barrel_num_classes (doc_barrel); class++)
{
if (1e-100 < mean_class_dist[class])
{
scores[k].weight -=
(mean_class_dist[class]
* log (mean_class_dist[class]));
}
}
/* Entropy must be greater than or equal to 0 */
if (scores[k].weight < -0.1)
bow_error("scores[k].weight < -0.1: %.20f", scores[k].weight);
}
/* Sort based on weight */
qsort (scores, total_unknown, sizeof (active_scores),
active_scores_compare);
/* Change doc types of those with highest entropy*/
for (k = 0; k < num_to_add; )
{
int z;
double top_score;
int n;
int j;
/* find how many top ranked docs have same score */
top_score = scores[k].weight;
for (z=k; z < total_unknown && scores[z].weight == top_score ; z++);
/* add all with top score if won't max it out */
if (z < num_to_add)
{
for (n=k; n<z; n++, k++)
{
bow_cdoc *doc;
doc = bow_cdocs_di2doc (doc_barrel->cdocs, scores[k].di);
assert (doc);
assert (doc->type == bow_doc_unlabeled);
bow_verbosify (bow_progress, "Labeling %s, weight %f", doc->filename,
scores[n].weight);
for (committee=0; committee < committee_size; committee++)
bow_verbosify(bow_progress, " %d",
scores[n].scores[committee][0].di);
bow_verbosify(bow_progress, "\n");
doc->type = bow_doc_train;
}
}
else
{
/* need to randomly select some of the docs for labeling */
for (j=0, n=k; n < num_to_add; j++)
{
int si = (rand() % (z-k)) + k;
int doci;
bow_cdoc *doc;
doci = scores[si].di;
doc = bow_cdocs_di2doc (doc_barrel->cdocs, doci);
assert (doc);