-
Notifications
You must be signed in to change notification settings - Fork 1
/
EM.cpp
202 lines (189 loc) · 5.53 KB
/
EM.cpp
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
/*
* EM.cpp
*
* Created on: Apr 28, 2015
* Author: prmathur
*/
#pragma once
#include "EM.h"
#include "cpmStore.h"
EM::EM(unsigned int topics, unsigned int iterations, bool printVerbose) {
m_maxIter=iterations;
m_topics=topics;
printModels=printVerbose;
srandom(20);
}
EM::~EM() {
// TODO Auto-generated destructor stub
}
void EM::InitializeParams(){
cpmStore* cpm = &cpmStore::Instance();
for(cpmStore::iterator i = cpm->begin();
i != cpm->end(); i++){
for(unsigned short int j = 0;
j < m_topics; j++){
float noise = ((double) random() / (RAND_MAX)) + 1;
P[i->second][j] = 1 / (cpm->getVocabSize() + noise);
noise = ((double) random() / (RAND_MAX)) + 1; // different noise
Q[i->second][j] = 1/(m_topics + noise);
}
}
normalize();
char filename[20];
sprintf(filename, "init.model");
PrintModel(filename);
}
void EM::normalize(){
cpmStore *cpm = &cpmStore::Instance();
m_totp.clear();
m_totq.clear();
if(cpm==NULL) return;
for(cpmStore::iterator itr = cpm->begin(); itr != cpm->end(); itr++){ // itr->first = word
for(unsigned short int j = 0; j < m_topics; j++){
m_totq[itr->second] += Q[itr->second][j];
m_totp[j] += P[itr->second][j];
}
}
for(cpmStore::iterator itr = cpm->begin(); itr != cpm->end(); itr++){ // itr->first = word
for(unsigned short int j = 0; j < m_topics; j++){
P[itr->second][j] /= m_totp[j];
Q[itr->second][j] /= m_totq[itr->second];
}
}
}
//void EM::ComputeThreadSpecificCounts(thread_specific_data& data){
// for(unsigned short int j = 0; j < m_topics; j++){
// data.product[j] = 1;
// bool flag = false;
// std::vector<Code*> cont_codes = data.context->getCodes();
// for(size_t indx=0; indx < cont_codes.size(); indx++){
// data.product[j] *= P[cont_codes[indx]][j];
// if(data.product[j] < 1e-16) {data.product[j]=0; flag=true; break;}
// }
// if(flag==true) continue;
// data.denominator += (data.product[j] * Q[data.code][j]);
// }
//}
//void EM::CalculatePartialCounts(thread_specific_data& data){
// double temp=0;
// for(unsigned short int j = 0; j < m_topics; j++){
// temp = (data.product[j] * Q[data.code][j]) / data.denominator;
// std::vector<Code*> cont_codes = data.context->getCodes();
// for(size_t indx=0; indx < cont_codes.size(); indx++){
// p[cont_codes[indx]][j] += temp;
// m_totp[j] += temp;
// }
// q[data.code][j]+=temp;
// m_totq[data.code] += temp;
// }
//}
void EM::ComputeExpectedCounts(){
//std::vector<std::thread> threads;
cpmStore *cpm = &cpmStore::Instance();
if(cpm==NULL) {std::cerr<<"Corpus was not loaded properly!\n";exit(0);}
size_t count=0;
for(boost::unordered_map<std::string, Code*>::iterator itr=cpm->begin();
itr!=cpm->end(); itr++){
count++;
if(count%1000==0) std::cerr<<".";
boost::unordered_map<unsigned short int, double> product; // \pi on topics
double denominator=0;
std::vector<Context*> cont_vec = itr->second->getContexts();
for(size_t i=0; i<cont_vec.size(); i++){
for(unsigned short int j = 0; j < m_topics; j++){
if(Q[itr->second][j] == 0) continue;
product[j] = 1;
bool flag = false;
std::vector<Code*> cont_codes = cont_vec[i]->getCodes();
for(size_t indx=0; indx < cont_codes.size(); indx++){
if(P[cont_codes[indx]][j] > 0 && product[j] > 0 && rand()%2==0){
product[j] *= P[cont_codes[indx]][j];
}
if(product[j] < 1e-16) {product[j]=0; flag=true; break;}
}
if(flag==true) continue;
denominator += (product[j] * Q[itr->second][j]);
}
if(denominator==0) {
continue;
}
double temp=0;
for(unsigned short int j = 0; j < m_topics; j++){
if(Q[itr->second][j] == 0 || product[j]==0) continue;
temp = (product[j] * Q[itr->second][j] * itr->second->getCounts()) / (denominator);
std::vector<Code*> cont_codes = cont_vec[i]->getCodes();
for(size_t indx=0; indx < cont_codes.size(); indx++)
p[cont_codes[indx]][j] += temp;
m_totp[j] += cont_codes.size()*temp;
q[itr->second][j]+=temp;
m_totq[itr->second] += temp;
}
}
}
}
void EM::MStep(){
// normalize to probabilities
cpmStore *cpm = &cpmStore::Instance();
size_t count=0;
for(cpmStore::iterator itr = cpm->begin();
itr != cpm->end(); itr++){ // itr->first = word
count++;
if(count%1000==0) std::cerr<<".";
for(unsigned short int j = 0; j < m_topics; j++){
if(m_totq[itr->second] > 0){
Q[itr->second][j] = q[itr->second][j] / m_totq[itr->second];
if(Q[itr->second][j] < 1e-10){
Q[itr->second][j]=0;
std::cerr<<"0\b";
}
}
if(m_totp[j] > 0){
P[itr->second][j] = p[itr->second][j] / m_totp[j];
if(P[itr->second][j] < 1e-10){
P[itr->second][j]=0;
std::cerr<<"0\b";
}
}
}
}
}
void EM::ResetExpectedCounts(){
p.clear();
q.clear();
m_totp.clear();
m_totq.clear();
return;
}
void EM::Train(){
std::cerr<<"\nInitializing Parameters\n";
InitializeParams();
for(size_t i=1; i <= m_maxIter; i++){
std::cerr<<"\nIteration "<<i<<std::endl;
ResetExpectedCounts();
// compute expected counts
std::cerr<<"\nE Step\n";
ComputeExpectedCounts();
// maximise probabilities
std::cerr<<"\nM Step\n";
MStep();
if(printModels){
char filename[20];
sprintf(filename, "%d.iter.model", i);
PrintModel(filename);
}
}
return;
}
void EM::PrintModel(char* filename){
std::ofstream ofs (filename, std::ofstream::out);
boost::unordered_map<Code*, boost::unordered_map<unsigned short int, double> >::iterator itr=Q.begin();
while(itr!=Q.end()){
ofs << itr->first->Decode() << " ";
for(unsigned short int j = 0; j != m_topics; j++){
ofs << Q[itr->first][j] << " ";
}
ofs <<std::endl;
itr++;
}
return;
}