forked from nagadomi/kaggle-lshtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvt_ncc.cpp
86 lines (76 loc) · 2.02 KB
/
vt_ncc.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
#include "util.hpp"
#include "reader.hpp"
#include "tick.hpp"
#include "tfidf_transformer.hpp"
#include "nearest_centroid_classifier.hpp"
#include "evaluation.hpp"
#include <cstdio>
#include <map>
#include "SETTINGS.h"
// simple Centroid Classifier baseline
#define K 4
static void
print_evaluation(const Evaluation &evaluation, int i, long t)
{
double maf, map, mar, top1_acc;
evaluation.score(maf, map, mar, top1_acc);
printf("--- %d MaF: %f, MaP:%f, MaR:%f, Top1ACC: %f %ldms\n",
i,
maf, map, mar, top1_acc,
tick() -t);
}
int
main(void)
{
DataReader reader;
std::vector<fv_t> data;
std::vector<fv_t> test_data;
std::vector<label_t> labels;
std::vector<label_t> test_labels;
category_index_t category_index;
NearestCentroidClassifier centroid_classifier;
TFIDFTransformer tfidf;
long t = tick();
long t_all = tick();
Evaluation evaluation;
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();
t = tick();
srand(VT_SEED);
build_category_index(category_index, data, labels);
split_data(test_data, test_labels, data, labels, category_index, 0.05f);
build_category_index(category_index, data, labels);
printf("split train:%ld, test:%ld\n", data.size(), test_data.size());
t = tick();
tfidf.train(data);
tfidf.transform(data);
tfidf.transform(test_data);
centroid_classifier.train(category_index, data);
printf("build index %ldms\n", tick() -t );
t = tick();
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic, 1)
#endif
for (int i = 0; i < (int)test_data.size(); ++i) {
std::vector<int> topn_labels;
centroid_classifier.predict(topn_labels, K, test_data[i]);
#ifdef _OPENMP
#pragma omp critical
#endif
{
evaluation.update(topn_labels, test_labels[i]);
if (i % 1000 == 0) {
print_evaluation(evaluation, i, t);
t = tick();
}
}
}
printf("----\n");
print_evaluation(evaluation, test_data.size(), t_all);
return 0;
}