-
Notifications
You must be signed in to change notification settings - Fork 3
/
WordPDPLDA.hpp
441 lines (420 loc) · 10.2 KB
/
WordPDPLDA.hpp
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
#pragma once
#include "GeneralizedStirlingNumber.hpp"
#include "ILDA.hpp"
#include"AliasWordPDPLDA.hpp"
namespace AliasLDA {
class WordPDPLDA: public ILDA {
const int METRO_HASTING_STEPS = 2;
const int ALIAS_SAMPLES_KEPT_RATIO = 8;
const bool USE_MIXED_SUPPLY_CONSUME_MECH=true;
Vector2DInt docs;
int V;
int K;
double a; //discount
double b; //concentration
double gamma; //for generating base Dirichlet
double gammasum;
Vector2DDouble phi; //dim K V. Word|topic,group distribution.
Vector2DDouble theta; //dim I D K topic|document,group distribution.
VecDouble thetasum; //dim I K theta_{I,D,K} sum over all documents
double alpha;
VecDouble alphaVec;
double alphasum;
Vector2DInt m; //dim V K. ==nwk;
VecInt msum; //dim K.
Vector2DInt t; //dim V K. multiplicity
VecInt tsum; //dim K
Vector2DInt ndk;
Vector2DInt z; //dim I _D __L. topic assignment
VecInt nw;
VecDouble bucket;
std::mt19937_64 rgen;
std::uniform_real_distribution<double> u01;
GeneralizedStirlingNumber& gsn;
Vector2DDouble gsnCache;
int gsn_maxN;
int gsn_maxM;
void initialiseGSN(){
NMPair limits=gsn.getLimit(a);
while(limits.m==-1)limits=gsn.getLimit(a);
gsn_maxN=limits.n;
gsn_maxM=limits.m;
if(limits.m<gsn_maxM||limits.n<gsn_maxN){
printf("initialising GSN Module...\n");
while(!gsn.initialize(a,gsn_maxN,gsn_maxM)){}
printf("GSN Module initialized\n");
}
double* tempCache;
gsnCache.resize(gsn_maxN);
while((tempCache=gsn.getCache(a))==NULL){}
printf("Copying over GSN to local buffer\n");
for(int i=0;i<limits.n;i++){
gsnCache[i].resize(gsn_maxM);
for(int j=0;j<std::min(limits.m,i);j++){
int ind=i * limits.m + j;
gsnCache[i][j]=tempCache[ind];
// printf("gsn %d %d val=%lf\n",i,j,gsnCache[i][j]);
}
}
}
double getGSNRatio(int n0,int m0,int n1,int m1){
assert(n0<gsn_maxN && n1<gsn_maxN);
assert(m0<gsn_maxM && m1<gsn_maxM);
assert(gsnCache[n0][m0]!=NAN && gsnCache[n1][m1]!=NAN);
// if(gsnCache[n0][m0] < gsnCache[n1][m1] ){
// printf("lS(%d,%d)=%lf, lS(%d,%d)=%lf, ratio=%lf\n",n0,m0,gsnCache[n0][m0],n1,m1,gsnCache[n1][m1],exp(gsnCache[n0][m0]-gsnCache[n1][m1]));
// }
return exp(gsnCache[n0][m0]-gsnCache[n1][m1]);
}
inline void inc_m(int w, int k) { //increment m
m[w][k]++;
msum[k]++;
}
inline void dec_m(int w, int k) { //decrement m
m[w][k]--;
msum[k]--;
}
inline void inc_t(int w, int k) {
t[w][k]++;
tsum[k]++;
}
inline void dec_t(int w, int k) {
t[w][k]--;
tsum[k]--;
}
inline void inc_ndk(int d, int k) {
ndk[d][k]++;
}
inline void dec_ndk(int d, int k) {
ndk[d][k]--;
}
inline double getphi0(int w,int k){
return (gamma+t[w][k])/(gammasum+tsum[k]);
}
public:
WordPDPLDA() :
K(0), V(0), alphasum(0), u01(0, 1), gsn(GeneralizedStirlingNumber::getInstance()) {
rgen.seed(time(NULL) + rand());
}
inline double rand01() {
return u01(rgen);
}
inline int randInt(int limit) {
return (int) (rand01() * limit);
}
inline int getndk(int d, int k) {
return ndk[d][k];
}
void initialiseWordCount() {
for (int d = 0; d < docs.size(); d++) {
for (int l = 0; l < docs[d].size(); l++) {
nw[docs[d][l]]++;
}
}
}
void initialiseRestaurantCounter() {
m.resize(V);
t.resize(V);
msum.resize(K);
tsum.resize(K);
for (int w = 0; w < V; w++) {
m[w].resize(K);
t[w].resize(K);
}
}
void initialiseDocumentTopicCounter() {
ndk.resize(docs.size());
for (int d = 0; d < docs.size(); d++) {
ndk[d].resize(K);
}
}
inline double calculate_f_flat(int w,int composite){
return calculate_f(w,composite/2,composite%2==1);
}
inline double calculate_f(int w,int k, bool r){
if(r){
// printf("calc f true\n");
return ((b+a*tsum[k])/(double)(b+msum[k]))*
((t[w][k]+1)/(double)(m[w][k]+1))*
getGSNRatio(m[w][k]+1,t[w][k]+1,m[w][k],t[w][k])*
((gamma+t[w][k])/(double)(gammasum+tsum[k]));
}else{
// printf("calc f false\n");
if(t[w][k]>0 && m[w][k]>0){
return ((m[w][k]-t[w][k]+1)/(double)(m[w][k]+1))*getGSNRatio(m[w][k]+1,t[w][k],m[w][k],t[w][k])/(double)(b+msum[k]);
}else{
return 0;
}
}
}
void initialiseAlpha() {
alphasum = 0;
if (alpha != 0) {
alphaVec.resize(K);
for (size_t i = 0; i < alphaVec.size(); i++) {
alphaVec[i] = alpha;
}
}
for (auto it = alphaVec.begin(); it != alphaVec.end(); it++) {
alphasum += *it;
}
}
void initialiseGamma(){
gammasum=gamma*V;
}
void initialiseTopicsAndCounts() {
z.resize(docs.size());
for (size_t d = 0; d < docs.size(); d++) {
VecInt& doc = docs[d];
z[d].resize(doc.size());
VecInt& zd = z[d];
for (size_t l = 0; l < doc.size(); l++) {
int w = doc[l];
int k = randInt(K);
inc_ndk(d, k);
inc_m(w, k);
zd[l] = k;
if((t[w][k]==0) || (rand01() <= 1/(double)(t[w][k]+1))){
inc_t(w,k);
}
assert(!(t[w][k]==0 && m[w][k]!=0));
}
}
}
void initialise() {
initialiseGSN();
initialiseAlpha();
initialiseGamma();
initialiseRestaurantCounter();
initialiseAssignmentOnly();
printf("initialised.");
}
virtual void initialiseAssignmentOnly(){
thetasum.resize(K);
nw.resize(V);
initialiseWordCount();
initialiseDocumentTopicCounter();
initialiseTopicsAndCounts();
bucket.resize(2*K);
}
virtual void computeTheta(){
theta.resize(docs.size());
for(size_t d=0;d<docs.size();d++){
theta[d].resize(K);
for(int k=0;k<K;k++){
theta[d][k]=(getndk(d,k)+alpha)/(docs[d].size()+alphasum);
}
}
}
virtual void computeThetaSum(){
for(int k=0;k<K;k++){
thetasum[k]=0;
}
for(size_t d=0;d<docs.size();d++){
for(int k=0;k<K;k++){
double val=(getndk(d,k)+alpha)/(docs[d].size()+alphasum);
thetasum[k]+=val/docs.size();
}
}
}
virtual void computePhi(){
phi.resize(V);
for(int w=0;w<V;w++){
phi[w].resize(K);
for(int k=0;k<K;k++){
double localsum=getphi0(w,k);
phi[w][k]=(m[w][k]-a*t[w][k])/(b+msum[k])+(a*tsum[k]+b)/(b+msum[k])*localsum;
phi[w][k]=std::max(0.0, phi[w][k]);
}
}
}
int discreteSample( VecDouble& prop, int size,
double roll) {
for (int i = 0; i < size; i++) {
if (roll <= prop[i])
return i;
else
roll -= prop[i];
}
return -1;
}
virtual void sampleWord(int d, int l) {
int w = docs[d][l];
int k = z[d][l];
assert(m[w][k]>=t[w][k]);
assert(!(t[w][k]==0 && m[w][k]!=0));
bool removeTable=(rand01()<=((double)(t[w][k])/(double)m[w][k])?true:false);
dec_m(w,k);
dec_ndk(d,k);
removeTable=removeTable&& (!(t[w][k]==1 && m[w][k]!=0));
if(removeTable){
dec_t(w,k);
}
if(t[w][k]==0 && m[w][k]!=0){
printf("error: d=%d l=%d t[%d][%d]=%d m[%d][%d]=%d\n",d,l,w,k,t[w][k],w,k,m[w][k]);
}
assert(!(t[w][k]==0 && m[w][k]!=0));
double bucket_sparse_sum = 0;
VecInt& ndkLocal = ndk[d];
for (int i = 0; i < K; i++) {
double val0 = (ndkLocal[i] +alpha) * calculate_f(w,i,false);
bucket[2*i] = val0;
bucket_sparse_sum += val0;
double val1 = (ndkLocal[i] +alpha) * calculate_f(w,i,true);
bucket[2*i+1] = val1;
bucket_sparse_sum += val1;
assert(!(t[w][i]==0 && m[w][i]!=0));
}
int newSample = discreteSample(bucket, K*2,
rand01() * bucket_sparse_sum);
if(newSample==-1){
printf(" d=%d,l=%d | oldtopic=%d newtopic=%d\n",d,l ,k, newSample);
for(int i=0;i<2*K;i++){
printf("--bucket[%d]=%lf, fval=%lf\n",i,bucket[i],calculate_f(w,i/2,i%2==1));
}
}
assert(newSample != -1);
int newTopic=newSample/2;
int headOfTable=newSample%2;
inc_m(w,newTopic);
inc_ndk(d, newTopic);
if(headOfTable==1){
inc_t(w, newTopic);
}
z[d][l] = newTopic;
}
virtual void gibbsStep() {
for (size_t d = 0; d < docs.size(); d++) {
VecInt& doc = docs[d];
for (size_t l = 0; l < doc.size(); l++) {
sampleWord(d, l);
}
}
}
virtual void setAlphaVec(const VecDouble& alphaVec) {
this->alphaVec = alphaVec;
}
virtual void setAlpha(double alpha) {
this->alpha = alpha;
}
virtual double getGamma() const {
return gamma;
}
virtual void setGamma(double gamma) {
this->gamma = gamma;
}
virtual void setDiscount(double a) {
this->a = a;
}
virtual double getDiscount() {
return a;
}
virtual void setConcentration(double b) {
this->b = b;
}
virtual double getConcentration() {
return b;
}
virtual void setNumTopics(int K) {
this->K = K;
}
virtual void setSizeVocabulary(int V) {
this->V = V;
}
virtual const Vector2DDouble& getPhi() const {
return phi;
}
virtual const Vector2DDouble& getTheta() const {
return theta;
}
virtual const VecDouble& getThetasum() const {
return thetasum;
}
virtual int getNumTopics() const {
return K;
}
virtual const Vector2DInt& getDocuments() const {
return docs;
}
virtual void clearPhi() {
phi.clear();
}
virtual void clearTheta() {
theta.clear();
}
virtual void setDocuments(Vector2DInt& docs) {
this->docs.resize(docs.size());
for (size_t i = 0; i < docs.size(); i++) {
auto& doc = this->docs[i];
auto& thisdoc = docs[i];
doc.resize(docs[i].size());
for (size_t j = 0; j < thisdoc.size(); j++) {
doc[j] = thisdoc[j];
//printf("%d ",thisdoc[j]);
}
}
//this->docs=docs;
}
virtual const VecDouble& getAlphaVec() const {
return alphaVec;
}
virtual void copyState(ILDA* _lda){
WordPDPLDA* lda=static_cast<WordPDPLDA*> (_lda);
K=lda->K;
V=lda->V;
m=lda->m;
msum=lda->msum;
alpha=lda->alpha;
alphasum=lda->alphasum;
alphaVec=lda->alphaVec;
t=lda->t;
tsum=lda->tsum;
a=lda->a;
b=lda->b;
gamma=lda->gamma;
gammasum=lda->gammasum;
gsnCache=lda->gsnCache;
gsn_maxM=lda->gsn_maxM;
gsn_maxN=lda->gsn_maxN;
}
virtual void copyState(void* _lda, std::string impl){
if(impl=="WordPDPLDA"){
WordPDPLDA* lda=static_cast<WordPDPLDA*> (_lda);
K=lda->K;
V=lda->V;
m=lda->m;
msum=lda->msum;
t=lda->t;
tsum=lda->tsum;
a=lda->a;
b=lda->b;
gamma=lda->gamma;
gammasum=lda->gammasum;
gsnCache=lda->gsnCache;
gsn_maxM=lda->gsn_maxM;
gsn_maxN=lda->gsn_maxN;
alpha=lda->alpha;
alphasum=lda->alphasum;
alphaVec=lda->alphaVec;
}else{
AliasWordPDPLDA* lda=static_cast<AliasWordPDPLDA*> (_lda);
K=lda->K;
V=lda->V;
m=lda->m;
msum=lda->msum;
t=lda->t;
tsum=lda->tsum;
a=lda->a;
b=lda->b;
gamma=lda->gamma;
gammasum=lda->gammasum;
gsnCache=lda->gsnCache;
gsn_maxM=lda->gsn_maxM;
gsn_maxN=lda->gsn_maxN;
alpha=lda->alpha;
alphasum=lda->alphasum;
alphaVec=lda->alphaVec;
}
}
};
}