-
Notifications
You must be signed in to change notification settings - Fork 1
/
sampler.h
203 lines (176 loc) · 7.45 KB
/
sampler.h
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
// Copyright 2008 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _OPENSOURCE_GLDA_SAMPLER_H__
#define _OPENSOURCE_GLDA_SAMPLER_H__
#include "common.h"
#include "document.h"
#include "model.h"
#include "accumulative_model.h"
#include <sstream>
typedef map<int,std::pair< vector<int> ,map<int,vector<int> > > > RuleVec;
typedef map<int,std::pair< set<int> ,map<int,set<int> > > > RuleSet;
namespace learning_lda {
// LDASampler trains LDA models and computes statistics about documents in
// LDA models.
class LDATopicSplitRule{
RuleVec split_rule_;
public:
bool LoadSplitRule(istream& in,const map<string, int>& word_index_map)
{
string line;
RuleSet loc_split_rule_;
while (getline(in, line)) { // Each line is a training document.
if (line.size() > 0 && // Skip empty lines.
line[0] != '\r' && // Skip empty lines.
line[0] != '\n' && // Skip empty lines.
line[0] != '$' && // Skip empty lines.
line[0] != '#') { // Skip comment lines.
std::istringstream ss(line);
string word;
ss >> word;
if ( "Topic" != word)
continue;
int orgtopic;
int curtopic;
ss >> orgtopic;
ss >> word;
if ( "->" != word)
continue;
ss >> curtopic;
loc_split_rule_.find(orgtopic);
if (loc_split_rule_.end() == loc_split_rule_.find(orgtopic))
{
std::pair< set<int> ,map<int,set<int> > > rule_;
loc_split_rule_.insert(make_pair(orgtopic,rule_));
}
std::pair< set<int> ,map<int,set<int> > > & currule =loc_split_rule_.find(orgtopic)->second;
currule.first.insert(curtopic);
map<int,set<int> >& word2topic = currule.second;
while (ss >> word) {
if (word_index_map.end() == word_index_map.find(word))
continue;
int wordid = word_index_map.find(word)->second;
if (word2topic.end() == word2topic.find(wordid))
{
set<int> topiclist;
word2topic.insert(make_pair(wordid,topiclist));
}
word2topic.find(wordid)->second.insert(curtopic);
}
}
}
map<int,std::pair< set<int> ,map<int,set<int> > > >::iterator iter;
for (iter = loc_split_rule_.begin() ; loc_split_rule_.end() != iter ; iter++)
{
iter->first;
set<int>::iterator iter2;
vector<int> topicvec;
for (iter2 = iter->second.first.begin(); iter->second.first.end() != iter2 ;iter2++)
{
topicvec.push_back(*iter2);
}
map<int,set<int> > & word2topic = iter->second.second;
map<int,vector<int> > word2topicvec;
map<int,set<int> >::iterator iter3;
for (iter3 = word2topic.begin();word2topic.end() != iter3 ; iter3++)
{
vector<int> wtopicvec;
set<int>::iterator iter4;
for (iter4 = iter3->second.begin(); iter3->second.end() != iter4 ;iter4++)
{
wtopicvec.push_back(*iter4);
}
word2topicvec.insert(make_pair(iter3->first,wtopicvec));
}
split_rule_.insert(make_pair(iter->first,make_pair(topicvec,word2topicvec)));
}
return true;
}
bool FindTopicList(int topicid,int wordid,vector<int> & topiclist)
{
RuleVec::iterator iter = split_rule_.find(topicid);
if (split_rule_.end() == iter)
{
return false;
}
vector<int> & default_list = iter->second.first;
map<int,vector<int> > & word2topiclist = iter->second.second;
if (word2topiclist.end() == word2topiclist.find(wordid))
{
topiclist = default_list;
}
else
{
topiclist = word2topiclist.find(wordid)->second;
}
return true;
}
};
class LDASampler {
public:
// alpha and beta are the Gibbs sampling symmetric hyperparameters.
// model is the model to use.
LDASampler(double alpha, double beta,
LDAModel* model,
LDAAccumulativeModel* accum_model);
~LDASampler() {}
// Given a corpus, whose every document have been initialized (i.e.,
// every word occurrences has a (randomly) assigned topic,
// initialize model_ to count the word-topic co-occurrences.
void InitModelGivenTopics(const LDACorpus& corpus);
// Performs one round of Gibbs sampling on documents in the corpus
// by invoking SampleNewTopicsForDocument(...). If we are to train
// a model given training data, we should set train_model to true,
// and the algorithm updates model_ during Gibbs sampling.
// Otherwise, if we are to sample the latent topics of a query
// document, we should set train_model to false. If train_model is
// true, burn_in indicates should we accumulate the current estimate
// to accum_model_. For the first certain number of iterations,
// where the algorithm has not converged yet, you should set burn_in
// to false. After that, we should set burn_in to true.
void DoIteration(LDACorpus* corpus, bool train_model, bool burn_in);
void DoIteration2(LDACorpus* corpus, bool train_model, bool burn_in);
// Performs one round of Gibbs sampling on a document. Updates
// document's topic assignments. For learning, update_model_=true,
// for sampling topics of a query, update_model_==false.
void SampleNewTopicsForDocument(LDADocument* document,
bool update_model);
// The core of the Gibbs sampling process. Compute the full conditional
// posterior distribution of topic assignments to the indicated word.
//
// That is, holding all word-topic assignments constant, except for the
// indicated one, compute a non-normalized probability distribution over
// topics for the indicated word occurrence.
void GenerateTopicDistributionForWord(const LDADocument& document,
int word, int current_word_topic, bool train_model,
vector<double>* distribution) const;
void SampleNewTopicsForDocument2(LDADocument* document,
bool update_model);
void GenerateTopicDistributionForWord2(const LDADocument& document,
int word, int current_word_topic, bool train_model,
const vector<int>& topic_list,vector<double>* distribution) const;
void AdjustCorpusWithRule(LDACorpus& corpus,
int new_topic_num,
set<int> & new_words,
LDATopicSplitRule &adjust_rule);
// Computes the log likelihood of a document.
double LogLikelihood(LDADocument* document) const;
private:
const double alpha_;
const double beta_;
LDAModel* model_;
LDAAccumulativeModel* accum_model_;
};
} // namespace learning_lda
#endif // _OPENSOURCE_GLDA_SAMPLER_H__