forked from fizx/libbow-osx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hem.c
1561 lines (1403 loc) · 47.3 KB
/
hem.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
/* hem.c - Hierarchical Expectation Maximization
Copyright (C) 1998, 1999 Andrew McCallum
Written by: Andrew Kachites McCallum <[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 <argp.h>
#include <bow/crossbow.h>
extern void crossbow_leaf_document_probs_print (int num_to_print);
extern void crossbow_classify_tagged_docs (int tag, int verbose,
FILE *out);
#define SHRINK_WITH_UNIFORM_ONLY 0
#define PRINT_WORD_DISTS 0
#define MN 0
#if MN
extern double crossbow_hem_em_one_mn_iteration ();
#endif
static int crossbow_hem_branching_factor = 2;
static double crossbow_hem_temperature = 100;
static double crossbow_hem_temperature_end = 1;
static int crossbow_hem_max_num_iterations = 9999999;
static double crossbow_hem_temperature_decay = 0.9;
static double crossbow_hem_em_acceleration = 1.0;
static double crossbow_hem_split_kl_threshold = 0.4;
static int crossbow_hem_maximum_depth = 6;
static double crossbow_hem_lambdas_from_validation = 0.0;
/* Doing statistical garbage collection? */
static int crossbow_hem_garbage_collection = 0;
/* Doing incremental labeling, ala co-training? */
static int crossbow_hem_incremental_labeling = 0;
/* Are the documents already labeled to belong to one leaf? */
int crossbow_hem_deterministic_horizontal = 0;
int crossbow_hem_restricted_horizontal = 0;
/* Doing "full-EM"?, meaning that vertical word distributions are
changed by EM. Note that speech recognitions's traditional
"deleted interpolation" only uses EM to set the lambdas. */
int crossbow_hem_vertical_word_movement = 1;
/* Doing shrinkage */
int crossbow_hem_shrinkage = 1;
/* Using shrinkage, but with fixed weights. Don't learn them by EM.
Only active is crossbow_hem_shrinkage = 1 */
int crossbow_hem_fixed_shrinkage = 0;
/* Doing Leave-One-Out */
int crossbow_hem_loo = 1;
/* The class tag is part of the generative model, and should be used
in the E-step to estimate class membership, and the M-step should
update the class distribution in each leaf. */
int crossbow_hem_generates_class = 1;
/* If non-zero, then after the initial E-step, change all labeled
documents to unlabeled. */
int crossbow_hem_pseudo_labeled = 0;
/* Command-line setting routines */
enum {
BRANCHING_FACTOR_KEY = 17000,
TEMPERATURE_START_KEY,
TEMPERATURE_END_KEY,
TEMPERATURE_DECAY_KEY,
EM_ACCELERATION_KEY,
SPLIT_KL_THRESHOLD_KEY,
MAXIMUM_DEPTH_KEY,
NO_VERTICAL_WORD_MOVEMENT_KEY,
NO_SHRINKAGE_KEY,
NO_LOO_KEY,
DETERMINISTIC_HORIZONTAL_KEY,
RESTRICTED_HORIZONTAL_KEY,
PSEUDO_LABELED_KEY,
GARBAGE_COLLECTION_KEY,
MAX_NUM_ITERATIONS_KEY,
LAMBDAS_FROM_VALIDATION_KEY,
INCREMENTAL_LABELING_KEY,
};
static struct argp_option crossbow_hem_options[] =
{
{0, 0, 0, 0,
"Hierarchical EM Clustering options:", 101},
{"hem-branching-factor", BRANCHING_FACTOR_KEY, "NUM", 0,
"Number of clusters to create. Default is 2."},
{"hem-temperature-start", TEMPERATURE_START_KEY, "NUM", 0,
"The initial value of T."},
{"hem-temperature-end", TEMPERATURE_END_KEY, "NUM", 0,
"The final value of T. Default is 1."},
{"hem-max-num-iterations", MAX_NUM_ITERATIONS_KEY, "NUM", 0,
"Do no more iterations of EM than this."},
{"hem-temperature-decay", TEMPERATURE_DECAY_KEY, "NUM", 0,
"Temperature decay factor. Default is 0.9."},
{"hem-em-acceleration", EM_ACCELERATION_KEY, "NUM", OPTION_HIDDEN,
"Accelerated EM \eta factor. 1 is plain EM. Can safely go "
"as high as 2.0. 1.8 is a good value. Default is 1."},
{"hem-split-kl-threshold", SPLIT_KL_THRESHOLD_KEY, "NUM", 0,
"KL divergence value at which tree leaves will be split. "
"Default is 0.2"},
{"hem-maximum-depth", MAXIMUM_DEPTH_KEY, "NUM", 0,
"The hierarchy depth beyond which it will not split. Default is 6."},
{"hem-no-vertical-word-movement", NO_VERTICAL_WORD_MOVEMENT_KEY, 0, 0,
"Use EM just to set the vertical priors, not to set the vertical "
"word distribution; i.e. do not to `full-EM'."},
{"hem-no-shrinkage", NO_SHRINKAGE_KEY, 0, 0,
"Use only the clusters at the leaves; do not do anything with the "
"hierarchy."},
{"hem-no-loo", NO_LOO_KEY, 0, 0,
"Do not use leave-one-out evaluation during the E-step."},
{"hem-deterministic-horizontal", DETERMINISTIC_HORIZONTAL_KEY, 0, 0,
"In the horizontal E-step for a document, set to zero the membership "
"probabilities of all leaves, except the one matching the document's "
"filename"},
{"hem-restricted-horizontal", RESTRICTED_HORIZONTAL_KEY, 0, 0,
"In the horizontal E-step for a document, set to zero the membership "
"probabilities of all leaves whose names are not found in the document's "
"filename"},
{"hem-pseudo-labeled", PSEUDO_LABELED_KEY, 0, 0,
"After using the labels to set the starting point for EM, change all "
"training documents to unlabeled, so that they can have their class "
"labels re-assigned by EM. Useful for imperfectly labeled training data."},
{"hem-garbage-collection", GARBAGE_COLLECTION_KEY, 0, 0,
"Add extra /Misc/ children to every internal node of the hierarchy, "
"and keep their local word distributions flat"},
{"hem-lambdas-from-validation", LAMBDAS_FROM_VALIDATION_KEY, "NUM", 0,
"Instead of setting the lambdas from the labeled/unlabeled data "
"(possibly with LOO), instead set the lambdas using held-out "
"validation data. 0<NUM<1 is the fraction of unlabeled documents "
"just before EM training of the classifier begins. Default is 0, "
"which leaves this option off."},
{"hem-incremental-labeling", INCREMENTAL_LABELING_KEY, 0, 0,
"Instead of using all unlabeled documents in the M-step, use only "
"the labeled documents, and incrementally label those unlabeled documents "
"that are most confidently classified in the E-step"},
{0, 0}
};
error_t
crossbow_hem_parse_opt (int key, char *arg, struct argp_state *state)
{
switch (key)
{
case BRANCHING_FACTOR_KEY:
crossbow_hem_branching_factor = atoi (arg);
break;
case TEMPERATURE_START_KEY:
crossbow_hem_temperature = atof (arg);
break;
case TEMPERATURE_END_KEY:
crossbow_hem_temperature_end = atof (arg);
break;
case TEMPERATURE_DECAY_KEY:
crossbow_hem_temperature_decay = atof (arg);
break;
case EM_ACCELERATION_KEY:
crossbow_hem_em_acceleration = atof (arg);
break;
case SPLIT_KL_THRESHOLD_KEY:
crossbow_hem_split_kl_threshold = atof (arg);
break;
case MAXIMUM_DEPTH_KEY:
crossbow_hem_maximum_depth = atoi (arg);
break;
case NO_VERTICAL_WORD_MOVEMENT_KEY:
crossbow_hem_vertical_word_movement = 0;
break;
case NO_SHRINKAGE_KEY:
crossbow_hem_shrinkage = 0;
break;
case NO_LOO_KEY:
crossbow_hem_loo = 0;
break;
case RESTRICTED_HORIZONTAL_KEY:
crossbow_hem_restricted_horizontal = 1;
break;
case DETERMINISTIC_HORIZONTAL_KEY:
crossbow_hem_deterministic_horizontal = 1;
break;
case PSEUDO_LABELED_KEY:
crossbow_hem_pseudo_labeled = 1;
break;
case GARBAGE_COLLECTION_KEY:
crossbow_hem_garbage_collection = 1;
break;
case MAX_NUM_ITERATIONS_KEY:
crossbow_hem_max_num_iterations = atoi (arg);
break;
case LAMBDAS_FROM_VALIDATION_KEY:
crossbow_hem_lambdas_from_validation = atof (arg);
break;
case INCREMENTAL_LABELING_KEY:
crossbow_hem_incremental_labeling = 1;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static const struct argp crossbow_hem_argp =
{
crossbow_hem_options,
crossbow_hem_parse_opt
};
static struct argp_child crossbow_hem_argp_child =
{
&crossbow_hem_argp, /* This child's argp structure */
0, /* flags for child */
0, /* optional header in help message */
0 /* arbitrary group number for ordering */
};
/* create num_children children for the leaf node tn */
void
crossbow_hem_create_children_for_node (treenode *tn, int num_children)
{
int ci;
treenode *child;
int ai;
int wi;
assert (tn->children_count == 0);
for (ci = 0; ci < num_children; ci++)
{
child = bow_treenode_new (tn, num_children, NULL);
if (!crossbow_hem_shrinkage)
{
/* if no shrinkage, set the lamdas all at the leaf */
child->new_lambdas[0] = 1.0;
for (ai = 1; ai < child->depth + 2; ai++)
child->new_lambdas[ai] = 0.0;
bow_treenode_set_lambdas_from_new_lambdas (child, 0);
}
else
{
/* set the children close to parent, sharing their lambdas */
child->new_lambdas[0] = tn->lambdas[0]/2;
child->new_lambdas[1] = tn->lambdas[0]/2;
for (ai = 2; ai < child->depth + 2; ai++)
child->new_lambdas[ai] =
tn->lambdas[ai-1];
bow_treenode_set_lambdas_from_new_lambdas (child, 0);
}
/* make each word distribution like parent's, but perturbed */
for (wi = 0; wi < tn->words_capacity; wi++)
child->words[wi] = tn->words[wi];
/* xxx But we're going to perturb them again in hem_cluster!!! */
bow_treenode_set_new_words_from_perturbed_words (child, 0.1);
/* split the prior of the parent amongst the children */
child->prior = tn->prior / num_children;
bow_treenode_set_words_from_new_words (child, 0);
}
/* zero out the prior of the parent now that it's not a leaf */
tn->prior = 0.0;
}
/* Return non-zero if a split happens */
int
crossbow_hem_hypothesize_grandchildren (treenode *tn, int num_children)
{
int ci;
double kldiv;
/* The number of words of training data in the children of TN */
assert (bow_treenode_is_leaf_parent (tn));
kldiv = bow_treenode_children_kl_div (tn);
if (kldiv > crossbow_hem_split_kl_threshold
&& tn->depth < crossbow_hem_maximum_depth)
{
printf ("Splitting children of node %s\n", tn->name);
/* Create and attach grandchildren, and copy perturbed word
distribution. */
for (ci = 0; ci < tn->children_count; ci++)
{
crossbow_hem_create_children_for_node (tn->children[ci],
num_children);
}
return 1;
}
return 0;
}
/* Return the perplexity of the data in documents for which the
function USE_DOC_P returns non-zero. */
double
crossbow_hem_perplexity (int (*use_doc_p)(bow_doc*))
{
int di;
crossbow_doc *doc;
bow_wv *wv;
treenode *iterator, *leaf;
int li; /* a leaf index */
int num_leaves;
double *leaf_membership;
double *leaf_data_prob;
double log_prob_of_data = 0;
int num_data_words = 0; /* the number of word occurrences */
num_leaves = bow_treenode_leaf_count (crossbow_root);
leaf_membership = alloca (num_leaves * sizeof (double));
leaf_data_prob = alloca (num_leaves * sizeof (double));
for (di = 0; di < crossbow_docs->length; di++)
{
doc = bow_array_entry_at_index (crossbow_docs, di);
if (! (*use_doc_p)((bow_doc*)doc))
continue;
/* E-step estimating leaf membership probability for one
document, with annealing temperature. */
wv = crossbow_wv_at_di (di);
for (iterator = crossbow_root, li = 0;
(leaf = bow_treenode_iterate_leaves (&iterator));
li++)
{
if (crossbow_hem_shrinkage)
leaf_data_prob[li] = bow_treenode_log_prob_of_wv (leaf, wv);
else
leaf_data_prob[li] = bow_treenode_log_local_prob_of_wv (leaf, wv);
leaf_membership[li] = (log (leaf->prior)
+ (leaf_data_prob[li]
/ crossbow_hem_temperature));
}
crossbow_convert_log_probs_to_probs (leaf_membership, num_leaves);
/* For perplexity calculation */
for (iterator = crossbow_root, li = 0;
(leaf = bow_treenode_iterate_leaves (&iterator));
li++)
{
/* xxx Should this be with bow_treenode_complete_log_prob_of_wv()? */
log_prob_of_data += (leaf_membership[li] * leaf_data_prob[li]);
assert (log_prob_of_data == log_prob_of_data);
}
num_data_words += bow_wv_word_count (wv);
}
/* Return the perlexity */
if (num_data_words)
return exp (-log_prob_of_data / num_data_words);
return 0;
}
/* Return the perplexity of the data (perplexity (without knowledge of
the class label, P(D|theta)) in documents for which the function
USE_DOC_P returns non-zero. */
double
crossbow_hem_unlabeled_perplexity (int (*use_doc_p)(bow_doc*))
{
int di;
crossbow_doc *doc;
bow_wv *wv;
treenode *iterator, *leaf;
int li; /* a leaf index */
int num_leaves;
double leaf_data_log_prob;
double leaf_pp;
double max_leaf_pp;
double log_prob_of_data = 0;
int num_data_words = 0; /* the number of word occurrences */
num_leaves = bow_treenode_leaf_count (crossbow_root);
for (di = 0; di < crossbow_docs->length; di++)
{
doc = bow_array_entry_at_index (crossbow_docs, di);
if (! (*use_doc_p)((bow_doc*)doc))
continue;
wv = crossbow_wv_at_di (di);
max_leaf_pp = -FLT_MAX;
for (iterator = crossbow_root, li = 0;
(leaf = bow_treenode_iterate_leaves (&iterator));
li++)
{
if (crossbow_hem_shrinkage)
leaf_data_log_prob = bow_treenode_log_prob_of_wv (leaf, wv);
else
leaf_data_log_prob = bow_treenode_log_local_prob_of_wv (leaf, wv);
leaf_pp = log(leaf->prior) + leaf_data_log_prob;
assert (leaf_pp == leaf_pp);
#if 1
/* Test for -Inf, and if so, immediately return Inf */
if (leaf_pp == -HUGE_VAL)
return HUGE_VAL;
#endif
if (leaf_pp > max_leaf_pp)
max_leaf_pp = leaf_pp;
}
assert (max_leaf_pp != -FLT_MAX);
log_prob_of_data += max_leaf_pp;
num_data_words += bow_wv_word_count (wv);
}
/* Return the perlexity */
if (num_data_words)
return exp (-log_prob_of_data / num_data_words);
return 0;
}
/* Return the perplexity (given knowledge of the class label,
P(D,C|theta)) of the data in documents for which the function
USE_DOC_P returns non-zero. */
double
crossbow_hem_labeled_perplexity (int (*use_doc_p)(bow_doc*))
{
int di;
crossbow_doc *doc;
bow_wv *wv;
treenode *leaf;
int num_leaves;
double leaf_data_log_prob;
double log_prob_of_data = 0;
int num_data_words = 0; /* the number of word occurrences */
num_leaves = bow_treenode_leaf_count (crossbow_root);
for (di = 0; di < crossbow_docs->length; di++)
{
doc = bow_array_entry_at_index (crossbow_docs, di);
if (! (*use_doc_p)((bow_doc*)doc))
continue;
wv = crossbow_wv_at_di (di);
leaf = bow_treenode_descendant_matching_name (crossbow_root,
doc->filename);
if (crossbow_hem_shrinkage)
leaf_data_log_prob = bow_treenode_log_prob_of_wv (leaf, wv);
else
leaf_data_log_prob = bow_treenode_log_local_prob_of_wv (leaf, wv);
/* Test for -Inf, and if so, immediately return Inf */
if (leaf_data_log_prob == -HUGE_VAL)
return HUGE_VAL;
log_prob_of_data += (log (leaf->prior) + leaf_data_log_prob);
assert (log_prob_of_data == log_prob_of_data);
num_data_words += bow_wv_word_count (wv);
}
/* Return the perlexity */
if (num_data_words)
return exp (-log_prob_of_data / num_data_words);
return 0;
}
/* Classify all unlabeled documents and convert the most confidently
classified to labeled */
void
crossbow_hem_label_most_confident ()
{
int di, li;
crossbow_doc *doc;
// bow_wv *wv;
bow_wa *wa;
int word_count;
double score;
int leaf_count = bow_treenode_leaf_count (crossbow_root);
bow_wa **high_scores_per_class;
static int unlabeled_count = -1;
static int num_to_label = 999;
treenode *iterator, *leaf;
assert (crossbow_hem_incremental_labeling);
/* Calculate num_to_label if we are to label all examples in 20
iterations. */
if (unlabeled_count == -1)
{
unlabeled_count = 0;
for (di = 0; di < crossbow_docs->length; di++)
{
doc = bow_array_entry_at_index (crossbow_docs, di);
if (doc->tag == bow_doc_unlabeled)
unlabeled_count++;
}
num_to_label = unlabeled_count / 20;
}
high_scores_per_class = alloca (leaf_count * sizeof (void*));
for (li = 0; li < leaf_count; li++)
high_scores_per_class[li] = bow_wa_new (0);
for (di = 0; di < crossbow_docs->length; di++)
{
bow_wv *wv;
doc = bow_array_entry_at_index (crossbow_docs, di);
if (doc->tag != bow_doc_unlabeled)
continue;
wv = crossbow_wv_at_di (doc->di);
word_count = bow_wv_word_count (wv);
wv = crossbow_wv_at_di (doc->di);
assert (wv);
wa = crossbow_classify_doc_new_wa (wv);
bow_wa_sort (wa);
score = wa->entry[0].weight;
score /= ((word_count + 1) / MIN(9,word_count));
bow_wa_append (high_scores_per_class[wa->entry[0].wi], di, score);
bow_wa_free (wa);
}
for (iterator = crossbow_root, li = 0;
(leaf = bow_treenode_iterate_leaves (&iterator));
li++)
{
int i, num_to_label_this_class = MAX(1,num_to_label * leaf->prior);
if (high_scores_per_class[li]->length == 0)
continue;
bow_wa_sort (high_scores_per_class[li]);
if (num_to_label_this_class > high_scores_per_class[li]->length)
{
bow_verbosify (bow_quiet,
"Not enough unlabeled documents classified as %s\n",
leaf->name);
num_to_label_this_class = high_scores_per_class[li]->length;
}
for (i = 0; i < num_to_label_this_class; i++)
{
char *newname = bow_malloc (128);
doc =
bow_array_entry_at_index (crossbow_docs,
high_scores_per_class[li]->entry[i].wi);
assert (doc->tag = bow_doc_unlabeled);
doc->tag = bow_doc_train;
doc->ci = li;
/* xxx Yuck! WhizBang-specific */
sprintf (newname, "./data%s%s", leaf->name,
strrchr(doc->filename, '/') + 1);
/* xxx Memory leak here. Free the doc->name first. */
doc->filename = newname;
bow_verbosify (bow_progress, "Labeling class %10s %35s %g\n",
leaf->name, doc->filename,
high_scores_per_class[li]->entry[i].weight);
}
}
for (li = 0; li < leaf_count; li++)
bow_wa_free (high_scores_per_class[li]);
}
#if MN
#include "mn.c"
#endif
/* Return the perplexity */
double
crossbow_hem_em_one_iteration ()
{
int di;
crossbow_doc *doc;
bow_wv *wv;
treenode *iterator, *leaf, *ancestor;
int li; /* a leaf index */
int wvi;
int num_leaves;
double *leaf_membership;
double *leaf_data_prob;
double pp, log_prob_of_data = 0;
int num_data_words = 0; /* the number of word occurrences */
double *ancestor_membership;
double ancestor_membership_total;
double total_deposit_prob;
int found_deterministic_leaf;
int docs_added_count = 0;
#if MN
return crossbow_hem_em_one_mn_iteration ();
#endif
num_leaves = bow_treenode_leaf_count (crossbow_root);
leaf_membership = alloca (num_leaves * sizeof (double));
leaf_data_prob = alloca (num_leaves * sizeof (double));
/* xxx Here NUM_LEAVES+10 should be MAX_DEPTH */
ancestor_membership = alloca ((num_leaves + 10) * sizeof (double));
for (di = 0; di < crossbow_docs->length; di++)
{
total_deposit_prob = 0;
doc = bow_array_entry_at_index (crossbow_docs, di);
if (crossbow_hem_incremental_labeling)
{
if (crossbow_hem_lambdas_from_validation)
{
if (doc->tag != bow_doc_train
&& doc->tag != bow_doc_validation)
continue;
}
else
{
if (doc->tag != bow_doc_train)
continue;
}
}
else if (crossbow_hem_lambdas_from_validation)
{
if (doc->tag != bow_doc_train
&& doc->tag != bow_doc_unlabeled
&& doc->tag != bow_doc_validation)
continue;
}
else
{
if (doc->tag != bow_doc_train && doc->tag != bow_doc_unlabeled)
continue;
}
/* Temporary fix */
if (strstr (doc->filename, ".include")
|| strstr (doc->filename, ".exclude"))
continue;
/* E-step estimating leaf membership probability for one
document, with annealing temperature. */
wv = crossbow_wv_at_di (di);
found_deterministic_leaf = 0;
for (iterator = crossbow_root, li = 0;
(leaf = bow_treenode_iterate_leaves (&iterator));
li++)
{
if (crossbow_hem_shrinkage)
{
if (crossbow_hem_loo)
leaf_data_prob[li] =
bow_treenode_log_prob_of_wv_loo (leaf, wv, di);
else
leaf_data_prob[li] = bow_treenode_log_prob_of_wv (leaf, wv);
}
else
{
if (crossbow_hem_loo)
leaf_data_prob[li] =
bow_treenode_log_local_prob_of_wv_loo (leaf, wv, di);
else
leaf_data_prob[li] =
bow_treenode_log_local_prob_of_wv (leaf, wv);
}
assert (leaf_data_prob[li] > -HUGE_VAL);
if (crossbow_hem_deterministic_horizontal
&& (doc->tag == bow_doc_train
|| doc->tag == bow_doc_validation))
{
if (strstr (doc->filename, leaf->name))
{
assert (!found_deterministic_leaf);
leaf_membership[li] = 1.0;
found_deterministic_leaf = 1;
}
else
/* The validation document was formerly an unlabeled
document. Set the membership to zero for now; we
will set it to the results of the E-step below when
we call crossbow_convert_log_probs_to_probs */
leaf_membership[li] = 0.0;
continue;
}
else if (crossbow_hem_restricted_horizontal
&& (doc->tag == bow_doc_train
|| doc->tag == bow_doc_validation))
{
treenode *label_node =
bow_treenode_descendant_matching_name (crossbow_root,
doc->filename);
if (strstr (leaf->name, label_node->name))
leaf_membership[li] = (log (leaf->prior)
+ (leaf_data_prob[li]
/ crossbow_hem_temperature));
else
/* Set it to probability zero, which, in log space is -Inf */
leaf_membership[li] = -HUGE_VAL;
}
else
{
leaf_membership[li] = (log (leaf->prior)
+ (leaf_data_prob[li]
/ crossbow_hem_temperature));
}
}
if (!crossbow_hem_deterministic_horizontal
|| doc->tag == bow_doc_unlabeled
|| !found_deterministic_leaf)
/* Last condition above for unlabeled docs that were changed
to validation docs */
crossbow_convert_log_probs_to_probs (leaf_membership, num_leaves);
else
/* No longer meaningful!? */
assert (found_deterministic_leaf);
/* For perplexity calculation */
for (iterator = crossbow_root, li = 0;
(leaf = bow_treenode_iterate_leaves (&iterator));
li++)
{
/* xxx Should this be with bow_treenode_complete_log_prob_of_wv()? */
if (leaf_membership[li])
log_prob_of_data += (leaf_membership[li] * leaf_data_prob[li]);
assert (log_prob_of_data == log_prob_of_data);
}
num_data_words += bow_wv_word_count (wv);
docs_added_count++;
/* E-step estimating ancestor membership probability for words
in one document, and M-step for one document */
for (iterator = crossbow_root, li = 0;
(leaf = bow_treenode_iterate_leaves (&iterator));
li++)
{
if (leaf_membership[li] == 0)
continue;
if (strstr (leaf->name, "/Misc/"))
continue;
for (wvi = 0; wvi < wv->num_entries; wvi++)
{
if (crossbow_hem_shrinkage)
{
int ai;
double word_deposit, lambda_deposit;
/* Calculate normalized ancestor membership probs */
ancestor_membership_total = 0;
for (ancestor = leaf, ai = 0; ancestor;
ancestor = ancestor->parent, ai++)
{
if (crossbow_hem_loo)
ancestor_membership[ai] =
leaf->lambdas[ai]
* bow_treenode_pr_wi_loo_local (ancestor,
wv->entry[wvi].wi,
di, wvi);
else
ancestor_membership[ai] = leaf->lambdas[ai] *
ancestor->words[wv->entry[wvi].wi];
assert (ancestor_membership[ai] >= 0);
ancestor_membership_total += ancestor_membership[ai];
}
ancestor_membership[ai] =
leaf->lambdas[ai] * 1.0 / leaf->words_capacity;
ancestor_membership_total += ancestor_membership[ai];
assert (ancestor_membership_total);
for (ai = 0; ai < leaf->depth + 2; ai++)
{
assert (ancestor_membership[ai] >= 0);
ancestor_membership[ai] /= ancestor_membership_total;
}
/* The M-step */
for (ancestor = leaf, ai = 0; ancestor;
ancestor = ancestor->parent, ai++)
{
if (crossbow_hem_vertical_word_movement)
word_deposit = wv->entry[wvi].count
* leaf_membership[li] * ancestor_membership[ai];
else
word_deposit = wv->entry[wvi].count
* leaf_membership[li];
#define UNLABELED_WEIGHT_REDUCED 0
#if UNLABELED_WEIGHT_REDUCED
if (doc->tag == bow_doc_unlabeled)
word_deposit /= 3;
#endif
assert (word_deposit >= 0);
if (!crossbow_hem_lambdas_from_validation
|| doc->tag != bow_doc_validation)
{
if (crossbow_hem_loo)
bow_treenode_add_new_loo_for_di_wvi
(ancestor, word_deposit, di, wvi,
wv->num_entries, crossbow_docs->length);
ancestor->new_words[wv->entry[wvi].wi] +=
word_deposit;
}
if (ancestor_membership[ai] == 0)
continue;
lambda_deposit = wv->entry[wvi].count
* leaf_membership[li] * ancestor_membership[ai];
assert (lambda_deposit >= 0);
if (!crossbow_hem_lambdas_from_validation
|| doc->tag == bow_doc_validation)
leaf->new_lambdas[ai] += lambda_deposit;
}
/* The uniform distribution */
if (!crossbow_hem_lambdas_from_validation
|| doc->tag == bow_doc_validation)
leaf->new_lambdas[ai] +=
wv->entry[wvi].count
* leaf_membership[li] * ancestor_membership[ai];
} /* if crossbow_hem_shrinkage */
else
{
/* The M-step without shrinkage, without ancestor
membership probabilities. */
leaf->new_words[wv->entry[wvi].wi] +=
wv->entry[wvi].count * leaf_membership[li];
leaf->new_lambdas[0]++;
}
assert (leaf->new_words[wv->entry[wvi].wi] >= 0);
assert (leaf->new_words[wv->entry[wvi].wi]
== leaf->new_words[wv->entry[wvi].wi]);
}
leaf->new_prior += leaf_membership[li];
}
}
/* Finish M-step */
bow_treenode_set_leaf_prior_from_new_prior_all (crossbow_root, 1);
for (iterator = crossbow_root;
(leaf = bow_treenode_iterate_all (&iterator));)
{
if (crossbow_hem_shrinkage)
{
bow_treenode_set_words_from_new_words (leaf, 0);
bow_treenode_set_lambdas_from_new_lambdas (leaf, 1);
}
else
{
bow_treenode_set_words_from_new_words (leaf, 1);
bow_treenode_set_lambdas_from_new_lambdas (leaf, 0);
}
}
pp = exp (-log_prob_of_data / num_data_words);
bow_verbosify (bow_progress, "EM incorporated %d documents; pp=%g\n",
docs_added_count, pp);
/* Return the perlexity */
return pp;
}
int
crossbow_hem_consider_splitting ()
{
int grandparents_count;
treenode *tn, *iterator, **grandparents;
int ci;
int num_leaves;
int did_split = 0;
/* Make an array of grandparents, then try splitting them.
If you just iterate through tree, then iteration gets messed
up the creation of new grandchildren. */
num_leaves = bow_treenode_leaf_count (crossbow_root);
grandparents = bow_malloc (num_leaves * sizeof (void*));
grandparents_count = 0;
for (iterator = crossbow_root;
(tn = bow_treenode_iterate_all (&iterator));)
{
if (bow_treenode_is_leaf_parent (tn))
grandparents[grandparents_count++] = tn;
}
for (ci = 0; ci < grandparents_count; ci++)
did_split |=
crossbow_hem_hypothesize_grandchildren (grandparents[ci],
crossbow_hem_branching_factor);
#if 0
printf (".........................................................\n");
for (iterator = crossbow_root;
(tn = bow_treenode_iterate_all (&iterator));)
{
printf ("%s %g\n", tn->name, tn->prior);
if (tn->children_count == 0)
{
bow_treenode_word_probs_print (tn, 5);
printf ("\n");
bow_treenode_word_leaf_likelihood_ratios_print (tn, 5);
//bow_treenode_word_likelihood_ratios_print (tn, 10);
}
}
#endif
bow_free (grandparents);
return did_split;
}
void
crossbow_hem_cluster ()
{
int di;
crossbow_doc *doc;
double pp, old_pp, test_pp;
treenode *iterator, *tn;
FILE *classify_fp;
int iteration;
char buf[1024];
bow_random_set_seed();
bow_treenode_set_lambdas_uniform (crossbow_root);
/* initialize all data to be at the root */
for (di = 0; di < crossbow_docs->length; di++)
{
int wvi;
bow_wv *wv = crossbow_wv_at_di (di);
doc = bow_array_entry_at_index (crossbow_docs, di);
if (doc->tag != bow_doc_train && doc->tag != bow_doc_unlabeled)
continue;
for (wvi = 0; wvi < wv->num_entries; wvi++)
{
crossbow_root->new_words[wv->entry[wvi].wi] +=
wv->entry[wvi].count;
if (crossbow_hem_loo)
bow_treenode_add_new_loo_for_di_wvi
(crossbow_root, wv->entry[wvi].count, di, wvi,
wv->num_entries, crossbow_docs->length);
}
}
crossbow_root->new_prior = 1.0;
//bow_treenode_set_new_words_from_perturbed_words_all (crossbow_root);
bow_treenode_set_words_from_new_words_all (crossbow_root,
1.0 / crossbow_root->words_capacity);
bow_treenode_set_leaf_prior_from_new_prior_all (crossbow_root, 1.0);
/* Initialize children of the root */
if (crossbow_root->children_count == 0)
crossbow_hem_create_children_for_node (crossbow_root,
crossbow_hem_branching_factor);
/* CROSSBOW_HEM_TEMPERATURE already set */
iteration = 0;
for ( ; crossbow_hem_temperature >= crossbow_hem_temperature_end;
crossbow_hem_temperature *= crossbow_hem_temperature_decay)
{
bow_verbosify (bow_progress, "TEMPERATURE = %g\n",
crossbow_hem_temperature);
printf ("TEMPERATURE = %g\n", crossbow_hem_temperature);
/* Always Add hypothesis children here. */
/* Run EM to convergence. */
old_pp = FLT_MAX;
pp = old_pp / 2;
/* Loop until convergence, i.e. perplexity doesn't change */
while (ABS (old_pp - pp) > 2
&& iteration < crossbow_hem_max_num_iterations)
{
printf ("--------------------------------------------------"
" Iteration %d\n", iteration);
old_pp = pp;
pp = crossbow_hem_em_one_iteration ();
iteration++;
test_pp = crossbow_hem_perplexity (bow_doc_is_test);
printf ("train-pp=%f test-pp=%f \n", pp, test_pp);
for (iterator = crossbow_root;
(tn = bow_treenode_iterate_all (&iterator));)
{
printf ("%s", tn->name);
if (tn->children_count == 0)
{
int ai, ci;
printf (" prior=%g lambdas=[ ", tn->prior);
for (ai = 0; ai < tn->depth + 2; ai++)
printf ("%5.3f ", tn->lambdas[ai]);
printf ("]");
if (0 && crossbow_classes_count > 1)