forked from acacar/shdp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
350 lines (289 loc) · 12.8 KB
/
main.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
#include <libgen.h>
#include <string.h>
#include "corpus.hpp"
#include "state.hpp"
#include "utils.hpp"
gsl_rng * RANDOM_NUMBER = NULL;
gsl_rng * RANDOM_NUMBER_ALT = NULL;
void print_usage_and_exit() {
// print usage information
printf("\nC++ implementation of Gibbs sampling for the Kernel Smoothed Hierarchical Dirichlet Process\n");
printf("Author: Aybar C. Acar, [email protected], Grad. School of Informatics, Middle East Technical University.\n\n");
printf("Forked from: https://github.com/Blei-Lab/hdp \n");
printf("Original author: Chong Wang, [email protected], Computer Science Department, Princeton University.\n\n");
printf("usage:\n");
printf(" shdp [options]\n");
printf(" --help: print help information.\n");
printf(" --verbose: print running information.\n");
printf("\n");
printf(" control parameters:\n");
printf(" --directory: the saving directory, required.\n");
printf(" --random_seed: the random seed, default from the current time.\n");
printf(" --max_iter: the max number of iterations, default 100 (-1 means infinite).\n");
printf(" --max_time: the max time allowed (in seconds), default 1800 (-1 means infinite).\n");
printf(" --burn_in: number of burn-in iterations, default 50.\n\t\t\t\tSmart iteration and best likelihood will not work during the burn-in period.\n");
printf(" --smart_iter: Will terminate if best likelihood has not improved in smart_iter iterations after burn-in.\n \t\t\t\t(-1 means disabled, smart_iter will override max_iter).\n");
printf("\n");
printf(" data parameters:\n");
printf(" --train_data: the training data file/pattern, in lda-c format.\n");
printf("\n");
printf(" model parameters:\n");
printf(" --eta: the topic Dirichlet parameter, default 0.5.\n");
printf(" --gamma: the first-level concentration parameter in hdp, default 1.0.\n");
printf(" --alpha: the second-level concentration parameter in hdp, default 1.0.\n");
printf(" %c[4m--rho_matrix: the smoothing matrix, in space delimited format. %c[0m\n\t\t\t\tA matrix of ones will be used if not given\n",0x1B,0x1B );
printf(" --gamma_a: shape for 1st-level concentration parameter, default 1.0.\n");
printf(" --gamma_b: scale for 1st-level concentration parameter, default 1.0.\n");
printf(" --alpha_a: shape for 2nd-level concentration parameter, default 1.0.\n");
printf(" --alpha_b: scale for 2nd-level concentration parameter, default 1.0.\n");
printf(" --sample_hyper: sample 1st and 2nd-level concentration parameter, default false\n");
printf("\n");
printf(" test only parameters:\n");
printf(" --test_data: the test data file/pattern, in lda-c format.\n");
printf(" --model_prefix: the model_prefix.\n");
printf("*******************************************************************************************************\n");
exit(0);
}
int main(int argc, char* argv[]) {
if (argc < 2) print_usage_and_exit();
int verbose = 0;
// Control parameters.
char* directory = NULL;
time_t t; time(&t);
long random_seed = (long) t;
int max_iter = 100;
int max_time = 1800;
int save_lag = 5;
int burn_in = 50;
int smart_iter = -1;
// Data parameters.
char* train_data = NULL;
char* rhomatrix_fn = NULL;
// Model parameters.
double eta = 0.5;
double gamma = 1.0;
double alpha = 1.0;
double gamma_a = 1.0;
double gamma_b = 1.0;
double alpha_a = 1.0;
double alpha_b = 1.0;
int sample_hyper = 0;
// test only parameters
char* test_data = NULL;
char* model_prefix = NULL;
for (int i = 1; i < argc; ++i) {
if (!strcmp(argv[i], "--help")) print_usage_and_exit();
else if (!strcmp(argv[i], "--verbose")) verbose = 1;
else if (!strcmp(argv[i], "--directory")) directory = argv[++i];
else if (!strcmp(argv[i], "--random_seed")) random_seed = atoi(argv[++i]);
else if (!strcmp(argv[i], "--max_iter")) max_iter = atoi(argv[++i]);
else if (!strcmp(argv[i], "--max_time")) max_time = atoi(argv[++i]);
else if (!strcmp(argv[i], "--burn_in")) burn_in = atoi(argv[++i]);
else if (!strcmp(argv[i], "--smart_iter")) smart_iter = atoi(argv[++i]);
else if (!strcmp(argv[i], "--save_lag")) save_lag = atoi(argv[++i]);
else if (!strcmp(argv[i], "--train_data")) train_data = argv[++i];
else if (!strcmp(argv[i], "--rho_matrix")) rhomatrix_fn = argv[++i];
else if (!strcmp(argv[i], "--eta")) eta = atof(argv[++i]);
else if (!strcmp(argv[i], "--gamma")) gamma = atof(argv[++i]);
else if (!strcmp(argv[i], "--alpha")) alpha = atof(argv[++i]);
else if (!strcmp(argv[i], "--gamma_a")) gamma_a = atof(argv[++i]);
else if (!strcmp(argv[i], "--gamma_b")) gamma_b = atof(argv[++i]);
else if (!strcmp(argv[i], "--gamma_a")) gamma_a = atof(argv[++i]);
else if (!strcmp(argv[i], "--gamma_b")) gamma_b = atof(argv[++i]);
else if (!strcmp(argv[i], "--sample_hyper")) sample_hyper = 1;
else if (!strcmp(argv[i], "--test_data")) test_data = argv[++i];
else if (!strcmp(argv[i], "--model_prefix")) model_prefix = argv[++i];
else {
printf("%s, unknown parameters, exit\n", argv[i]);
print_usage_and_exit();
}
}
/// print information
printf("************************************************************************************************\n");
if (directory == NULL) {
printf("Following information is missing: --directory\n");
printf("Run ./shdp for help.\n");
exit(0);
}
if (!dir_exists(directory)) make_directory(directory);
printf("Working directory: %s.\n", directory);
char name[500];
// Init random numbe generator.
RANDOM_NUMBER = new_random_number_generator(random_seed);
RANDOM_NUMBER_ALT = new_random_number_generator(random_seed+1);
if (test_data == NULL || model_prefix == NULL) {
if (train_data == NULL) {
printf("Following information is missing: --train_data\n");
printf("Run ./shdp for help.\n");
exit(0);
}
if (smart_iter != -1)
max_iter = -1;
sprintf(name, "%s/settings.dat", directory);
printf("Setting saved at %s.\n", name);
FILE* setting_file = fopen(name, "w");
fprintf(setting_file, "Control parameters:\n");
fprintf(setting_file, "directory: %s\n", directory);
fprintf(setting_file, "random_seed: %d\n", (int)random_seed);
fprintf(setting_file, "save_lag: %d\n", save_lag);
fprintf(setting_file, "max_iter: %d\n", max_iter);
fprintf(setting_file, "max_time: %d\n", max_time);
fprintf(setting_file, "burn_in: %d\n", burn_in);
fprintf(setting_file, "smart_iter: %d\n", smart_iter);
fprintf(setting_file, "\nData parameters:\n");
fprintf(setting_file, "train_data: %s\n", train_data);
fprintf(setting_file, "rho_matrix: %s\n", rhomatrix_fn);
fprintf(setting_file, "\nModel parameters:\n");
fprintf(setting_file, "eta: %.4lf\n", eta);
fprintf(setting_file, "gamma: %.4lf\n", gamma);
fprintf(setting_file, "alpha: %.4lf\n", alpha);
fprintf(setting_file, "gamma_a: %.2lf\n", gamma_a);
fprintf(setting_file, "gamma_b: %.4lf\n", gamma_b);
fprintf(setting_file, "gamma_a: %.2lf\n", alpha_a);
fprintf(setting_file, "gamma_b: %.4lf\n", alpha_b);
fprintf(setting_file, "sample_hyper: %d\n", sample_hyper);
fclose(setting_file);
Corpus* c_train = NULL;
printf("Reading training data from %s.\n", train_data);
// Reading one of the train data.
c_train = new Corpus();
c_train->read_data(train_data);
if (rhomatrix_fn == NULL)
printf("Not rho matrix file given. Using unit rho matrix.\n");
else
printf("Reading rho matrix from %s.\n", rhomatrix_fn);
// Start iterating.
time_t start, current;
int total_time = 0;
int iter = 0;
if (smart_iter != -1)
{
max_iter = burn_in + smart_iter;
}
HDP* hdp = new HDP();
hdp->init_hdp(eta, gamma, alpha, c_train->size_vocab_, rhomatrix_fn);
// Setting up the hdp state.
hdp->setup_doc_states(c_train->docs_);
// first iteration
hdp->iterate_gibbs_state(false, false);
// Open the log file for training data.
sprintf(name, "%s/train.log", directory);
FILE* train_log = fopen(name, "w");
// Heldout columns record the documents that have not seen before.
sprintf(name, "time\titer\tnum.topics\tgamma\talpha\t\tword.count\tlikelihood\tavg.likelihood\tbest.so.far");
if(verbose) printf("%s\n", name);
fprintf(train_log, "%s\n", name);
double best_likelihood = -INFINITY;
char best_so_far = ' ';
while ((max_iter == -1 || iter < max_iter) && (max_time == -1 || total_time < max_time)) {
++iter;
time (&start);
// Iterations.
hdp->iterate_gibbs_state(true, true);
// Scoring the documents.
double likelihood = hdp->log_likelihood(NULL);
hdp->compact_hdp_state();
if (sample_hyper) hdp->hyper_inference(gamma_a, gamma_b, alpha_a, alpha_b);
// Record the time.
time(¤t);
int elapse = (int) difftime(current, start);
total_time += elapse;
best_so_far = ' ';
if (iter > burn_in)
{
if (best_likelihood < likelihood)
{
best_likelihood = likelihood;
sprintf(name, "%s/best", directory);
hdp->save_state(name);
hdp->save_doc_states(name);
if (smart_iter != -1 )
max_iter = iter + smart_iter;
best_so_far = '*';
}
}
sprintf(name, "%d\t%d\t%d\t\t%.5f\t%.5f\t\t%d\t\t%.3f\t%.5f\t%c",
total_time, iter, hdp->hdp_state_->num_topics_, hdp->hdp_state_->gamma_,
hdp->hdp_state_->alpha_, c_train->num_total_words_, likelihood, likelihood/c_train->num_total_words_, best_so_far);
if (verbose) printf("%s\n", name);
fprintf(train_log, "%s\n", name);
fflush(train_log);
if (save_lag > 0 && (iter % save_lag == 0)) {
sprintf(name, "%s/iter@%05d", directory, iter);
hdp->save_state(name);
}
}
sprintf(name, "%s/final", directory);
hdp->save_state(name);
hdp->save_doc_states(name);
// Free training data.
if (c_train != NULL) {
delete c_train;
}
fclose(train_log);
delete hdp;
}
if (test_data != NULL && model_prefix != NULL) {
Corpus* c_test = new Corpus();
c_test->read_data(test_data);
if (rhomatrix_fn == NULL)
{
printf("Not rho matrix file given. Using unit rho matrix.\n");
//rho->create_unit_matrix(c_test->size_vocab_);
}
else
{
printf("Reading rho matrix from %s.\n", rhomatrix_fn);
//rho->read_from_file(rhomatrix_fn);
}
//if (!rho->check(c_test->size_vocab_))
//exit(0);
HDP* hdp = new HDP();
printf("Loading model from prefix %s...\n", model_prefix);
hdp->load_state(model_prefix);
// Remember the old state.
HDPState* old_hdp_state = new HDPState();
old_hdp_state->copy_hdp_state(*hdp->hdp_state_);
hdp->setup_doc_states(c_test->docs_);
if (verbose) printf("Initialization ...\n");
hdp->iterate_gibbs_state(false, false);
sprintf(name, "%s/%s-test.log", directory, basename(model_prefix));
FILE* test_log = fopen(name, "w");
sprintf(name, "time\titer\tnum.topics\tword.count\tlikelihood\tavg.likelihood");
if(verbose) printf("%s\n", name);
fprintf(test_log, "%s\n", name);
time_t start, current;
int total_time = 0;
int iter = 0;
// Iterations.
while ((max_iter == -1 || iter < max_iter) && (max_time == -1 || total_time < max_time)) {
++iter;
time (&start);
hdp->iterate_gibbs_state(true, true);
double likelihood = hdp->log_likelihood(old_hdp_state);
hdp->compact_hdp_state();
time(¤t);
int elapse = (int) difftime(current, start);
total_time += elapse;
sprintf(name, "%d\t%d\t%d\t\t%d\t\t%.3f\t%.5f",
total_time, iter, hdp->hdp_state_->num_topics_,
c_test->num_total_words_, likelihood,
likelihood/c_test->num_total_words_);
if (verbose) printf("%s\n", name);
fprintf(test_log, "%s\n", name);
fflush(test_log);
}
if (verbose) printf("Done and saving ...\n");
sprintf(name, "%s/%s-test", directory, basename(model_prefix));
hdp->save_state(name);
hdp->save_doc_states(name);
fclose(test_log);
delete hdp;
delete old_hdp_state;
delete c_test;
}
// Free random number generator.
free_random_number_generator(RANDOM_NUMBER);
free_random_number_generator(RANDOM_NUMBER_ALT);
return 0;
}