-
Notifications
You must be signed in to change notification settings - Fork 2
/
plsa.cpp
379 lines (350 loc) · 7.63 KB
/
plsa.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
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
#include <math.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include "plsa.h"
using namespace std;
PLSAOBJ::PLSAOBJ()
{
doc_num = MAX_DOC_NUM_;
word_num = MAX_WORD_NUM_;
topic_num = MAX_TOPIC_NUM_;
pt_d = new float* [doc_num];
for(int i=0;i<doc_num;i++)
{
double sum = 0;
pt_d[i] = new float[topic_num];
for(int j=0;j<topic_num;j++)
{
pt_d[i][j] = (float)(rand()%99+1)/100;
sum += pow(pt_d[i][j],2);
}
sum = sqrt(sum);
for(int j=0;j<topic_num;j++)
{
pt_d[i][j] = pt_d[i][j]/sum;
}
}
pw_t = new float* [topic_num];
for(int i=0;i<topic_num;i++)
{
pw_t[i] = new float[word_num];
double sum = 0;
for(int j=0;j<word_num;j++)
{
pw_t[i][j] = (float)(rand()%99+1)/100;//(float)(rand()%((int)(restP*100))+1)/100;
sum += pow(pw_t[i][j],2);
}
sum = sqrt(sum);
for(int j=0;j<word_num;j++)
{
pw_t[i][j] = pw_t[i][j]/sum;//(float)(rand()%((int)(restP*100))+1)/100;
}
}
}
PLSAOBJ::~PLSAOBJ()
{
doc_num = MAX_DOC_NUM_;
word_num = MAX_WORD_NUM_;
topic_num = MAX_TOPIC_NUM_;
for(int i=0;i<doc_num;i++)
{
delete[] pt_d[i];
}
delete[] pt_d;
for(int i=0;i<topic_num;i++)
{
delete[] pw_t[i];
}
delete[] pw_t;
}
void PLSAOBJ::EStep(double** train_data)
{
//calculate p(tk|di)
cout<<"e-step"<<endl;
for(int i=0;i<doc_num;i++)
for(int j=0;j<word_num;j++)
{
double down = 0;
for(int kk=0;kk<topic_num;kk++)
{
down += pw_t[kk][j]*pt_d[i][kk];
}
if(train_data[i][j]!=0)
{
int tmpindex = i*word_num+j;
map<int,float*>::iterator itr;
itr = pt_wd.find(tmpindex);
float* tmpP = itr->second;
for(int k=0;k<topic_num;k++)
{
double top = pw_t[k][j]*pt_d[i][k];
tmpP[k] = top/down;
}
}
}
}
void PLSAOBJ::MStep(double** train_data)
{
//calc p(w|t)
cout<<"m-step"<<endl;
for(int i=0;i<topic_num;i++)
{
double down = 0;
for(int k=0;k<doc_num;k++)
{
for(int kk=0;kk<word_num;kk++)
{
if(train_data[k][kk]!=0)
{
int tmpindex = k*word_num+kk;
map<int,float*>::iterator itr = pt_wd.find(tmpindex);
float* tmpP = itr->second;
down += train_data[k][kk]*tmpP[i];
}
}
}
for(int j=0;j<word_num;j++)
{
double top = 0;
for(int k=0;k<doc_num;k++)
{
if(train_data[k][j]!=0)
{
int tmpindex = k*word_num+j;
map<int,float*>::iterator itr = pt_wd.find(tmpindex);
float* tmpP = itr->second;
top += train_data[k][j]*tmpP[i];
}
}
if(top>0&&down>0)
pw_t[i][j] = top/down;
}
}
//calc p(t|d)
for(int i=0;i<doc_num;i++)
{
double down = 0;
for(int k=0;k<word_num;k++)
{
down += train_data[i][k];
}
for(int j=0;j<topic_num;j++)
{
double top = 0;
for(int k=0;k<word_num;k++)
{
if(train_data[i][k]!=0)
{
int tmpindex = i*word_num + k;
map<int,float*>::iterator itr = pt_wd.find(tmpindex);
float* tmpP = itr->second;
top += train_data[i][k]*tmpP[j];
}
}
if(top>0&&down>0)
pt_d[i][j] = top/down;
}
}
}
double PLSAOBJ::LogLikehood(double** train_data)
{
double likehood = 0;
for(int i=0;i<doc_num;i++)
for(int j=0;j<word_num;j++)
{
if(train_data[i][j]!=0)
{
double tmp1 = 0;
for(int k=0;k<topic_num;k++)
{
tmp1 += pt_d[i][k]*pw_t[k][j];
}
if(tmp1!=0)
{
likehood += train_data[i][j]*log(tmp1);
}
}
}
return likehood;
}
int PLSAOBJ::TrainModel(double** train_data,int d_num,int w_num,int t_num,double eps,int max_iter)
{
doc_num = d_num;
word_num = w_num;
topic_num = t_num;
double tmpsum = 0;
for(int i=0;i<doc_num;i++)
for(int j=0;j<word_num;j++)
{
if(train_data[i][j]!=0)
{
int tmpindex = i*word_num+j;
float* tmpP = new float[topic_num];
///*
for(int k=0;k<topic_num;k++)
{
tmpP[k] = (float)(rand()%99+1)/100;//(double)(rand()%((int)(100*restP))+1)/100;
tmpsum += pow(tmpP[k],2);
}
//*/
pt_wd.insert(pair<int,float*>(tmpindex,tmpP));
}
}
tmpsum = sqrt(tmpsum);
for(map<int,float*>::iterator itr=pt_wd.begin();itr!=pt_wd.end();itr++)
{
for(int k=0;k<topic_num;k++)
itr->second[k] = (itr->second[k])/tmpsum;
}
double likehood = 99;
double last_likehood = 0;
struct timeval start_t,end_t;
int cur_iter = 0;
while(abs(likehood-last_likehood)>eps&&cur_iter<max_iter)
//while(cur_iter<max_iter)
{
cur_iter++;
gettimeofday(&start_t,NULL);
last_likehood = likehood;
//E step
EStep(train_data);
//M step
MStep(train_data);
//calculate likelyhood
likehood = LogLikehood(train_data);
gettimeofday(&end_t,NULL);
double timeuse = end_t.tv_sec-start_t.tv_sec;
cout<<"likehood: "<<likehood<<endl;
cout<<"time consume: "<<timeuse<<endl;
}
for(map<int,float*>::iterator itr=pt_wd.begin();itr!=pt_wd.end();itr++)
{
delete[] itr->second;
}
return 0;
}
int PLSAOBJ::Inference(double* src,double* dst,int& len,int iter_max)
{
len = topic_num;
float** tmpP_t_w = new float*[word_num];
for(int i=0;i<word_num;i++)
{
tmpP_t_w[i] = new float[topic_num];
}
//rand initial
double sum = 0;
for(int i=0;i<topic_num;i++)
{
dst[i] = (float)(rand()%99+1)/100;
sum += pow(dst[i],2);
}
sum = sqrt(sum);
for(int i=0;i<topic_num;i++)
{
dst[i] = dst[i]/sum;
}
int iter = 0;
while(iter<iter_max)
{
iter++;
//E step:
for(int i=0;i<word_num;i++)
{
double down = 0;
for(int j=0;j<topic_num;j++)
{
down += dst[j]*pw_t[j][i];
}
for(int j=0;j<topic_num;j++)
{
double top = dst[j]*pw_t[j][i];
tmpP_t_w[i][j] = top/down;
}
}
//M step
double down1 = 0;
for(int i=0;i<word_num;i++)
for(int j=0;j<topic_num;j++)
{
if(src[i]!=0)
down1 += src[i]*tmpP_t_w[i][j];
}
for(int i=0;i<topic_num;i++)
{
double top = 0;
for(int j=0;j<word_num;j++)
{
if(src[j]!=0)
{
top += src[j]*tmpP_t_w[j][i];
}
}
dst[i] = top/down1;
}
}
for(int i=0;i<word_num;i++)
{
delete[] tmpP_t_w[i];
}
delete[] tmpP_t_w;
return 0;
}
int PLSAOBJ::SaveModel(const string& filepath)
{
ofstream outfile(filepath.c_str());
outfile<<doc_num<<endl;
outfile<<topic_num<<endl;
outfile<<word_num<<endl;
for(int i=0;i<doc_num;i++)
for(int j=0;j<topic_num;j++)
{
outfile<<pt_d[i][j]<<endl;
}
for(int i=0;i<topic_num;i++)
for(int j=0;j<word_num;j++)
{
outfile<<pw_t[i][j]<<endl;
}
outfile.close();
return 0;
}
int PLSAOBJ::LoadModel(const string& filepath)
{
ifstream infile(filepath.c_str());
if(!infile)
return -1;
char str[256];
infile.getline(str,sizeof(str));
doc_num = atoi(str);
infile.getline(str,sizeof(str));
topic_num = atoi(str);
infile.getline(str,sizeof(str));
word_num = atoi(str);
for(int i=0;i<doc_num;i++)
for(int j=0;j<topic_num;j++)
{
infile.getline(str,sizeof(str));
pt_d[i][j] = atof(str);
}
for(int i=0;i<topic_num;i++)
for(int j=0;j<word_num;j++)
{
infile.getline(str,sizeof(str));
pw_t[i][j] = atof(str);
}
return 0;
}
int PLSAOBJ::GetSize(int& d_num,int& t_num,int& w_num)
{
d_num = doc_num;
t_num = topic_num;
w_num = word_num;
return 0;
}
float** PLSAOBJ::GetPT_D()
{
return pt_d;
}