/
NaiveBayesGibbs.cpp
330 lines (298 loc) · 9.54 KB
/
NaiveBayesGibbs.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
#include <time.h>
#include <stdlib.h>
#include <fstream>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <random>
#include <set>
#include <cmath>
#include <string.h>
#include <cfloat>
#include "utils.hpp"
#include "NaiveBayesGibbs.hpp"
inline double logsumexp(double x, double y, bool flg) {
if (flg) return y; // init mode
double vmin = x;
double vmax = y;
if (x > y)
{
vmax = x;
vmin = y;
}
if (vmax > vmin + 50) {
return vmax;
} else {
return vmax + std::log(std::exp(vmin - vmax) + 1.0);
}
}
NaiveBayesGibbs::NaiveBayesGibbs(std::string trainDir, std::string testDir, int parameter, int interval):
DIRICHLET_HYPERPARAMETER(parameter), INTERVAL(interval)
{
//get all train files
std::cout << "loading all files..." << std::endl;
numCategories_ = stasticsFiles(trainDir, true);
for (int c=0; c<numCategories_; c++)
categoryFiles_.push_back(0);
stasticsFiles(testDir, false);
std::cout << "load "<< trainFiles_.size() << " files for train..." << std::endl;
std::cout << "load "<< testFiles_.size() << " files for test..." << std::endl;
std::cout << "num categories : " << numCategories_ << std::endl;
//init sampling labels;
std::cout << "init sampling labels..." << std::endl;
initLabels();
//init sampling theta
std::cout << "init sampling theta..." << std::endl;
stasticsWords();
updateTheta(0);
const int numTrainFiles = trainFiles_.size();
const double Z = (numTrainFiles + numCategories_*DIRICHLET_HYPERPARAMETER -1);
logLabelNorm = log(Z);
}
void NaiveBayesGibbs::initLabels()
{
int numTrainFiles = trainFiles_.size();
for (int i=0; i<numTrainFiles; i++)
{
int label = trainAns_[i];
labelHistory_[i] = labelVec_[i] = label;
categoryFiles_[label]++;
}
}
void NaiveBayesGibbs::stasticsWords()
{
//stastics words
std::cout << "stastics words... " << std::endl;
std::set<std::string> uniqWords;
for (std::vector<std::string>::iterator iter = trainFiles_.begin(); iter != trainFiles_.end(); iter++)
{
std::ifstream fin(*iter);
std::string line;
std::vector<std::string> v;
while (getline(fin, line))
{
std::shared_ptr<std::vector<std::string> > splits = split(line, "\\s+");
int sz = splits->size();
for (int i=0; i<sz; i++)
{
uniqWords.insert(splits->at(i));
v.push_back(splits->at(i));
}
}
fin.close();
trainWords_.push_back(v);
}
for (std::set<std::string>::iterator iter=uniqWords.begin(); iter!=uniqWords.end(); iter++)
{
thetaCur_[*iter] = std::vector<double>(numCategories_, 0.0);
thetaHistory_[*iter] = std::vector<double>(numCategories_, 0);
wordCount_[*iter] = std::vector<int>(numCategories_, 0);
}
std::cout << "train words number " << uniqWords.size() << std::endl;
uniqWords.clear();
for (std::vector<std::string>::iterator iter = testFiles_.begin(); iter != testFiles_.end(); iter++)
{
std::ifstream fin(*iter);
std::string line;
std::vector<std::string> v;
while (getline(fin, line))
{
std::shared_ptr<std::vector<std::string> > splits = split(line, "\\s+");
int sz = splits->size();
for (int i=0; i<sz; i++)
{
uniqWords.insert(splits->at(i));
v.push_back(splits->at(i));
}
}
fin.close();
testWords_.push_back(v);
}
std::cout << "test words number " << uniqWords.size() << std::endl;
std::cout << "stastics the number of given words in each category" << std::endl;
for (int f=0; f<trainFiles_.size(); f++)
{
int label = labelVec_[f];
std::vector<std::string> words = trainWords_[f];
for (std::vector<std::string>::iterator iter=words.begin(); iter!=words.end(); iter++)
wordCount_[*iter][label]++;
}
}
NaiveBayesModel NaiveBayesGibbs::train(int numIter)
{
double accuracy2 = checkMLE();
std::cout << "start training..." << std::endl;
for (int t=1; t<=numIter; t++)//number iteration
{
std::cout << Utils::getTime() << " update theta" << std::endl;
updateTheta(t);
double accuracy = check();
std::cout << Utils::getTime() << " Iteratation num " << t << " Accuracy " << accuracy << " maximum likelihood " << accuracy2 << std::endl;
std::cout << "dirichlet hyperparameter : " << DIRICHLET_HYPERPARAMETER << std::endl;
}
return NaiveBayesModel(numCategories_, labelNames_, thetaHistory_);
}
void NaiveBayesGibbs::updateTheta(int round)
{
std::mt19937 eng(int(time(0)));
//#pragma omp parallel for
for (int c=0; c<numCategories_; c++)
{
std::vector<double> y;
double sum = 0.0;
HashMap::iterator iter = thetaCur_.begin();
for (; iter!=thetaCur_.end(); iter++)
{
std::gamma_distribution<double> gamma(wordCount_[iter->first][c] + DIRICHLET_HYPERPARAMETER);
double tmp = log(1.0 * gamma(eng));
y.push_back(tmp);
sum = logsumexp(sum, tmp, iter==thetaCur_.begin());
}
int num=0;
for (iter=thetaCur_.begin(); iter!=thetaCur_.end(); iter++)
{
double value = y[num]-sum;
iter->second[c] = value;
// thetaHistory_[iter->first][c] += value;
double tmp = thetaHistory_[iter->first][c];
if (round % INTERVAL == 0)
thetaHistory_[iter->first][c] = logsumexp(tmp, value, tmp==0);
num++;
}
}
}
int NaiveBayesGibbs::stasticsFiles(const std::string& dir, bool isTrainDir)
{
int label = 0;
DIR * pdir;
struct dirent * entry;
pdir = opendir(dir.c_str());
while ((entry = readdir(pdir)) != NULL)
{
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
if (entry->d_type == DT_DIR)
{
int ans = label;
if (isTrainDir)
labelNames_.push_back(entry->d_name);
else
{
for (int i=labelNames_.size()-1; i>=0; i--)
{
if (labelNames_[i] == entry->d_name)
{
ans = i;
break;
}
}
}
std::string folder;
if (dir[dir.length()-1] == '/')
folder = dir + entry->d_name;
else
folder = dir + "/" + entry->d_name;
DIR* subPdir;
struct dirent * subEntry;
subPdir = opendir(folder.c_str());
while ((subEntry = readdir(subPdir)) != NULL)
{
if (strcmp(subEntry->d_name, ".") == 0 || strcmp(subEntry->d_name, "..") == 0)
continue;
if (subEntry->d_type == DT_DIR)
{
std::cout << "文件组织形式有误,这里不应该出现文件夹 : " << folder+"/"+subEntry->d_name << std::endl;
}
else
{
if (isTrainDir)
{
trainFiles_.push_back(folder + "/" + subEntry->d_name);
labelHistory_.push_back(-1);
labelVec_.push_back(-1);
trainAns_.push_back(ans);
}
else
{
testFiles_.push_back(folder + "/" + subEntry->d_name);
testAns_.push_back(ans);
}
}
}
label++;
}
}
return label;
}
double NaiveBayesGibbs::check()
{
int correct = 0;
int numFiles = testFiles_.size();
for (int i=0; i<numFiles; i++)
{
int label = predict(i);
if (label == testAns_[i])
correct++;
}
return 1.0*correct/numFiles;
}
double NaiveBayesGibbs::checkMLE()
{
int correct = 0;
int numFiles = testFiles_.size();
for (int i=0; i<numFiles; i++)
{
int label = predictMLE(i);
if (label == testAns_[i])
correct++;
}
return 1.0*correct/numFiles;
}
int NaiveBayesGibbs::predict(int fid)
{
std::vector<std::string> words = testWords_[fid];
std::vector<double> probs(numCategories_, 0.0);
for (int i=words.size()-1; i>=0; i--)
{
if (thetaHistory_.find(words[i]) == thetaHistory_.end())
continue;
std::vector<double> thetaList = thetaHistory_[words[i]];
for (int c=0; c<numCategories_; c++)
probs[c] += thetaList[c];
}
double max = -DBL_MAX;
int label = -1;
for (int c=0; c<numCategories_; c++)
{
if (probs[c] > max)
{
max = probs[c];
label = c;
}
}
return label;
}
int NaiveBayesGibbs::predictMLE(int fid)
{
std::vector<std::string> words = testWords_[fid];
std::vector<double> probs(numCategories_, 0.0);
for (int i=words.size()-1; i>=0; i--)
{
if (wordCount_.find(words[i]) == wordCount_.end())
continue;
std::vector<int> counter = wordCount_[words[i]];
for (int c=0; c<numCategories_; c++)
probs[c] += log(counter[c]+0.01);
}
double max = -DBL_MAX;
int label = -1;
for (int c=0; c<numCategories_; c++)
{
if (probs[c] > max)
{
max = probs[c];
label = c;
}
}
return label;
}