forked from nagadomi/kaggle-lshtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vt_classifier.cpp
214 lines (200 loc) · 5.64 KB
/
vt_classifier.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
#include "util.hpp"
#include "reader.hpp"
#include "tick.hpp"
#include "tfidf_transformer.hpp"
#include "evaluation.hpp"
#include "ncc_cache.hpp"
#include "binary_classifier.hpp"
#include <cstdio>
#include <map>
#include "SETTINGS.h"
// validation program for binary classifier
void
build_train_data(unsigned int k,
category_index_t &dataset,
std::vector<fv_t> &data,
std::vector<label_t> &labels,
NCCCache &cache)
{
dataset.clear();
for (int i = 0; i < (int)data.size(); ++i) {
std::vector<int> results;
std::set<int> hit_labels;
if (cache.get(i, results)) {
if (k < results.size()) {
results.erase(results.begin() + k, results.end());
}
for (auto res = results.begin(); res != results.end(); ++res) {
hit_labels.insert(*res);
}
for (auto l = hit_labels.begin(); l != hit_labels.end(); ++l) {
auto d = dataset.find(*l);
if (d != dataset.end()) {
d->second.push_back(i);
} else {
std::vector<int> vec;
vec.push_back(i);
dataset.insert(std::make_pair(*l, vec));
}
}
}
}
}
void
get_train_data(
int target,
std::vector<fv_t> &posi,
std::vector<fv_t> &nega,
const std::vector<fv_t> &test_data,
const std::vector<label_t> &test_labels,
const category_index_t &dataset)
{
posi.clear();
nega.clear();
auto target_dataset = dataset.find(target);
if (target_dataset == dataset.end()) {
return;
}
for (auto i = target_dataset->second.begin(); i != target_dataset->second.end(); ++i) {
if (test_labels[*i].find(target) != test_labels[*i].end()) {
posi.push_back(test_data[*i]);
} else {
nega.push_back(test_data[*i]);
}
}
}
int
main(void)
{
DataReader reader;
std::vector<fv_t> data;
std::vector<label_t> labels;
std::vector<fv_t> test_data;
std::vector<label_t> test_labels;
NCCCache cache;
NCCCache cache_test;
TFIDFTransformer transformer;
category_index_t category_index;
category_index_t dataset;
category_index_t test_category_index;
category_index_t test_dataset;
long t = tick();
size_t posi_tp = 0;
size_t nega_tp = 0;
size_t nega_count = 0;
size_t posi_count = 0;
float posi_acc = 0.0f;
float nega_acc = 0.0f;
float mar = 0.0f;
float map = 0.0f;
size_t test_count = 0;
size_t zero_count = 0;
if (!reader.open(TRAIN_DATA)) {
fprintf(stderr, "cant read file\n");
return -1;
}
reader.read(data, labels);
printf("read %ld, %ld, %ldms\n", data.size(), labels.size(), tick() - t);
reader.close();
if (!cache.load(CACHE)) {
std::fprintf(stderr, "failed: please either run ./vt_prefetch\n");
return -1;
}
if (!cache_test.load(CACHE_TEST)) {
std::fprintf(stderr, "failed: please either run ./vt_prefetch\n");
return -1;
}
t = tick();
build_category_index(category_index, data, labels);
srand(13);
split_data(test_data, test_labels, data, labels, category_index, 0.05f);
build_category_index(category_index, data, labels);
build_category_index(test_category_index, test_data, test_labels);
t = tick();
transformer.train(data);
transformer.transform(data);
transformer.transform(test_data);
printf("build index %ldms\n", tick() -t );
t = tick();
build_train_data(K_TRAIN, dataset, data, labels, cache);
build_train_data(K_PREDICT, test_dataset, test_data, labels, cache_test);
printf("build dataset %ld %ldms\n", dataset.size(), tick() -t );
for (auto i = category_index.begin(); i != category_index.end(); ++i) {
long tt = tick();
// learning classifier each labels
std::vector<fv_t> posi;
std::vector<fv_t> nega;
std::vector<fv_t> test_posi;
std::vector<fv_t> test_nega;
if (i->second.size() < 2) {
continue;
}
get_train_data(i->first, posi, nega, data, labels, dataset);
get_train_data(i->first, test_posi, test_nega, test_data, test_labels, test_dataset);
BinaryClassifier model;
model.train(posi, nega, LR_ETA, LR_P, LR_ITERATION);
{
int correct_posi = 0;
int correct_nega = 0;
std::vector<fv_t> *instance[2] = {&test_nega, &test_posi};
for (int k = 0; k < 2; ++k) {
for (auto j = instance[k]->begin();
j != instance[k]->end();
++j)
{
float p = model.predict(*j);
if (p > 0.0f) {
if (k == 1) {
correct_posi += 1;
}
} else {
if (k == 0) {
correct_nega += 1;
}
}
}
}
posi_count += test_posi.size();
nega_count += test_nega.size();
posi_tp += correct_posi;
nega_tp += correct_nega;
if (test_posi.size() > 0) {
posi_acc += (float)correct_posi/test_posi.size();
} else {
zero_count += 1;
}
if ((correct_posi + (test_nega.size() - correct_nega)) > 0) {
map += (float)correct_posi / (correct_posi + (test_nega.size() - correct_nega));
}
auto c = test_category_index.find(i->first);
if (c != test_category_index.end() && c->second.size() > 0) {
mar += (float)correct_posi / test_category_index.find(i->first)->second.size();
}
if (test_nega.size() > 0) {
nega_acc += (float)correct_nega/test_nega.size();
}
test_count += 1;
printf("label %08d: Non zero feature: %ld, accuracy nega:%f%% (%ld), posi:%f%% (%ld) %ldms\n",
i->first,
model.size(),
(float)correct_nega / test_nega.size(),
test_nega.size(),
(float)correct_posi / test_posi.size(),
test_posi.size(),
tick() - tt
);
printf("posi: %f(%f), nega: %f(%f), P/N: %f, MaF: %f, MaP: %f, MaR: %f zero: %f\n",
(float)posi_tp/posi_count,
posi_acc/test_count,
(float)nega_tp/nega_count,
nega_acc/test_count,
(float)posi_count/nega_count,
(2.0f *(map / test_count) * (mar / test_count)) / ((map / test_count) + (mar / test_count)),
map / test_count,
mar / test_count,
(float)zero_count/test_count
);
}
}
return 0;
}