forked from mtresearcher/CPMT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpm1.pl
140 lines (105 loc) · 3.1 KB
/
cpm1.pl
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
#! /usr/bin/perl
use strict;
use Getopt::Long "GetOptions";
use File::Basename;
my $debug=0;
#training data is a seguence of entries
#count W X1,\ldots,Xk:
my(@VOC,%VOC)=();
my($MAXCODE)=1;
my(%TRAIN)=();
my (%P,%Q)=();
my (%p,%totp,%q,%totq)=();
my ($DIM)= 50;
my ($MAXITER)=10;
my ($Model)="Model.csv";
sub Encode(){
my ($word) = @_;
my ($code)=();
if (!defined($code=$VOC{$word})){
$code=($VOC{$word}=$MAXCODE++);
$VOC[$code]=$word;
}
return $code;
}
sub Decode(){
my ($code) = @_;
die "decode: code $code is out of boundaries [0,$MAXCODE)\n" if ($code < 0 || $code >= $MAXCODE);
return $VOC[$code];
}
sub LoadTrain(){
#load training data into a tree
while (chop($_=<STDIN>)){
my ($c,$w,@x)=split(/ +/,$_);
print "$w ",join(" ",@x)," count: $c\n" if $debug;
my $cw=&Encode($w);
my @cx=map &Encode($_) , @x;
print "Encoded: $cw ",join(" ",@cx)," count $c\n" if $debug;
next if $w=~/<\/?s>/; #remove example if w is a separator
$TRAIN{join(" ",($cw,@cx))}=$c;
}};
sub ResetExpectedCounts(){
(%p,%totp,%q,%totq)=();
};
sub ComputeExpectedCounts(){
my ($x,$w,$wx,$i,$j)=();
if (!%P){
print "Initialize parameters\n";
for ($w=1;$w<$MAXCODE;$w++){
for ($i=1;$i<=$DIM;$i++){
$P{$w,$i}=1/($MAXCODE+rand(1));
$Q{$w}->[$i]=1/($DIM+rand(1));
}
}
}
print "Compute expected counts\n";
my %den=(); # denominator
foreach $wx (keys %TRAIN){
my ($w,@x)=split(/ /,$wx); # separting word and context
my $count=$TRAIN{$wx}; # get the counts of the word with the context
my $den=0; my @prod=();
for ($i=1;$i<=$DIM;$i++){ # for all topics
$prod[$i]=1;
foreach $x (@x){$prod[$i]*=$P{$x,$i}}; # get the product of all context for all topics
$den+=$prod[$i] * $Q{$w}->[$i]; # compute the denominator
}
die "den($wx)=0 \n".Dumper(@prod) if $den==0;
my $tmp=0;
for ($i=1;$i<=$DIM;$i++){
$tmp=$count * $prod[$i] * $Q{$w}->[$i]/$den;
foreach $x (@x){$p{$x,$i}+= $tmp; $totp{$i} += $tmp};
$q{$w}->[$i]+=$tmp; $totq{$w} +=$tmp;
}
}
}
#################### MAIN
use Data::Dumper;
&LoadTrain();
#print Dumper(%TRAIN);
#initialize parameters
(%P,%Q)=();
my $totq=0; #variable to avoid repeated access to %totq
for (my $iter=1;$iter<=$MAXITER;$iter++){
print "Start iteration $iter\n";
&ResetExpectedCounts();
&ComputeExpectedCounts();
print Dumper(%totp);
for (my $v=1;$v<$MAXCODE;$v++){
#print "Norm: ".&Decode($v)."\n";
$totq=$totq{$v};
print "Warning: Q(".&Decode($v).")=0\n" if !$totq>0;
for (my $i=1;$i<=$DIM;$i++){
$Q{$v}->[$i]=$q{$v}->[$i]/$totq if $totq>0;
$P{$v,$i} = $p{$v,$i}/ $totp{$i};
}
}
}
print "Saving model\n";
open(OUT," > $Model");
for my $v (keys %Q){
print OUT &Decode($v)." ";
for (my $i=1;$i<=$DIM;$i++){
printf OUT ("%4.3f ",${$Q{$v}}[$i]);
}
print OUT "\n";
}