-
Notifications
You must be signed in to change notification settings - Fork 84
/
gLDA_serial.cpp
382 lines (347 loc) · 9.29 KB
/
gLDA_serial.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
379
380
381
382
/**
* Copyright (c) 2014, Douban Inc.
* All rights reserved.
*
* Distributed under the BSD License. Check out the LICENSE file for full text.
*
* Paracel - A distributed optimization framework with parameter server.
*
* Downloading
* git clone https://github.com/douban/paracel.git
*
* Authors: Ping Qin <qinping@douban.com>
*
*/
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<string>
#include<vector>
#include<unordered_map>
#include<algorithm>
#include <google/gflags.h>
#include "ps.hpp"
#include "utils.hpp"
namespace paracel {
namespace tool {
class LDAmodel{
public:
LDAmodel(Comm comm,
std::string input,
std::string output,
double alpha,
double beta,
int k_topics,
int iters,
int top_words) : input(input),
output(output),
alpha(alpha),
beta(beta),
K(k_topics),
max_iter(iters),
top(top_words) {
pt = new paralg(comm);
load(input);
printf("Total docs:%d, dicts:%d, words:%d\n", M, V, T);
top = std::min(top, V);
printf("Top %d\n", top);
Kalpha = alpha * K;
Vbeta = beta * V;
para_init();
}
~LDAmodel(){
if(pt) delete pt;
if(k_prob) delete[] k_prob;
if(sum_doc2topic) delete[] sum_doc2topic;
if(sum_topic2word) delete[] sum_topic2word;
if(doc2topic) {
for(int i = 0; i < M; i++) {
if(doc2topic[i]) delete[] doc2topic[i];
}
delete[] doc2topic;
}
if(topic2word) {
for(int i = 0; i < K; i++) {
if(topic2word[i]) delete[] topic2word[i];
}
delete[] topic2word;
}
if(z_index) {
for(int i = 0; i < M; i++) {
if(z_index[i]) delete[] z_index[i];
}
delete[] z_index;
}
if(theta) {
for(int i = 0; i < M; i++) {
if(theta[i]) delete[] theta[i];
}
delete[] theta;
}
if(phi) {
for(int i = 0; i < K; i++) {
if(phi[i]) delete[] phi[i];
}
delete[] phi;
}
}
void train() {
for(int i = 0; i < max_iter; i++) {
single_iter();
printf("ITER:%d, LOGLIKELIHOOD:%f, PERPLEXITY:%f\n",
i + 1,
likelihood(),
exp(-1 * likelihood() / T));
}
}
void save(){
theta_phi();
FILE* f = fopen(output.c_str(), "w");
auto sort_comp = [] (std::pair<int, double> a,
std::pair<int, double> b) {
return a.second > b.second;
};
for(int i = 0; i < K; i++) {
std::vector<std::pair<int, double> > words_probs;
std::pair<int, double> word_prob;
for (int j = 0; j < V; j++) {
word_prob.first = j;
word_prob.second = phi[i][j];
words_probs.push_back(word_prob);
}
std::sort(words_probs.begin(), words_probs.end(), sort_comp);
fprintf(f, "%dtopic\t", i);
for(int j = 0; j < top; j ++) {
auto word_id = words_probs[j].first;
auto word = id2word[word_id];
fprintf(f, "%s:%f|", word.c_str(), words_probs[j].second);
}
fprintf(f, "\n");
}
fclose(f);
}
private:
void load(std::string file_name) {
word2id.clear();
id2word.clear();
std::string sep(" \t\r\n");
std::vector<std::string> res;
auto parser = [&] (const std::string line){
std::vector<int> tmp;
if(split(line, sep, res)) {
for(auto it = res.begin(); it != res.end(); it++) {
if(!word2id.count(*it)) {
word2id[*it] = V;
id2word[V] = *it;
V++;
}
tmp.push_back(word2id[*it]);
T++;
}
}
docs.push_back(tmp);
M++;
};
pt->paracel_load_handle(file_name, parser);
}
bool split(const std::string & src,
std::string sep,
std::vector<std::string>& res) {
int N = src.size();
if (N == 0 || sep.size() == 0) {
return false;
}
res.clear();
int start = src.find_first_not_of(sep);
int stop = 0;
while(start >= 0 && start < N) {
stop = src.find_first_of(sep, start);
if(stop < 0 || stop > N) {
stop = N;
}
res.push_back(src.substr(start, stop - start));
start = src.find_first_not_of(sep, stop + 1);
}
return true;
}
void para_init() {
k_prob = new double[K];
for(int i = 0; i < K; i++) k_prob[i] = 1.0 / K;
doc2topic = new int*[M];
for(int i = 0; i < M; i++) {
doc2topic[i] = new int[K];
for(int j = 0; j < K; j++) {
doc2topic[i][j] = 0;
}
}
topic2word = new int*[K];
for(int i = 0; i < K; i++) {
topic2word[i] = new int[V];
for(int j = 0; j < V; j++) {
topic2word[i][j] = 0;
}
}
sum_doc2topic = new int[M];
for(int i = 0; i < M; i++) sum_doc2topic[i] = 0;
sum_topic2word = new int[K];
for(int i = 0; i < K; i++) sum_topic2word[i] = 0;
srand(time(NULL));
z_index = new int*[M];
for(int doc_id = 0; doc_id < M; doc_id++) {
auto tmp_doc = docs[doc_id];
int N = tmp_doc.size();
z_index[doc_id] = new int[N];
for(int word_index = 0; word_index < N; word_index++) {
int topic_id = (int) (ran_uniform() * K);
z_index[doc_id][word_index] = topic_id;
int word_id = tmp_doc[word_index];
add(doc_id, word_id, topic_id);
}
}
theta = new double*[M];
for(int i = 0; i < M; i++) theta[i] = new double[K];
phi = new double*[K];
for(int i = 0; i < K; i++) phi[i] = new double[V];
}
void show_info() {
for(int doc_id = 0; doc_id < M; doc_id++) {
auto tmp_doc = docs[doc_id];
int N = tmp_doc.size();
for(int word_index = 0; word_index < N; word_index++) {
printf("Z:%d ", z_index[doc_id][word_index]);
}
printf("\n");
}
for(int i = 0; i < M; i++) printf("sumDOC:%d\n", sum_doc2topic[i]);
for(int i = 0; i < K; i++) printf("sumTOP:%d\n", sum_topic2word[i]);
for(int doc_id = 0; doc_id < M; doc_id++) {
for(int i = 0; i < K; i++) {
printf("d2t:%d ", doc2topic[doc_id][i]);
}
printf("\n");
}
for(int i = 0; i < K; i++) {
for(int j = 0; j < V; j++) {
printf("t2w:%d ", topic2word[i][j]);
}
printf("\n");
}
}
void add(int doc_id, int word_id, int topic_id, int num = 1) {
doc2topic[doc_id][topic_id] += num;
sum_doc2topic[doc_id] += num;
topic2word[topic_id][word_id] += num;
sum_topic2word[topic_id] += num;
}
void remove(int doc_id, int word_id, int topic_id) {
add(doc_id, word_id, topic_id, -1);
}
double ran_uniform() {
return rand() / (RAND_MAX + 1.0);
}
int ran_multinomial(double* prob, int k) {
int i;
for(i = 1; i < k; i++) {
prob[i] += prob[i - 1];
}
double p = ran_uniform() * prob[k - 1];
for(i = 0; i < k; i++) {
if(prob[i] > p) break;
}
return i;
}
void single_iter() {
for(int doc_id = 0; doc_id < M; doc_id++) {
auto tmp_doc = docs[doc_id];
int N = tmp_doc.size();
for(int word_index = 0; word_index < N; word_index++) {
int topic_id = z_index[doc_id][word_index];
int word_id = tmp_doc[word_index];
remove(doc_id, word_id, topic_id);
for(int k = 0; k < K; k++) {
k_prob[k] = (doc2topic[doc_id][k] + alpha) / (sum_doc2topic[doc_id] + Kalpha) *
(topic2word[k][word_id] + beta) / (sum_topic2word[k] + Vbeta);
}
topic_id = ran_multinomial(k_prob, K);
z_index[doc_id][word_index] = topic_id;
add(doc_id, word_id, topic_id);
}
}
}
double likelihood() {
theta_phi();
double sum = 0.0;
for(int doc_id = 0; doc_id < M; doc_id++) {
auto tmp_doc = docs[doc_id];
int N = tmp_doc.size();
double tmp_log = 0.00001;
for(int word_index = 0; word_index < N; word_index++) {
int word_id = tmp_doc[word_index];
for(int k = 0; k < K; k++) {
tmp_log += theta[doc_id][k] * phi[k][word_id];
}
sum += log(tmp_log);
}
}
return sum;
}
void theta_phi(){
for(int i = 0; i < M; i++) {
for(int j = 0 ; j < K; j++) {
theta[i][j] = (doc2topic[i][j] + alpha) / (sum_doc2topic[i] + Kalpha);
}
}
for(int i = 0; i < K; i++) {
for(int j = 0 ; j < V; j++) {
phi[i][j] = (topic2word[i][j] + beta) / (sum_topic2word[i] + Vbeta);
}
}
}
private:
std::string input, output;
double alpha, beta, Kalpha, Vbeta;
int K, max_iter, top;
int M = 0, V = 0, T = 0;
paralg* pt;
std::vector<std::vector<int>> docs;
std::unordered_map<std::string, int> word2id;
std::unordered_map<int, std::string> id2word;
double* k_prob;
int** doc2topic;
int** topic2word;
int** z_index;
int* sum_doc2topic;
int* sum_topic2word;
double** theta;
double** phi;
}; // class LDA
} // namespace tool
} // namespace paracel
DEFINE_string(cfg_file, "", "config json file with absolute path.\n");
int main(int argc, char *argv[])
{
paracel::main_env comm_main_env(argc, argv);
paracel::Comm comm(MPI_COMM_WORLD);
google::SetUsageMessage("[options]\n\t--cfg_file\n");
google::ParseCommandLineFlags(&argc, &argv, true);
paracel::json_parser pt(FLAGS_cfg_file);
std::string input = pt.parse<std::string>("input");
std::string output = pt.parse<std::string>("output");
double alpha = pt.parse<double>("alpha");
double beta = pt.parse<double>("beta");
int k_topics = pt.parse<int>("k_topics");
int iters = pt.parse<int>("iters");
int top_words = pt.parse<int>("top_words");
paracel::tool::LDAmodel solver(comm,
input,
output,
alpha,
beta,
k_topics,
iters,
top_words);
solver.train();
solver.save();
return 0;
}