forked from fizx/libbow-osx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiclass.c
1957 lines (1773 loc) · 57.1 KB
/
multiclass.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
#include <bow/libbow.h>
#include <argp.h>
#include <bow/crossbow.h>
/* Thursday am - changed total_num_mixtures_possible calculation
changed palpha from 1.0 to 0.01
changed malpha from 0 to 1
changed pruning class set size from 4 to 3 */
extern bow_int4str *crossbow_classnames;
static double multiclass_uniform_prior;
static double multiclass_uniform_new_prior;
static double multiclass_mixture_prior_alpha;
static double multiclass_mixture_prior_normalizer;
static double **cached_mixture = NULL;
void multiclass_mixture_clear_cache ();
int compare_ints (const void *x, const void *y)
{
if (*(int*)x > *(int*)y)
return 1;
else if (*(int*)x == *(int*)y)
return 0;
else
return -1;
}
#define CCC crossbow_classes_count
#define DOING_COMBO 1
/* Plus 1 for no class */
#define MAX_NUM_CLASSES (100 + 1)
/* Plus one for root, and plus one for uniform */
#define MAX_NUM_MIXTURE_CLASSES 20
#define MAX_NUM_MIXTURES (MAX_NUM_MIXTURE_CLASSES + 1 + 1)
typedef struct _mcombo {
double prior;
double new_prior;
int doc_count;
/* The class indicies for this multi-label combination */
/* Indexed by cisi, up to cis_size-1; + root + uniform */
int cis[MAX_NUM_MIXTURE_CLASSES];
int cis_size;
/* Indexed by cisi, up to cis_size-1; + root + uniform */
/* The mixture weights for this multi-label combination */
double m[MAX_NUM_MIXTURES];
double new_m[MAX_NUM_MIXTURES];
/* each dimension indexed by CI+1 */
} cmixture;
/* Info on all class mixtures */
static cmixture *cm = NULL;
/* The number of entries in the above */
static int cm_length = 0;
typedef struct _multiclass_score {
double score;
int c[MAX_NUM_MIXTURE_CLASSES];
} multiclass_score;
static int
compare_multiclass_scores (const void *x, const void *y)
{
if (((multiclass_score*)x)->score > ((multiclass_score*)y)->score)
return -1;
else if (((multiclass_score*)x)->score == ((multiclass_score*)y)->score)
return 0;
else
return 1;
}
/* Return a pointer to the cmixture structure for the specific class
set specified by CIS. If CREATE_NEW is non-zero, then create a
cmixture entry if one doesn't already exist. The actual number of
classes in the mixture is returned in ACTUAL_SIZE */
cmixture *
cmixture_for_cis (const int *cis, int cis_size, int create_new,
int *actual_size)
{
static bow_int4str *cmi_map = NULL;
static int cm_size = 0;
int cmi, cisi, num_chars, real_size;
static const int cis_name_size = 512;
char cis_name[cis_name_size], *cis_name_p;
assert (cis_size <= MAX_NUM_MIXTURE_CLASSES);
cis_name_p = cis_name;
real_size = 0;
for (cisi = 0; cisi < cis_size && cis[cisi] >= 0; cisi++)
{
num_chars = sprintf (cis_name_p, "%d,", cis[cisi]);
cis_name_p += num_chars;
assert (cis_name_p - cis_name <= cis_name_size);
real_size++;
}
if (actual_size)
*actual_size = real_size;
if (!cmi_map)
cmi_map = bow_int4str_new (0);
if (create_new)
cmi = bow_str2int (cmi_map, cis_name);
else
cmi = bow_str2int_no_add (cmi_map, cis_name);
if (cmi < 0)
return NULL;
if (cmi >= cm_length)
{
/* Add a new entry for this class mixture combination */
cm_length++;
if (cm == NULL)
{
cm_size = 128;
cm = bow_malloc (cm_size * sizeof (cmixture));
}
if (cmi >= cm_size)
{
cm_size *= 2;
cm = bow_realloc (cm, cm_size * sizeof (cmixture));
}
bow_verbosify (bow_verbose, "New entry for ");
for (cisi = 0; cisi < real_size; cisi++)
bow_verbosify (bow_verbose, "%s,",
bow_int2str (crossbow_classnames, cis[cisi]));
bow_verbosify (bow_verbose, "\n");
/* Initialize the new CM entry */
cm[cmi].prior = 0;
cm[cmi].new_prior = 0;
cm[cmi].doc_count = 0;
cm[cmi].cis_size = real_size;
for (cisi = 0; cisi < real_size; cisi++)
{
cm[cmi].cis[cisi] = cis[cisi];
cm[cmi].m[cisi] = 1.0 / real_size;
cm[cmi].new_m[cisi] = 0;
}
for (cisi = real_size; cisi < MAX_NUM_MIXTURE_CLASSES; cisi++)
cm[cmi].cis[cisi] = -1;
for (cisi = real_size; cisi < MAX_NUM_MIXTURES; cisi++)
{
cm[cmi].m[cisi] = 0;
cm[cmi].new_m[cisi] = 0;
}
}
return &(cm[cmi]);
}
void
cmixture_set_from_new (int set_p_flag, double p_alpha, double m_alpha)
{
double p_sum;
double m_sum;
int cmi, l, total_num_mixtures_possible;
cmixture *m;
/* Get normalization constants */
assert (MAX_NUM_CLASSES > crossbow_classes_count);
p_sum = 0;
for (cmi = 0; cmi < cm_length; cmi++)
{
m = &(cm[cmi]);
p_sum += m->new_prior + p_alpha;
/* Don't touch the mixtures cached at test time. */
if (m->doc_count <= 0)
continue;
m_sum = 0;
assert (m->cis_size+2 <= MAX_NUM_MIXTURES);
for (l = 0; l < m->cis_size+2; l++)
m_sum += m->new_m[l] + m_alpha;
assert (m_sum);
for (l = 0; l < m->cis_size+2; l++)
{
m->m[l] = (m->new_m[l] + m_alpha) / m_sum;
assert (m->m[l] > 0);
m->new_m[l] = 0;
assert (m->m[l] <= 1.0 && m->m[l] >= 0.0);
}
}
/* xxx This number possible is an over-estimate? */
total_num_mixtures_possible = 1;
for (l = crossbow_classes_count;
(l > (crossbow_classes_count - MAX_NUM_MIXTURE_CLASSES)
&& l >= 1);
l--)
total_num_mixtures_possible *= l;
p_sum += (total_num_mixtures_possible - cm_length) * p_alpha;
assert (p_sum > 0);
multiclass_mixture_prior_alpha = p_alpha;
multiclass_mixture_prior_normalizer = p_sum;
/* Set p and m's from normalized new data, and zero the new data */
for (cmi = 0; cmi < cm_length; cmi++)
{
m = &(cm[cmi]);
if (set_p_flag)
{
m->prior = (m->new_prior + p_alpha) / p_sum;
assert (m->prior <= 1.0 && m->prior >= 0.0);
}
m->new_prior = 0;
}
/* Clear the mixture cache so it will get reset */
multiclass_mixture_clear_cache ();
}
void
cmixture_print_diagnostics (FILE *out)
{
int i, l, cmi;
cmixture *m;
for (cmi = 0; cmi < cm_length; cmi++)
{
m = &(cm[cmi]);
/* Skip over class mixtures that have no training data */
if (m->doc_count <= 0)
continue;
/* Print the list of classes */
for (i = 0; i < MAX_NUM_MIXTURE_CLASSES; i++)
if (m->cis[i] >= 0)
fprintf (out, "%s,", bow_int2str (crossbow_classnames, m->cis[i]));
fprintf (out, " prior=%g ", m->prior);
for (l = 0; l < m->cis_size+2; l++)
fprintf (out, "%g,", m->m[l]);
fprintf (out, "\n");
}
}
void
multiclass_place_labeled_data ()
{
int di, wvi;
crossbow_doc *doc;
treenode *node;
bow_wv *wv;
int cmi, cisi;
cmixture *m;
int l, cis_size;
/* Clear all previous information. */
bow_treenode_set_new_words_to_zero_all (crossbow_root);
bow_treenode_free_loo_and_new_loo_all (crossbow_root, crossbow_docs->length);
bow_treenode_set_prior_from_new_prior_all (crossbow_root, 0);
multiclass_uniform_new_prior = 0;
/* Clear MC */
for (cmi = 0; cmi < cm_length; cmi++)
{
cm[cmi].doc_count = 0;
cm[cmi].prior = 0;
cm[cmi].new_prior = 0;
for (l = 0; l < MAX_NUM_MIXTURES; l++)
{
cm[cmi].m[l] = 0;
cm[cmi].new_m[l] = 0;
}
}
/* Initialize the word distributions and LOO entries with the data
and initialize lambdas to uniform */
for (di = 0; di < crossbow_docs->length; di++)
{
doc = bow_array_entry_at_index (crossbow_docs, di);
/* Make sure that the CIS are in sorted order */
if (doc->cis_size > 1)
qsort (doc->cis, doc->cis_size, sizeof (int), compare_ints);
/* If space for this document's mixture hasn't already been allocated,
do that now. */
if (doc->cis_mixture == NULL)
doc->cis_mixture = bow_malloc ((doc->cis_size + 2) * sizeof (double));
wv = crossbow_wv_at_di (di);
if (doc->tag != bow_doc_train)
continue;
/* Temporary fix */
if (strstr (doc->filename, ".include")
|| strstr (doc->filename, ".exclude"))
continue;
/* Put the data in each of the leaf classes to which the
document belongs, and lastly the root. */
for (cisi = 0; cisi <= doc->cis_size; cisi++)
{
if (cisi == doc->cis_size)
node = crossbow_root;
else
{
assert (crossbow_root->children_count > doc->cis[cisi]);
node = crossbow_root->children[doc->cis[cisi]];
}
node->new_prior++;
multiclass_uniform_new_prior++;
for (wvi = 0; wvi < wv->num_entries; wvi++)
{
node->new_words[wv->entry[wvi].wi] += wv->entry[wvi].count;
bow_treenode_add_new_loo_for_di_wvi
(node, wv->entry[wvi].count, di, wvi,
wv->num_entries, crossbow_docs->length);
}
}
/* Put data into MC */
m = cmixture_for_cis (doc->cis, doc->cis_size, 1, &cis_size);
assert (cis_size == doc->cis_size);
m->doc_count++;
m->new_prior += 1.0;
for (cisi = 0; cisi < doc->cis_size+2; cisi++)
m->new_m[cisi] += 1.0;
}
bow_treenode_set_prior_and_extra_from_new_prior_all
(crossbow_root, &multiclass_uniform_new_prior,
&multiclass_uniform_prior, 0);
bow_treenode_set_words_from_new_words_all (crossbow_root, 0);
cmixture_set_from_new (1, 0.01, 1);
}
double
multiclass_cis_overlap (int *cis1, int cis1_size, int *cis2, int cis2_size)
{
int cisi1, cisi2;
double overlap = 0;
#if 0
if (cis1_size == cis2_size)
overlap++;
#endif
for (cisi1 = cisi2 = 0; cisi2 < cis2_size; cisi2++)
{
while (cisi1 < cis1_size && cis1[cisi1] < cis2[cisi2])
cisi1++;
if (cis1[cisi1] == cis2[cisi2])
overlap++;
}
return 2 * overlap / (cis1_size + cis2_size);
}
/* Erase the cached information used by MULTICLASS_MIXTURE_GIVEN_CIS(),
forcing it to be re-calculated. */
void
multiclass_mixture_clear_cache ()
{
int cisi, cmi;
if (cached_mixture)
{
for (cisi = 0; cisi < MAX_NUM_MIXTURE_CLASSES; cisi++)
if (cached_mixture[cisi])
bow_free (cached_mixture[cisi]);
bow_free (cached_mixture);
cached_mixture = NULL;
}
/* Clear the CMIXTURE cache by changing the special "has cached
average mixture" flag of -1 back to the "simply has no data, no
cached mixture" of 0. */
for (cmi = 0; cmi < cm_length; cmi++)
{
if (cm[cmi].doc_count == -1)
cm[cmi].doc_count = 0;
}
}
/* Place into MIXTURE the mixture weights for the class set specified by
CIS. When this class set appeared in the training data, this is simply
a matter of copying the mixtures from the global CM structure. When it
didn't, various forms of backoff are used. This function caches its
backoff calculations. The above function clears the cache, which should
happen any time mixtures in CM are changed. */
void
multiclass_mixture_given_cis (int *cis, int cis_size, double *mixture)
{
cmixture *m;
int cisi;
assert (cis_size <= MAX_NUM_MIXTURE_CLASSES);
if (cached_mixture == NULL)
{
cached_mixture = bow_malloc((MAX_NUM_MIXTURE_CLASSES+1)*sizeof(double*));
/* Entry 0 never gets used. */
for (cisi = 0; cisi < MAX_NUM_MIXTURE_CLASSES+1; cisi++)
cached_mixture[cisi] = NULL;
}
m = cmixture_for_cis (cis, cis_size, 0, 0);
if (m && !m->doc_count == 0)
{
/* This set of classes exists in the training data, use the
MAP-calculated mixture weights. */
for (cisi = 0; cisi < cis_size + 2; cisi++)
mixture[cisi] = m->m[cisi];
}
else
{
/* This set of classes appeared nowhere in the training data,
backoff to an average of related mixtures, and cache the
results in a (possibly) new CMIXTURE extry. */
int cmi, cisimb;
double bmixture_sum, *mixture_count, similarity;
cmixture *mb;
/* Make sure that there is at least one training document
with this label. */
for (cisi = 0; cisi < cis_size; cisi++)
assert (crossbow_root->children[cis[cisi]]->prior);
/* Get a (possibly new) CMIXTURE extry; We will set
DOC_COUNT==-1 to indicate that it has a mixture cached from
the following calculation. */
m = cmixture_for_cis (cis, cis_size, 1, 0);
assert (m->doc_count == 0);
m->doc_count = -1;
mixture_count = alloca (MAX_NUM_MIXTURES * sizeof (double));
for (cisi = 0; cisi < cis_size+2; cisi++)
{
m->m[cisi] = 0;
mixture_count[cisi] = 0;
}
/* Go through all mixtures for which there is training data */
for (cmi = 0; cmi < cm_length; cmi++)
{
mb = &(cm[cmi]);
if (mb->doc_count <= 0)
continue;
similarity = multiclass_cis_overlap (mb->cis, mb->cis_size,
cis, cis_size);
if (similarity == 0)
continue;
for (cisimb = cisi = 0; cisimb < mb->cis_size; cisimb++)
{
while (cisi < cis_size && cis[cisi] < mb->cis[cisimb])
cisi++;
if (mb->cis[cisimb] == cis[cisi])
{
m->m[cisi] += mb->m[cisimb] * similarity;
assert (m->m[cisi] == m->m[cisi]);
mixture_count[cisi] += similarity;
}
}
/* Likewise for the root and uniform mixtures */
m->m[cis_size] += mb->m[mb->cis_size] * similarity;
mixture_count[cis_size] += similarity;
m->m[cis_size+1] += mb->m[mb->cis_size+1] * similarity;
mixture_count[cis_size+1] += similarity;
}
/* Take the average of each column */
for (cisi = 0; cisi < cis_size+2; cisi++)
{
assert (mixture_count[cisi]);
m->m[cisi] /= mixture_count[cisi];
assert (m->m[cisi] == m->m[cisi]);
}
/* Normalize the mixture to sum to one */
bmixture_sum = 0;
for (cisi = 0; cisi < cis_size+2; cisi++)
bmixture_sum += m->m[cisi];
assert (bmixture_sum > 0);
/* Normalize and put into MIXTURE for return */
for (cisi = 0; cisi < cis_size+2; cisi++)
{
m->m[cisi] /= bmixture_sum;
mixture[cisi] = m->m[cisi];
}
}
#if 0
double normalizer = 0;
int cisi2;
/* Another (unused) estimate based on adding all mixtures */
/* This mixture did not occur in the training data, used a smoothed
estimate. */
for (cisi = 0; cisi < cis_size + 2; cisi++)
mixture[cisi] = 1.0;
normalizer = (cis_size + 2) * 1.0;
for (cmi = 0; cmi < cm_length; cmi++)
{
for (cisi = cisi2 = 0; cisi2 < cm[cmi]->cis_size; cisi2++)
{
while (cisi < cisi2)
cisi++;
if (cm[cmi]->cis[cisi2] == cis[cisi])
{
mixture[cisi] += cm[cmi]->cis[cisi2];
normalizer += mixture[cisi];
}
}
}
for (cisi = 0; cisi < cis_size; cisi++)
mixture[cisi] /= normalizer;
return;
#endif
#if 0
/* Another (unused) option is to use a completely factored
representation */
/* Calculcate normalized mixture weights just from the treenode priors,
i.e., not using the CMIXTURE. These may not actually get used. */
/* Plus one for the root, plus one for the uniform */
double mixture_prior_sum;
mixture_weights = alloca ((cis_size + 1 + 1) * sizeof (double));
mixture_prior_sum = 0;
for (cisi = 0; cisi < cis_size; cisi++)
{
assert (cis[cisi] >= 0);
mixture_prior_sum += crossbow_root->children[cis[cisi]]->prior;
}
mixture_prior_sum += crossbow_root->prior + multiclass_uniform_prior;
for (cisi = 0; cisi < cis_size; cisi++)
if (cis[cisi] >= 0)
mixture_weights[cisi] =
crossbow_root->children[cis[cisi]]->prior / mixture_prior_sum;
mixture_weights[cis_size] = crossbow_root->prior / mixture_prior_sum;
mixture_weights[cis_size+1] = multiclass_uniform_prior / mixture_prior_sum;
#endif
}
/* MIXTURE must be as large as CIS_SIZE+2 */
void
multiclass_mixture_given_doc_and_cis (crossbow_doc *doc,
int *cis, int cis_size,
double *mixture)
{
bow_wv *wv;
double *cis_mixture;
double mixture_sum;
treenode *node;
int cisi, wvi;
int num_nodes;
double *node_data_prob;
double node_data_prob_sum;
double *node_membership;
wv = crossbow_wv_at_di (doc->di);
cis_mixture = alloca (sizeof (double) * (cis_size + 2));
multiclass_mixture_given_cis (cis, cis_size, cis_mixture);
num_nodes = crossbow_root->children_count + 1 + 1;
node_membership = alloca (num_nodes * sizeof (double));
node_data_prob = alloca (num_nodes * sizeof (double));
for (cisi = 0; cisi <= cis_size+1; cisi++)
mixture[cisi] = 0;
mixture_sum = 0;
for (wvi = 0; wvi < wv->num_entries; wvi++)
{
/* Analagous to the per-word E-step */
node_data_prob_sum = 0;
for (cisi = 0; cisi <= cis_size; cisi++)
{
if (cisi == cis_size)
node = crossbow_root;
else
node = crossbow_root->children[cis[cisi]];
node_data_prob[cisi] = cis_mixture[cisi] *
bow_treenode_pr_wi_loo_local(node,wv->entry[wvi].wi,doc->di,wvi);
assert (node_data_prob[cisi] >= 0);
node_data_prob_sum += node_data_prob[cisi];
}
/* For the uniform distribution */
node_data_prob[cis_size+1] = cis_mixture[cis_size+1] *
(1.0 / bow_num_words ());
assert (node_data_prob[cis_size+1] >= 0);
node_data_prob_sum += node_data_prob[cis_size+1];
assert (node_data_prob_sum != 0);
/* Normalize the node data probs, so they are membership
probabilities. */
for (cisi = 0; cisi <= cis_size+1; cisi++)
node_membership[cisi] =
node_data_prob[cisi] / node_data_prob_sum;
/* Analagous to the per-word M-step */
for (cisi = 0; cisi <= cis_size+1; cisi++)
{
mixture[cisi] += wv->entry[wvi].count * node_membership[cisi];
mixture_sum += mixture[cisi];
}
}
/* Normalize the mixture to be returned */
for (cisi = 0; cisi <= cis_size+1; cisi++)
mixture[cisi] /= mixture_sum;
}
/* MIXTURE must be as large as CIS_SIZE+2 */
void
multiclass_iterated_mixture_given_doc_and_cis (crossbow_doc *doc,
int *cis, int cis_size,
double *mixture)
{
bow_wv *wv;
double *cis_mixture;
double mixture_sum;
treenode *node;
int cisi, wvi;
int num_nodes;
double *node_data_prob;
double node_data_prob_sum;
double *node_membership;
//double pp, old_pp;
wv = crossbow_wv_at_di (doc->di);
cis_mixture = alloca (sizeof (double) * (cis_size + 2));
multiclass_mixture_given_cis (cis, cis_size, cis_mixture);
num_nodes = crossbow_root->children_count + 1 + 1;
node_membership = alloca (num_nodes * sizeof (double));
node_data_prob = alloca (num_nodes * sizeof (double));
for (cisi = 0; cisi <= cis_size+1; cisi++)
mixture[cisi] = 0;
mixture_sum = 0;
for (wvi = 0; wvi < wv->num_entries; wvi++)
{
/* Analagous to the per-word E-step */
node_data_prob_sum = 0;
for (cisi = 0; cisi <= cis_size; cisi++)
{
if (cisi == cis_size)
node = crossbow_root;
else
node = crossbow_root->children[cis[cisi]];
node_data_prob[cisi] = cis_mixture[cisi] *
bow_treenode_pr_wi_loo_local(node,wv->entry[wvi].wi,doc->di,wvi);
assert (node_data_prob[cisi] >= 0);
node_data_prob_sum += node_data_prob[cisi];
}
/* For the uniform distribution */
node_data_prob[cis_size+1] = cis_mixture[cis_size+1] *
(1.0 / bow_num_words ());
assert (node_data_prob[cis_size+1] >= 0);
node_data_prob_sum += node_data_prob[cis_size+1];
assert (node_data_prob_sum != 0);
/* Normalize the node data probs, so they are membership
probabilities. */
for (cisi = 0; cisi <= cis_size+1; cisi++)
node_membership[cisi] =
node_data_prob[cisi] / node_data_prob_sum;
/* Analagous to the per-word M-step */
for (cisi = 0; cisi <= cis_size+1; cisi++)
{
mixture[cisi] += wv->entry[wvi].count * node_membership[cisi];
mixture_sum += mixture[cisi];
}
}
/* Normalize the mixture to be returned */
for (cisi = 0; cisi <= cis_size+1; cisi++)
mixture[cisi] /= mixture_sum;
}
/* MIXTURE must be as large as CROSSBOW_ROOT->CHILDREN_COUNT+2 */
void
multiclass_mixture_given_doc (crossbow_doc *doc,
double *mixture)
{
int mixture_count = crossbow_root->children_count + 2;
bow_wv *wv;
double mixture_sum;
treenode *node;
int mi, wvi;
double node_membership_sum;
double *node_membership;
wv = crossbow_wv_at_di (doc->di);
node_membership = alloca (mixture_count * sizeof (double));
for (mi = 0; mi < mixture_count; mi++)
mixture[mi] = 0;
mixture_sum = 0;
for (wvi = 0; wvi < wv->num_entries; wvi++)
{
/* Analagous to the per-word E-step */
node_membership_sum = 0;
for (mi = 0; mi <= mixture_count-2; mi++)
{
if (mi == mixture_count-2)
node = crossbow_root;
else
node = crossbow_root->children[mi];
if (doc->tag == bow_doc_train || doc->tag == bow_doc_unlabeled)
node_membership[mi] =
bow_treenode_pr_wi_loo_local (node,wv->entry[wvi].wi,
doc->di,wvi);
else
node_membership[mi] = node->words[wv->entry[wvi].wi];
assert (node_membership[mi] >= 0);
node_membership_sum += node_membership[mi];
}
/* For the uniform distribution */
node_membership[mixture_count-1] = 1.0 / bow_num_words ();
node_membership_sum += node_membership[mixture_count-1];
assert (node_membership_sum != 0);
/* Normalize the node data probs, so they are membership
probabilities. */
for (mi = 0; mi < mixture_count; mi++)
node_membership[mi] = node_membership[mi] / node_membership_sum;
/* Analagous to the per-word M-step */
for (mi = 0; mi < mixture_count; mi++)
{
mixture[mi] += wv->entry[wvi].count * node_membership[mi];
mixture_sum += mixture[mi];
}
}
/* Normalize the mixture to be returned */
assert (mixture_sum);
for (mi = 0; mi < mixture_count; mi++)
{
mixture[mi] /= mixture_sum;
//assert (mixture[mi] > 0);
}
}
/* Return the most likely mixture over mixture components, assuming
that we are already committed to including the classes in CIS, and
that we probabilistically remove the words that they account for.
MIXTURE must be as large as CROSSBOW_ROOT->CHILDREN_COUNT+2 */
void
multiclass_mixture_given_doc_and_partial_cis (crossbow_doc *doc,
const int *cis, int cis_size,
const int *exclude_cis,
int exclude_cis_size,
double *mixture)
{
int mixture_count = crossbow_root->children_count + 2;
bow_wv *wv;
double mixture_sum;
treenode *node;
int mi, wvi, cisi;
double node_membership_sum;
double *node_membership;
double *node_word_prob;
double average_word_prob_cis, incr;
wv = crossbow_wv_at_di (doc->di);
node_membership = alloca (mixture_count * sizeof (double));
node_word_prob = alloca (mixture_count * sizeof (double));
for (mi = 0; mi < mixture_count; mi++)
mixture[mi] = 0;
mixture_sum = 0;
for (wvi = 0; wvi < wv->num_entries; wvi++)
{
/* Analagous to the per-word E-step */
node_membership_sum = 0;
for (mi = 0; mi <= mixture_count-2; mi++)
{
if (mi == mixture_count-2)
node = crossbow_root;
else
node = crossbow_root->children[mi];
node_word_prob[mi] =
bow_treenode_pr_wi_loo_local (node,wv->entry[wvi].wi,doc->di,wvi);
node_membership[mi] = node_word_prob[mi];
assert (node_membership[mi] >= 0);
}
/* For the uniform distribution */
node_membership[mixture_count-1] = 1.0 / bow_num_words ();
/* Calculate the average word probability of the classes
explicitly included with CIS, and the always-included root
and uniform distribution. Zero the mixture probabilities for
those mixtures. */
average_word_prob_cis = 0;
for (cisi = 0; cisi < cis_size; cisi++)
{
average_word_prob_cis += node_membership[cis[cisi]];
node_membership[cis[cisi]] = 0;
}
average_word_prob_cis += node_membership[mixture_count-2];
node_membership[mixture_count-2] = 0;
average_word_prob_cis += node_membership[mixture_count-1];
node_membership[mixture_count-1] = 0;
average_word_prob_cis /= cis_size + 2;
/* Zero the probabilities of the classes explicitly excluded */
for (cisi = 0; cisi < exclude_cis_size; cisi++)
node_membership[exclude_cis[cisi]] = 0;
/* Subtract the average */
for (mi = 0; mi < mixture_count; mi++)
{
node_membership[mi] -= average_word_prob_cis;
if (node_membership[mi] < 0)
node_membership[mi] = 0;
node_membership_sum += node_membership[mi];
}
#if 1
/* If any of the NODE_MEMBERSHIP's are non-zero, normalize the
node data probs, so they are membership probabilities. */
if (node_membership_sum != 0)
for (mi = 0; mi < mixture_count; mi++)
node_membership[mi] = node_membership[mi] / node_membership_sum;
#endif
/* Analagous to the per-word M-step */
for (mi = 0; mi < mixture_count; mi++)
{
if (node_membership[mi] == 0)
continue;
incr= (wv->entry[wvi].count * node_membership[mi]
* log (node_word_prob[mi]/average_word_prob_cis));
assert (incr >= 0);
mixture[mi] += incr;
mixture_sum += mixture[mi];
}
}
/* Normalize the mixture to be returned */
for (mi = 0; mi < mixture_count; mi++)
mixture[mi] /= mixture_sum;
}
/* Return the perplexity */
double
multiclass_em_one_iteration ()
{
int di;
crossbow_doc *doc;
bow_wv *wv;
treenode *node;
int cisi, wvi;
int num_nodes;
double *node_word_prob, log_prob_of_data2;
double node_membership_sum, word_prob, log_prob_of_data, deposit;
int num_data_words = 0; /* the number of word occurrences */
double *node_membership;
cmixture *m;
int cis_size;
double *mixture_all;
/* One node for each topic, plus one for all-english, plus one for uniform */
num_nodes = crossbow_root->children_count + 1 + 1;
node_membership = alloca (num_nodes * sizeof (double));
node_word_prob = alloca (num_nodes * sizeof (double));
mixture_all = alloca ((crossbow_root->children_count+2) * sizeof(double));
log_prob_of_data = log_prob_of_data2 = 0;
for (di = 0; di < crossbow_docs->length; di++)
{
doc = bow_array_entry_at_index (crossbow_docs, di);
if (doc->tag != bow_doc_train && doc->tag != bow_doc_unlabeled)
continue;
/* Temporary fix */
if (strstr (doc->filename, ".include")
|| strstr (doc->filename, ".exclude"))
continue;
multiclass_mixture_given_doc (doc, mixture_all);
bow_verbosify (bow_verbose, "%s ", doc->filename);
for (cisi = 0; cisi < crossbow_root->children_count+2; cisi++)
{
bow_verbosify (bow_verbose, "%s=%g,",
(cisi < crossbow_root->children_count
? bow_int2str (crossbow_classnames, cisi)
: (cisi == crossbow_root->children_count
? "root"
: "uniform")),
mixture_all[cisi]);
}
bow_verbosify (bow_verbose, "\n");
/* Get the word vector for this document, and for each word,
estimate its membership probability in each of its classes
(and the root class), and then gather stats for the M-step */
wv = crossbow_wv_at_di (di);
m = cmixture_for_cis (doc->cis, doc->cis_size, 0, &cis_size);
assert (m);
assert (m->doc_count > 0);
/* Zero the document-specific mixture in preparation for incrementing */
for (cisi = 0; cisi < cis_size + 2; cisi++)
doc->cis_mixture[cisi] = 0;
for (wvi = 0; wvi < wv->num_entries; wvi++)
{
num_data_words += wv->entry[wvi].count;
/* Per-word E-step */
node_membership_sum = 0;
for (cisi = 0; cisi <= doc->cis_size; cisi++)
{
if (cisi == doc->cis_size)
node = crossbow_root;
else
node = crossbow_root->children[doc->cis[cisi]];
node_word_prob[cisi] =
bow_treenode_pr_wi_loo_local (node, wv->entry[wvi].wi,
di, wvi);
node_membership[cisi] = m->m[cisi] * node_word_prob[cisi];
assert (node_word_prob[cisi] >= 0);
node_membership_sum += node_membership[cisi];
}
/* For the uniform distribution */
node_word_prob[doc->cis_size+1] = (1.0 / bow_num_words ());
node_membership[doc->cis_size+1] = m->m[doc->cis_size+1] *
node_word_prob[doc->cis_size+1];
node_membership_sum += node_membership[doc->cis_size+1];
assert (node_membership_sum != 0);
/* Normalize the node membership probs. Also increment
perplexity */
word_prob = 0;
for (cisi = 0; cisi <= doc->cis_size+1; cisi++)
{
node_membership[cisi] /= node_membership_sum;
word_prob += node_membership[cisi] * node_word_prob[cisi];
if (node_membership[cisi])
log_prob_of_data2 += (node_membership[cisi]
* wv->entry[wvi].count
* log (node_word_prob[cisi]));
}
log_prob_of_data += wv->entry[wvi].count * log (word_prob);
/* Per-word M-step */
for (cisi = 0; cisi <= doc->cis_size; cisi++)
{
if (cisi == doc->cis_size)
node = crossbow_root;
else
node = crossbow_root->children[doc->cis[cisi]];
deposit = wv->entry[wvi].count * node_membership[cisi];
node->new_words[wv->entry[wvi].wi] += deposit;
bow_treenode_add_new_loo_for_di_wvi
(node, deposit, di, wvi,
wv->num_entries, crossbow_docs->length);
/* For non-combo version */
node->new_prior += deposit;
/* For combo version */
m->new_m[cisi] += deposit;
doc->cis_mixture[cisi] += deposit;
}
/* For the uniform distribution */
deposit = wv->entry[wvi].count * node_membership[doc->cis_size+1];
multiclass_uniform_new_prior += deposit;
m->new_m[doc->cis_size+1] += deposit;
doc->cis_mixture[cis_size+1] += deposit;
}
/* Normalize the document-specific CIS_MIXTURE, (and print it out) */
{
double max = -FLT_MAX;
double cis_mixture_sum;
for (cisi = 0; cisi < cis_size+2; cisi++)
if (doc->cis_mixture[cisi] > max)
max = doc->cis_mixture[cisi];
cis_mixture_sum = 0;
for (cisi = 0; cisi < cis_size+2; cisi++)
{
//doc->cis_mixture[cisi] = exp (doc->cis_mixture[cisi] - max);
cis_mixture_sum += doc->cis_mixture[cisi];
}
bow_verbosify (bow_verbose, "%s ", doc->filename);
for (cisi = 0; cisi < cis_size+2; cisi++)
{
doc->cis_mixture[cisi] /= cis_mixture_sum;
bow_verbosify (bow_verbose, "%s=%g,",
(cisi < cis_size
? bow_int2str (crossbow_classnames, doc->cis[cisi])
: (cisi == cis_size
? "root"
: "uniform")),
doc->cis_mixture[cisi]);
}
bow_verbosify (bow_verbose, "\n");
}
}
/* Normalize all per-word M-step results */
bow_treenode_set_words_from_new_words_all (crossbow_root, 0.0);