-
Notifications
You must be signed in to change notification settings - Fork 2
/
ocuLazyNN_RF.cpp
164 lines (127 loc) · 4.79 KB
/
ocuLazyNN_RF.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
#include "cuLazyNN_RF.h"
#include <iostream>
inline TermCriteria TC(int iters, double eps)
{
return TermCriteria(TermCriteria::MAX_ITER + (eps > 0 ? TermCriteria::EPS : 0), iters, eps);
}
cuLazyNN_RF::cuLazyNN_RF(){
}
cuLazyNN_RF::~cuLazyNN_RF(){
if(randomForest != NULL){
delete randomForest;
}
}
cuLazyNN_RF::cuLazyNN_RF(Dataset &data){
training = data;
convertDataset(data);
buildInvertedIndex();
}
void cuLazyNN_RF::train(Dataset &data){
training = data;
convertDataset(data);
buildInvertedIndex();
}
int cuLazyNN_RF::classify(std::map<unsigned int, double> test_features, int K){
Mat testSample( 1, training.dimension(), CV_32F);
std::vector<Entry> query;
std::map<unsigned int, double>::iterator it;
for(it = test_features.begin(); it != test_features.end(); ++it){
unsigned int term_id = it->first;
double term_count = it->second;
query.push_back(Entry(0, term_id, term_count)); // doc_id, term_id, term_count
float *ptr = testSample.ptr<float>(0);
ptr[term_id] = term_count * log((double)training.size() / float(max(1, training.getIdf(term_id))));
}
//Creates an empty document if there are no terms
if(query.empty()) {
query.push_back(Entry(0, 0, 0));
}
Similarity *k_nearest = KNN(inverted_index, query, K, CosineDistance);
Ptr<RTrees> randomForest = RTrees::create();
// randomForest->setMinSampleCount(floor(training.size()*0.01));
// randomForest->setRegressionAccuracy(0.f);
// randomForest->setUseSurrogates(true);
// randomForest->setMaxCategories(16);
// randomForest->setPriors(Mat());
// randomForest->setCalculateVarImportance(false);
//randomForest->setActiveVarCount(1);
// randomForest->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 200, 0.1));
//randomForest->setMaxDepth(10);
randomForest->setMinSampleCount(10);
randomForest->setRegressionAccuracy(0);
randomForest->setUseSurrogates(false);
randomForest->setMaxCategories(15);
randomForest->setPriors(Mat());
randomForest->setCalculateVarImportance(false);
randomForest->setActiveVarCount(4);
randomForest->setTermCriteria(TC(200,0.00f));
Ptr<TrainData> dt = prepareTrainSamples(k_nearest, K);
printf("Training...\n");
randomForest->train(dt);
printf("Number of trees: %d\n", randomForest->getRoots().size());
printf("Predicting...\n");
float pred = randomForest->predict(testSample);
printf("Prediction : %f\n", pred);
return (int)round(pred);
}
void cuLazyNN_RF::convertDataset(Dataset &data){
num_terms = 0;
// delete all old entries
entries.clear();
num_docs = data.getSamples().size();
for (unsigned int i = 0; i < num_docs; ++i)
{
unsigned int doc_id = i;
std::map<unsigned int, double>::iterator it;
for(it = data.getSamples()[i].features.begin(); it != data.getSamples()[i].features.end(); ++it){
unsigned int term_id = it->first;
double term_cout = it->second;
num_terms = std::max(num_terms, term_id + 1);
entries.push_back(Entry(doc_id, term_id, term_cout)); // doc_id, term_id, term_count
}
doc_to_class[doc_id] = data.getSamples()[i].y;
}
}
void cuLazyNN_RF::buildInvertedIndex(){
inverted_index = make_inverted_index(num_docs, num_terms, entries);
}
void cuLazyNN_RF::createRF(){
if(randomForest != NULL){
delete randomForest;
}
randomForest = RTrees::create();
// Commented in order to allow the trees
// be grown to the their maximal depth
//randomForest->setMaxDepth(4);
randomForest->setMinSampleCount(2);
randomForest->setRegressionAccuracy(0.f);
randomForest->setUseSurrogates(false);
randomForest->setMaxCategories(16);
randomForest->setPriors(Mat());
randomForest->setCalculateVarImportance(false);
randomForest->setActiveVarCount(1);
randomForest->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 5, 0));
}
Ptr<TrainData> cuLazyNN_RF::prepareTrainSamples(Similarity *k_nearest, unsigned int K)
{
printf("Allocation Matrix %dx%d...\n", K , training.dimension());
Mat samples(K, training.dimension(), CV_32F);
Mat responses(K, 1, CV_32F);
for(int i = 0; i < K; i++) {
Similarity &sim = k_nearest[i];
unsigned int idx = sim.doc_id;
responses.at<double>(i, 0) = training.getSamples()[idx].y;
float *ptr = responses.ptr<float>(i);
ptr[0] = training.getSamples()[idx].y;
std::map<unsigned int, double>::iterator it;
for(it = training.getSamples()[idx].features.begin(); it != training.getSamples()[idx].features.end(); ++it){
unsigned int term_id = it->first;
double term_cout = it->second;
//cout << term_cout << endl;
float *ptr = samples.ptr<float>(i);
ptr[term_id] = term_cout * log((double)training.size() / float(max(1, training.getIdf(term_id))));
}
}
printf("Creating TrainData...\n");
return TrainData::create(samples, ROW_SAMPLE, responses);
}