forked from raphaelcampos/stacking-bagged-boosted-forests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cuLazyNN_Broof.cu
151 lines (106 loc) · 4.2 KB
/
cuLazyNN_Broof.cu
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
#include "cuLazyNN_Broof.cuh"
#include <iostream>
// broof
//#include "lib/broof/tree.hpp"
#include "lib/broof/rf_bst.hpp"
#include "lib/broof/rf.hpp"
inline void prepareTrainSamples(RF_BOOST *rf, Dataset &training, cuSimilarity *k_nearest, unsigned int K)
{
for(int i = 0; i < K; i++) {
DTDocument *doc = new DTDocument();
cuSimilarity &sim = k_nearest[i];
unsigned int idx = sim.doc_id;
doc->set_id(Utils::toString(idx));
doc->set_class(Utils::toString(training.getSamples()[idx].y));
std::map<unsigned int, float>::iterator it;
for(it = training.getSamples()[idx].features.begin(); it != training.getSamples()[idx].features.end(); ++it){
unsigned int term_id = it->first;
float term_count = it->second;
doc->insert_term(term_id, term_count); //* log((double)training.size() / float(max(1, training.getIdf(term_id))));
}
rf->add_document(doc);
}
}
inline void prepareTrainSamples(RF *rf, Dataset &training, cuSimilarity *k_nearest, unsigned int K)
{
for(int i = 0; i < K; i++) {
DTDocument *doc = new DTDocument();
cuSimilarity &sim = k_nearest[i];
unsigned int idx = sim.doc_id;
doc->set_id(Utils::toString(idx));
doc->set_class(Utils::toString(training.getSamples()[idx].y));
std::map<unsigned int, float>::iterator it;
for(it = training.getSamples()[idx].features.begin(); it != training.getSamples()[idx].features.end(); ++it){
unsigned int term_id = it->first;
float term_count = it->second;
doc->insert_term(term_id, term_count); //* log((double)training.size() / float(max(1, training.getIdf(term_id))));
}
rf->add_document(doc);
}
}
cuLazyNN_Boost::~cuLazyNN_Boost(){
}
cuLazyNN_Boost::cuLazyNN_Boost(Dataset &data, float max_features, int n_boost_iter, int n_gpus) : cuKNN(data, n_gpus), max_features(max_features), n_boost_iter(n_boost_iter){
training = data;
}
void cuLazyNN_Boost::train(Dataset &data){
training = data;
cuKNN.train(data);
}
int cuLazyNN_Boost::classify(const std::map<unsigned int, float> &test_features, int K){
DTDocument * doc = new DTDocument();
doc->set_id("0");doc->set_class("1");
Scores<double> similarities(doc->get_id(), doc->get_class());
std::map<unsigned int, float>::const_iterator it;
for(it = test_features.begin(); it != test_features.end(); ++it){
unsigned int term_id = it->first;
float term_count = it->second;
doc->insert_term(term_id, term_count);
}
cuSimilarity *k_nearest = cuKNN.getKNearestNeighbors(test_features, K);
RF_BOOST * rf = new RF_BOOST(0, max_features, n_boost_iter);
prepareTrainSamples(rf, training, k_nearest, K);
rf->build();
std::map<const DTDocument*, double> doc_similarities;
similarities = rf->classify(doc, doc_similarities);
delete rf;
delete doc;
delete[] k_nearest;
return atoi(similarities.top().class_name.c_str());
}
std::vector<int> cuLazyNN_Boost::classify(Dataset &test, int K){
std::vector<cuSimilarity*> idxs = cuKNN.getKNearestNeighbors(test, K);
std::vector<int> pred;
int correct_cosine = 0, wrong_cosine = 0;
for (int i = 0; i < idxs.size(); ++i)
{
DTDocument * doc = new DTDocument();
doc->set_id("0");doc->set_class("1");
Scores<double> similarities(doc->get_id(), doc->get_class());
std::map<unsigned int, float> &test_features = test.getSamples()[i].features;
std::map<unsigned int, float>::const_iterator it;
for(it = test_features.begin(); it != test_features.end(); ++it){
unsigned int term_id = it->first;
float term_count = it->second;
doc->insert_term(term_id, term_count);
}
RF_BOOST * rf = new RF_BOOST(0, max_features, n_boost_iter);
prepareTrainSamples(rf, training, idxs[i], K);
rf->build();
std::map<const DTDocument*, double> doc_similarities;
similarities = rf->classify(doc, doc_similarities);
delete rf;
delete doc;
free(idxs[i]);
pred.push_back(atoi(similarities.top().class_name.c_str()));
if(pred.back() == test.getSamples()[i].y) {
correct_cosine++;
} else {
wrong_cosine++;
}
std::cerr.precision(4);
std::cerr.setf(std::ios::fixed);
std::cerr << "\r" << double(i+1)/test.getSamples().size() * 100 << "%" << " - " << double(correct_cosine) / (i+1);
}
return pred;
}