-
Notifications
You must be signed in to change notification settings - Fork 1
/
NBClustering.xs
458 lines (376 loc) · 10.9 KB
/
NBClustering.xs
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#ifdef __cplusplus
extern "C" {
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#ifdef __cplusplus
}
#endif
#include "ppport.h"
#include <string>
#include <vector>
#include <map>
#include <cfloat>
#include <cmath>
typedef std::map<int, int> IntToIntMap;
typedef std::map<int, double> IntToDoubleMap;
typedef std::map<int, std::map<int, double> > Int2ToDoubleMap;
typedef std::map<std::string, int> StrToIntMap;
typedef std::map<std::string, double> StrToDoubleMap;
typedef std::vector<double> DoubleVector;
typedef std::vector<std::vector<double> > Double2DVector;
typedef std::vector<IntToDoubleMap> IDMapVector;
class NBClustering{
public:
NBClustering();
~NBClustering();
void AddInstance(const StrToDoubleMap &doc);
void AddLabeledInstance(const StrToDoubleMap &doc, const std::string &label);
void Train(const int cluster_num, int max_iteration, double epsilon, double alpha, int seed);
StrToDoubleMap Predict(const StrToDoubleMap &doc);
private:
void initializePrior(DoubleVector *prior, int num, double alpha);
void initializeParam(double *array, int num);
void normalizeParam(double *array, int num, DoubleVector *prior);
double calcLogPrior(double *cls_param, double **wrd_param, int cnum, int wnum);
int numDoc;
StrToIntMap cdict;
StrToIntMap wdict;
IDMapVector data;
IntToIntMap ccount;
Int2ToDoubleMap wcount;
DoubleVector cparam;
Double2DVector wparam;
DoubleVector cprior;
Double2DVector wprior;
};
NBClustering::NBClustering()
: cdict(), wdict(), data(), ccount(), wcount(), cparam(), wparam(), cprior(), wprior()
{
}
NBClustering::~NBClustering()
{
}
void
NBClustering::AddInstance(const StrToDoubleMap &instance)
{
IntToDoubleMap datum;
for (StrToDoubleMap::const_iterator it = instance.begin(); it != instance.end(); ++it) {
if (wdict.find(it->first) == wdict.end()) {
int wnum = wdict.size();
wdict[it->first] = wnum;
}
int w = wdict[it->first];
datum[w] = it->second;
}
data.push_back(datum);
return;
}
void
NBClustering::AddLabeledInstance(const StrToDoubleMap &instance, const std::string &label)
{
if (cdict.find(label) == cdict.end()) {
int cnum = cdict.size();
cdict[label] = cnum;
}
int c = cdict[label];
ccount[c] += 1;
for (StrToDoubleMap::const_iterator it = instance.begin(); it != instance.end(); ++it) {
if (wdict.find(it->first) == wdict.end()) {
int wnum = wdict.size();
wdict[it->first] = wnum;
}
int w = wdict[it->first];
wcount[c][w] += it->second;
}
return;
}
void
NBClustering::Train(const int cluster_num, int max_iteration, double epsilon, double alpha, int seed)
{
/*
int max_iteration = 100;
double epsilon = 1e-10;
double alpha = 1.0;
int seed = 1000;
*/
srand(seed);
int cnum = (cdict.size() < 2) ? cluster_num : cdict.size();
int wnum = wdict.size();
double *cls_param = new double[cnum];
double **wrd_param = new double*[cnum];
double *cls_exp = new double[cnum];
double **wrd_exp = new double*[cnum];
double *cls_tmp = NULL;
double **wrd_tmp = NULL;
initializeParam(cls_param, cnum);
std::fill_n(cls_exp, cnum, 0.0);
for (int c = 0; c < cnum; ++c) {
wrd_param[c] = new double[wnum];
wrd_exp[c] = new double[wnum];
initializeParam(wrd_param[c], wnum);
std::fill_n(wrd_exp[c], wnum, 0.0);
}
if (cdict.size() < 2) {
initializePrior(&cprior, cnum, alpha);
char buf[1024];
for (int c = 0; c < cluster_num; ++c) {
sprintf(buf, "%d", c);
cdict[std::string(buf)] = c;
std::vector<double> tmp;
initializePrior(&tmp, wnum, alpha);
wprior.push_back(tmp);
}
} else {
cprior.assign(cnum, alpha);
for (IntToIntMap::iterator cit = ccount.begin(); cit != ccount.end(); ++cit) {
int c = cit->first;
cprior[c] += cit->second;
std::vector<double> tmp;
tmp.assign(wnum, alpha);
for (IntToDoubleMap::iterator wit = wcount[c].begin(); wit != wcount[c].end(); ++wit) {
int w = wit->first;
tmp[w] += wit->second;
}
wprior.push_back(tmp);
}
}
bool converged = false;
int iter = 0;
double prev_logprobs = -DBL_MAX;
double curr_logprobs = -DBL_MAX;
std::vector<double> cond_prob(cnum);
while (!converged && iter <= max_iteration) {
++iter;
double logprobs = 0.0;
// Expectation step
for (int d = 0; d < data.size(); ++d) {
IntToDoubleMap datum = data[d];
double scaled_sum = 0.0;
double max_val = -DBL_MAX;
for (int c = 0; c < cnum; ++c) {
cond_prob[c] = log(cls_param[c]);
for (IntToDoubleMap::iterator it = datum.begin(); it != datum.end(); ++it) {
cond_prob[c] += it->second * log(wrd_param[c][it->first]);
}
if (cond_prob[c] > max_val) {
max_val = cond_prob[c];
}
}
for (int c = 0; c < cnum; ++c) {
cond_prob[c] = exp(cond_prob[c] - max_val);
scaled_sum += cond_prob[c];
}
logprobs += max_val + log(scaled_sum);
for (int c = 0; c < cnum; ++c) {
cond_prob[c] /= scaled_sum;
cls_exp[c] += cond_prob[c];
for (IntToDoubleMap::iterator it = datum.begin(); it != datum.end(); ++it) {
wrd_exp[c][it->first] += it->second * cond_prob[c];
}
}
}
double logprior = calcLogPrior(cls_param, wrd_param, cnum, wnum);
logprobs += logprior;
// Maximization step
normalizeParam(cls_exp, cnum, &cprior);
for (int c = 0; c < cnum; ++c) {
normalizeParam(wrd_exp[c], wnum, &(wprior[c]));
}
if (fabs(logprobs - curr_logprobs) < fabs(curr_logprobs) * epsilon) {
converged = true;
} else if (logprobs < curr_logprobs) {
fprintf(stderr, "decrese: %15.10f to %15.10f\n", curr_logprobs, logprobs);
converged = true;
} else if (data.size() == 0) {
converged = true;
}
fprintf(stderr, "%5d: %15.10f\n", iter, logprobs);
prev_logprobs = curr_logprobs;
curr_logprobs = logprobs;
cls_tmp = cls_param; cls_param = cls_exp; cls_exp = cls_tmp;
wrd_tmp = wrd_param; wrd_param = wrd_exp; wrd_exp = wrd_tmp;
for (int c = 0; c < cnum; ++c) {
std::fill_n(wrd_exp[c], wnum, 0.0);
}
std::fill_n(cls_exp, cnum, 0.0);
}
cparam.clear();
wparam.clear();
for (int c = 0; c < cnum; ++c) {
cparam.push_back(cls_param[c]);
std::vector<double> tmp_vec;
for (int w = 0; w < wnum; ++w) {
tmp_vec.push_back(wrd_param[c][w]);
}
wparam.push_back(tmp_vec);
delete[] wrd_param[c]; wrd_param[c] = NULL;
delete[] wrd_exp[c]; wrd_exp[c] = NULL;
}
delete[] wrd_param; wrd_param = NULL;
delete[] wrd_exp; wrd_exp = NULL;
return;
}
void
NBClustering::initializePrior(DoubleVector *prior, int num, double alpha)
{
(*prior).clear();
(*prior).assign(num, alpha);
}
void
NBClustering::initializeParam(double *array, int num)
{
double sum = 0.0;
for (int i = 0; i < num; ++i) {
double tmp = static_cast<double>(rand()) / RAND_MAX;
array[i] = 1.0 + tmp;
sum += array[i];
}
for (int i = 0; i < num; ++i) {
array[i] /= sum;
}
return;
}
void
NBClustering::normalizeParam(double *array, int num, DoubleVector *prior)
{
double sum = 0.0;
for (int i = 0; i < num; ++i) {
array[i] += (*prior)[i];
sum += array[i];
}
for (int i = 0; i < num; ++i) {
array[i] /= sum;
}
return;
}
double
NBClustering::calcLogPrior(double *cls_param, double **wrd_param, int cnum, int wnum)
{
double logprior = 0.0;
for (int c = 0; c < cnum; ++c) {
if (cprior[c] > 0) {
logprior += cprior[c] * log(cls_param[c]);
}
for (int w = 0; w < wnum; ++w) {
if (wprior[c][w]) {
logprior += wprior[c][w] * log(wrd_param[c][w]);
}
}
}
return logprior;
}
StrToDoubleMap
NBClustering::Predict(const StrToDoubleMap &doc)
{
StrToDoubleMap result;
double max_score = -DBL_MAX;
for (StrToIntMap::iterator cit = cdict.begin(); cit != cdict.end(); ++cit) {
std::string cluster = cit->first;
int c = cdict[cluster];
result[cluster] = log(cparam[c]);
for (StrToDoubleMap::const_iterator wit = doc.begin(); wit != doc.end(); ++wit) {
std::string word = wit->first;
double freq = wit->second;
if (wdict.find(word) == wdict.end()) {
continue;
}
int w = wdict[word];
result[cluster] += freq * log(wparam[c][w]);
}
if (result[cluster] > max_score) {
max_score = result[cluster];
}
}
double sum = 0.0;
for (StrToIntMap::iterator cit = cdict.begin(); cit != cdict.end(); ++cit) {
std::string cluster = cit->first;
result[cluster] = exp(result[cluster] - max_score);
sum += result[cluster];
}
for (StrToIntMap::iterator cit = cdict.begin(); cit != cdict.end(); ++cit) {
std::string cluster = cit->first;
result[cluster] /= sum;
}
return result;
}
MODULE = ToyBox::XS::NBClustering PACKAGE = ToyBox::XS::NBClustering
NBClustering *
NBClustering::new()
void
NBClustering::DESTROY()
void
NBClustering::xs_add_instance(attributes_input)
SV * attributes_input
CODE:
{
HV *hv_attributes = (HV*) SvRV(attributes_input);
SV *val;
char *key;
I32 retlen;
int num = hv_iterinit(hv_attributes);
StrToDoubleMap attributes;
for (int i = 0; i < num; ++i) {
val = hv_iternextsv(hv_attributes, &key, &retlen);
attributes[key] = (double)SvNV(val);
}
THIS->AddInstance(attributes);
}
void
NBClustering::xs_add_labeled_instance(attributes_input, label_input)
SV * attributes_input
char* label_input
CODE:
{
HV *hv_attributes = (HV*) SvRV(attributes_input);
SV *val;
char *key;
I32 retlen;
int num = hv_iterinit(hv_attributes);
std::string label = std::string(label_input);
StrToDoubleMap attributes;
for (int i = 0; i < num; ++i) {
val = hv_iternextsv(hv_attributes, &key, &retlen);
attributes[key] = (double)SvNV(val);
}
THIS->AddLabeledInstance(attributes, label);
}
void
NBClustering::xs_train(cluster_num, max_iteration, epsilon, alpha, seed)
int cluster_num
int max_iteration
double epsilon
double alpha
int seed
CODE:
{
THIS->Train(cluster_num, max_iteration, epsilon, alpha, seed);
}
SV*
NBClustering::xs_predict(attributes_input)
SV * attributes_input
CODE:
{
HV *hv_attributes = (HV*) SvRV(attributes_input);
SV *val;
char *key;
I32 retlen;
int num = hv_iterinit(hv_attributes);
StrToDoubleMap attributes;
StrToDoubleMap result;
for (int i = 0; i < num; ++i) {
val = hv_iternextsv(hv_attributes, &key, &retlen);
attributes[key] = (double)SvNV(val);
}
result = THIS->Predict(attributes);
HV *hv_result = newHV();
for (StrToDoubleMap::iterator it = result.begin(); it != result.end(); ++it) {
const char *const_key = (it->first).c_str();
SV* val = newSVnv(it->second);
hv_store(hv_result, const_key, strlen(const_key), val, 0);
}
RETVAL = newRV_inc((SV*) hv_result);
}
OUTPUT:
RETVAL