-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLDA.hpp
324 lines (309 loc) · 6.47 KB
/
LDA.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
/*
* LDA.h
*
* Created on: 17/12/2013
* Author: polymorpher
*/
#pragma once
#include"ILDA.hpp"
#include"util.hpp"
#include"SparseLDA_Speed.hpp"
//#include"AliasLDA_UltraSpeed.hpp"
namespace AliasLDA {
class LDA: public ILDA {
VecInt nk;
Vector2DInt docs;
Vector2DInt z; //Topic assignment
//VecInt nkw;//Awesome SparseLDA special data structure
Vector2DInt nwk;
Vector2DInt ndk;
//Uniform Prior
double beta;
double alpha;
//Non-uniform Prior
//VecDouble betaVec;
VecDouble alphaVec;
//Common between unifrom and non-unifrom prior
double betasum;
double alphasum;
int K; //numTopics
int V; //sizeVocabulary
std::mt19937_64 rgen;
std::uniform_real_distribution<double> u01;
//results
// Vector2DDouble phi;
// Vector2DDouble theta;
Vector2DDouble phi;
Vector2DDouble theta;
VecDouble thetasum;
//temp buffer
VecDouble topicProbabilities;
public:
LDA():K(0),V(0),beta(0.1)/*,alpha(0.1)*/,betasum(0),alphasum(0),u01(0,1){
rgen.seed(time(NULL));
}
inline double rand01(){
return u01(rgen);
}
inline int randInt(int limit){
return (int)(rand01()*limit);
}
inline void inc_ndk(int d,int k){
ndk[d][k]++;
//nd[d]++;
}
inline void dec_ndk(int d,int k){
ndk[d][k]--;
//nd[d]--;
}
inline void inc_nwk(int w,int k){
nwk[w][k]++;
nk[k]++;
}
inline void dec_nwk(int w,int k){
nwk[w][k]--;
nk[k]--;
}
void initialise(){
initialiseBeta();
initialiseAlpha();
vecResize2D(nwk,V,K);
initialiseAssignmentOnly();
}
virtual void initialiseAssignmentOnly(){
vecResize2D(ndk,docs.size(),K);
thetasum.resize(K);
initialiseTopicsAndCounts();
topicProbabilities.resize(K);
}
void initialiseTopicsAndCounts(){
z.resize(docs.size());
nk.resize(K);
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_nwk(w,k);
zd[l]=k;
}
}
}
void initialiseBeta(){
betasum=beta*V;
}
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;
}
}
virtual void sampleWord(int d,int l){
int w=docs[d][l];
int k=z[d][l];
dec_ndk(d,k);
dec_nwk(w,k);
double sump=0;
for(int i=0;i<K;i++){
double prob=(ndk[d][i]+alpha)*(beta+nwk[w][i])/(betasum+nk[i]);
topicProbabilities[i]=prob;
//printf("k=%d prob=%lf\n",i,topicProbabilities[i]);
sump+=prob;
}
double r=rand01()*sump;
int newTopic=-1;
for(int i=0;i<K;i++){
double curprob=topicProbabilities[i];
if(curprob>=r){
newTopic=i;
break;
}else{
r-=curprob;
}
}
if(abs(r)<=0.0001 && newTopic==-1){
newTopic=K-1;
}
assert(newTopic!=-1);
inc_ndk(d,newTopic);
inc_nwk(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 computePhi(){
phi.resize(V);
for(int w=0;w<V;w++){
phi[w].resize(K);
for(int k=0;k<K;k++){
phi[w][k]=(nwk[w][k]+beta)/(nk[k]+betasum);
}
}
}
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]=(ndk[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 thetaval=(ndk[d][k]+alpha)/(docs[d].size()+alphasum);
thetasum[k]+=thetaval/docs.size();
}
}
}
//getters and setters
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;
}
// double getAlpha() const {
// return alpha;
// }
//
// void setAlpha(double alpha) {
// this->alpha = alpha;
// }
virtual const VecDouble& getAlphaVec() const {
return alphaVec;
}
virtual void setAlphaVec(const VecDouble& alphaVec) {
this->alphaVec = alphaVec;
}
virtual void setAlpha(double alpha){
this->alpha = alpha;
}
virtual double getBeta() const {
return beta;
}
virtual void setBeta(double beta) {
this->beta = beta;
}
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 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;
}
// const VecDouble& getBetaVec() const {
// return betaVec;
// }
//
// void setBetaVec(const VecDouble& betaVec) {
// this->betaVec = betaVec;
// }
virtual void clearPhi(){
phi.clear();
}
virtual void clearTheta(){
theta.clear();
}
virtual void copyState(ILDA* _lda){
LDA* lda=static_cast<LDA*> (_lda);
K=lda->K;
V=lda->V;
nk=lda->nk;
nwk=lda->nwk;
beta=lda->beta;
alpha=lda->alpha;
alphaVec=lda->alphaVec;
betasum=lda->betasum;
alphasum=lda->alphasum;
}
virtual void copyState(void* _lda, std::string impl){
if(impl=="SparseLDA_Speed"){
SparseLDA_Speed* lda=static_cast<SparseLDA_Speed*> (_lda);
K=lda->K;
V=lda->V;
nk=lda->nk;
nwk.resize(V);
for(int w=0;w<V;w++){
nwk[w].resize(K);
for(int k=0;k<K;k++){
nwk[w][k]=lda->getnwk(w,k);
}
}
beta=lda->beta;
alpha=lda->alpha;
alphaVec=lda->alphaVec;
betasum=lda->betasum;
alphasum=lda->alphasum;
// }else if(impl=="AliasLDA_UltraSpeed"){
// AliasLDA_UltraSpeed* lda=static_cast<AliasLDA_UltraSpeed*> (_lda);
// K=lda->K;
// V=lda->V;
// nk=lda->nk;
// nwk=lda->nwk;
// beta=lda->beta;
// alpha=lda->alpha;
// alphaVec=lda->alphaVec;
// betasum=lda->betasum;
// alphasum=lda->alphasum;
}else if(impl=="LDA"){
LDA* lda=static_cast<LDA*> (_lda);
K=lda->K;
V=lda->V;
nk=lda->nk;
nwk=lda->nwk;
beta=lda->beta;
alpha=lda->alpha;
alphaVec=lda->alphaVec;
betasum=lda->betasum;
alphasum=lda->alphasum;
}
}
};
} /* namespace AliasLDA */