-
Notifications
You must be signed in to change notification settings - Fork 4
/
hdp.cpp
358 lines (300 loc) · 7.99 KB
/
hdp.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
#include "hdp.h"
#include <assert.h>
#define VERBOSE true
#define PERMUTE true
#define PERMUTE_LAG 10
#define TABLE_SAMPLING true
#define INF -1e50
hdp::hdp()
{
m_hdp_param = NULL;
m_state = NULL;
}
hdp::~hdp()
{
m_hdp_param = NULL;
delete m_state;
m_state = NULL;
}
void hdp::free_hdp()
{
if (m_hdp_param != NULL) {
delete m_hdp_param;
}
if (m_state != NULL) {
delete m_state;
}
}
void hdp::load(char * model_path, int sampler_num)
{
free_hdp();
m_state = new hdp_state();
m_state->load_state_ex(model_path, sampler_num);
}
void hdp::save(char * model_path, int sample_num)
{
m_state->save_state_ex(model_path, sample_num);
}
void hdp::setup_state(const hdp_hyperparameter _hdp_param)
{
if (m_hdp_param != NULL) {
delete m_hdp_param;
m_hdp_param = NULL;
}
m_hdp_param = new hdp_hyperparameter();
m_hdp_param->copy_parameters(_hdp_param);
}
void hdp::setup_state(const corpus * c,
double _eta, double _gamma, double _alpha,
const hdp_hyperparameter _hdp_param,
int vocab_size)
{
free_hdp();
m_hdp_param = new hdp_hyperparameter();
m_hdp_param->copy_parameters(_hdp_param);
m_state = new hdp_state();
m_state->set_vocab_size(vocab_size);
m_state->setup_state_from_corpus(c);
m_state->allocate_initial_space();
m_state->m_eta = _eta;
/// set the pointer for the hyper parameters
assert(m_hdp_param != NULL);
/// use the means of gamma distribution
if (_gamma <= 0) {
m_state->m_gamma = m_hdp_param->m_gamma_a * m_hdp_param->m_gamma_b;
} else {
m_state->m_gamma = _gamma;
}
if (_alpha <= 0) {
m_state->m_alpha = m_hdp_param->m_alpha_a * m_hdp_param->m_alpha_b;
} else {
m_state->m_alpha = _alpha;
}
}
void hdp::setup_doc_info_update(const document * doc)
{
m_state->setup_doc_info_from_document(doc);
m_state->allocate_space_for_online_updates();
}
int hdp::get_size_vocab() const
{
if (m_state != NULL) {
return m_state->m_size_vocab;
} else {
return -1;
}
}
double hdp::get_eta() const
{
if (m_state != NULL) {
return m_state->m_eta;
} else {
return -1;
}
}
int hdp::get_topics_number() const
{
if (m_state != NULL) {
return m_state->m_num_topics;
} else {
return -1;
}
}
int hdp::get_word_topic_counts(int k) const
{
if (m_state != NULL) {
return m_state->m_word_counts_by_z[k];
} else {
return -1;
}
}
int hdp::get_word_topic_counts(int k, int w) const
{
if (m_state != NULL) {
return m_state->m_word_counts_by_zw[k][w];
} else {
return -1;
}
}
void hdp::run()
{
m_state->iterate_gibbs_state(false, PERMUTE, m_hdp_param, TABLE_SAMPLING); //init the state
printf("starting with %d topics \n", m_state->m_num_topics);
/// time part ...
time_t start, current;
time (&start);
double dif;
for (int iter = 0; iter < m_hdp_param->m_max_iter; iter++)
{
printf("iter = %05d, ", iter);
bool permute = PERMUTE && (iter > 0) && (iter % PERMUTE_LAG == 0);
m_state->iterate_gibbs_state(true, permute, m_hdp_param, TABLE_SAMPLING);
time(¤t); dif = difftime (current,start);
printf("#topics = %04d, time = %8.2f\n", m_state->m_num_topics, dif);
}
}
void hdp::run_online()
{
m_state->iterate_gibbs_state_online(false, m_hdp_param, TABLE_SAMPLING); //init the state
for (int iter = 0; iter < m_hdp_param->m_max_iter; iter++)
{
m_state->iterate_gibbs_state_online(true, m_hdp_param, TABLE_SAMPLING);
}
}
void hdp::free_online_info()
{
m_state->free_online_info();
}
hdp_dynamic::hdp_dynamic()
{
m_hdp_param = NULL;
m_state = NULL;
}
hdp_dynamic::~hdp_dynamic()
{
if (m_hdp_param != NULL) {
delete m_hdp_param;
}
m_hdp_param = NULL;
if (m_state != NULL) {
delete m_state;
}
m_state = NULL;
}
void hdp_dynamic::run()
{
m_state->iterate_gibbs_state(false, m_hdp_param, TABLE_SAMPLING); //init the state
printf("starting with %d topics \n", m_state->m_num_topics);
/// time part ...
time_t start, current;
time (&start);
double dif;
///
for (int iter = 0; iter < m_hdp_param->m_max_iter; iter++)
{
printf("iter = %05d, ", iter);
m_state->iterate_gibbs_state(true, m_hdp_param, TABLE_SAMPLING);
time(¤t); dif = difftime (current,start);
printf("#topics = %04d, time = %8.2f\n",
m_state->m_num_topics, dif);
}
}
void hdp_dynamic::run_online()
{
m_state->iterate_gibbs_state_online(false, m_hdp_param, TABLE_SAMPLING); //init the state
for (int iter = 0; iter < m_hdp_param->m_max_iter; iter++)
{
m_state->iterate_gibbs_state_online(true, m_hdp_param, TABLE_SAMPLING);
}
}
void hdp_dynamic::setup_state(const corpus *c, double _eta, double _gamma, double _alpha, double _delta,
const hdp_hyperparameter _hdp_param, int vocab_size)
{
free_hdp();
m_hdp_param = new hdp_hyperparameter();
m_hdp_param->copy_parameters(_hdp_param);
m_state = new hdp_state_dynamic();
m_state->set_vocab_size(vocab_size);
m_state->setup_state_from_corpus(c);
m_state->allocate_initial_space();
m_state->m_eta = _eta;
m_state->m_delta = _delta;
/// set the pointer for the hyper parameters
assert(m_hdp_param != NULL);
/// use the means of gamma distribution
if (_gamma <= 0) {
m_state->m_gamma = m_hdp_param->m_gamma_a * m_hdp_param->m_gamma_b;
} else {
m_state->m_gamma = _gamma;
}
if (_alpha <= 0) {
m_state->m_alpha = m_hdp_param->m_alpha_a * m_hdp_param->m_alpha_b;
} else {
m_state->m_alpha = _alpha;
}
}
void hdp_dynamic::setup_state(const hdp_hyperparameter _hdp_param)
{
if (m_hdp_param != NULL) {
delete m_hdp_param;
m_hdp_param = NULL;
}
m_hdp_param = new hdp_hyperparameter();
m_hdp_param->copy_parameters(_hdp_param);
}
void hdp_dynamic::setup_doc_info_update(const document * doc)
{
m_state->setup_doc_info_from_document(doc);
m_state->allocate_space_for_online_updates();
}
int hdp_dynamic::get_size_vocab() const
{
if (m_state != NULL) {
return m_state->m_size_vocab;
} else {
return -1;
}
}
double hdp_dynamic::get_eta() const
{
if (m_state != NULL) {
return m_state->m_eta;
} else {
return -1;
}
}
int hdp_dynamic::get_topics_number() const
{
if (m_state != NULL) {
return m_state->m_num_topics;
} else {
return -1;
}
}
int hdp_dynamic::get_word_topic_counts(int k) const
{
if (m_state != NULL) {
return m_state->m_word_counts_by_z[k];
} else {
return -1;
}
}
int hdp_dynamic::get_word_topic_counts(int k, int w) const
{
if (m_state != NULL) {
return m_state->m_word_counts_by_zw[k][w];
} else {
return -1;
}
}
void hdp_dynamic::load(char * model_path, int sampler_num)
{
free_hdp();
m_state = new hdp_state_dynamic();
m_state->load_state_ex(model_path, sampler_num);
}
void hdp_dynamic::save(char * model_path, int sample_num)
{
m_state->save_state_ex(model_path, sample_num);
}
void hdp_dynamic::free_doc_info()
{
m_state->free_doc_info();
}
void hdp_dynamic::update_state_after_online_update()
{
m_state->update_state_after_online_update();
}
void hdp_dynamic::free_hdp()
{
if (m_hdp_param != NULL) {
delete m_hdp_param;
}
if (m_state != NULL) {
delete m_state;
}
}
void hdp_dynamic::save_last_document_info_for_online_update()
{
m_state->save_last_document_info_for_online_update();
}