forked from renaud/hdp-faster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
288 lines (229 loc) · 10.1 KB
/
main.cpp
File metadata and controls
288 lines (229 loc) · 10.1 KB
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
#include <libgen.h>
#include <string.h>
#include "corpus.h"
#include "state.h"
#include "utils.h"
gsl_rng * RANDOM_NUMBER = NULL;
void print_usage_and_exit() {
// print usage information
printf("\nC++ implementation of Gibbs sampling for hierarchical Dirichlet process, a much faster version.\n");
printf("Authors: Chong Wang, chongw@cs.princeton.edu, Computer Science Department, Princeton University.\n");
printf("usage:\n");
printf(" hdp [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(" --save_lag: the saving point, default 5.\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.05.\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(" --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;
// Data parameters.
char* train_data = NULL;
// Model parameters.
double eta = 0.01;
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], "--save_lag")) save_lag = atoi(argv[++i]);
else if (!strcmp(argv[i], "--train_data")) train_data = 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 ./hdp 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);
if (test_data == NULL || model_prefix == NULL) {
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, "\nData parameters:\n");
fprintf(setting_file, "train_data: %s\n", train_data);
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);
// 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");
if(verbose) printf("%s\n", name);
fprintf(train_log, "%s\n", name);
// Start iterating.
time_t start, current;
int total_time = 0;
int iter = 0;
HDP* hdp = new HDP();
hdp->init_hdp(eta, gamma, alpha, c_train->size_vocab_);
// Setting up the hdp state.
hdp->setup_doc_states(c_train->docs_);
// first iteration
hdp->iterate_gibbs_state(false, false);
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;
sprintf(name, "%d\t%d\t%d\t\t%.5f\t%.5f\t\t%d\t\t%.3f\t%.5f",
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_);
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);
// 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);
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);
return 0;
}