forked from benob/tbb-boost
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtbb-predict.cpp
More file actions
189 lines (181 loc) · 7.34 KB
/
tbb-predict.cpp
File metadata and controls
189 lines (181 loc) · 7.34 KB
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
/* This file is part of tbb-boost.
tbb-boost is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
tbb-boost is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with tbb-boost. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include <unordered_map>
#include <vector>
#include <string>
#include "tbb/task_scheduler_init.h"
#include "tbb/parallel_for.h"
#include "tbb/blocked_range.h"
using namespace std;
using namespace tbb;
class Classifier {
public:
string feature;
double threshold;
double** weight;
};
class ExampleProcessor {
int num_labels;
unordered_map<string, vector<Classifier> > const *classifiers;
double const *default_score;
vector<char*> *lines;
int *output;
//double *score;
public:
ExampleProcessor(int num_labels, unordered_map<string, vector<Classifier> > const *classifiers, double const * default_score, vector<char*> *lines, int* output) {
this->num_labels = num_labels;
this->classifiers = classifiers;
this->default_score = default_score;
this->lines = lines;
this->output = output;
//this->score = new double[num_labels];
//fprintf(stderr, "%p\n", this->score);
}
~ExampleProcessor() {
//delete this->score;
}
void operator() (const blocked_range<unsigned int>& range) const {
//fprintf(stderr, "%d %d\n", range.begin(), range.end());
for(unsigned int line = range.begin(); line != range.end(); line++) {
char* save_pointer = NULL;
char* token = strtok_r((*lines)[line], " \t\n\r", &save_pointer);
if(token == NULL || token[0] == '\0') {
output[line] = -1;
continue;
}
double score[num_labels];
memcpy(score, default_score, sizeof(double) * num_labels);
string feature;
double value = 0;
for(int i = 0; token != NULL; token = strtok_r(NULL, " \t\n\r", &save_pointer), i++) {
if(i == 0) {
// label
} else {
char* end = strrchr(token, ':');
if(end == NULL) {
fprintf(stderr, "ERROR: unexpected feature format \"%s\", line %d\n", token, line + 1);
exit(1);
}
*end = '\0';
feature = token;
value = strtod(end + 1, NULL);
unordered_map<string, vector<Classifier> >::const_iterator found = classifiers->find(feature);
if(found != classifiers->end()) {
for(vector<Classifier>::const_iterator classifier = (*found).second.begin(); classifier != (*found).second.end(); classifier++) {
for(int label = 0; label < num_labels; label++) {
score[label] -= (*classifier).weight[label][0];
if(value < (*classifier).threshold) score[label] += (*classifier).weight[label][1];
else score[label] += (*classifier).weight[label][2];
}
}
}
}
}
double max = 0;
int argmax = -1;
for(int label = 0; label < num_labels; label++) {
if(max < score[label] || argmax == -1) {
argmax = label;
max = score[label];
}
}
output[line] = argmax;
}
}
};
int main(int argc, char** argv) {
if(argc != 2) {
fprintf(stdout, "USAGE: %s model < test > predictions\n", argv[0]);
exit(1);
}
int num_labels = 0;
unordered_map<string, vector<Classifier> > classifiers;
vector<string> labels;
FILE* model = fopen(argv[1], "r");
if(model == NULL) {
perror("Error loading model");
exit(2);
}
size_t buffer_size = 0;
char* buffer = 0;
// read model
Classifier classifier;
int state = 0;
while(-1 != getline(&buffer, &buffer_size, model)) {
char* token = strtok(buffer, " \t:\n\r");
vector<const char*> values;
for(;token != NULL; token = strtok(NULL, " \t:\n\r")) {
values.push_back(token); //strtod(token, NULL));
}
if(state == 0) {
labels.resize(values.size() / 2);
for(int label = 0; label < (int) values.size(); label+= 2) {
labels[strtol(values[label + 1], NULL, 10)] = values[label];
}
num_labels = labels.size();
state = 1;
} else if(state == 1) {
classifier.feature = values[1];
classifier.threshold = strtod(values[2], NULL);
state = 2;
} else if(state == 2) {
classifier.weight = new double*[num_labels];
for(int label = 0; label < num_labels; label++) {
classifier.weight[label] = new double[3];
classifier.weight[label][0] = strtod(values[label], NULL);
}
state = 3;
} else if(state == 3) {
for(int label = 0; label < num_labels; label++) classifier.weight[label][1] = strtod(values[label], NULL);
state = 4;
} else if(state == 4) {
for(int label = 0; label < num_labels; label++) classifier.weight[label][2] = strtod(values[label], NULL);
unordered_map<string, vector<Classifier> >::iterator found = classifiers.find(classifier.feature);
if(found == classifiers.end()) {
classifiers[classifier.feature] = vector<Classifier>();
}
classifiers[classifier.feature].push_back(classifier);
state = 5;
} else if(state == 5) {
state = 1;
}
}
fclose(model);
double default_score[num_labels];
for(unordered_map<string, vector<Classifier> >::iterator item = classifiers.begin(); item != classifiers.end(); item++) {
for(vector<Classifier>::iterator classifier = (*item).second.begin(); classifier != (*item).second.end(); classifier++) {
for(int label = 0; label < num_labels; label++) {
default_score[label] += (*classifier).weight[label][0];
}
}
}
vector<char*> lines;
while(-1 != getline(&buffer, &buffer_size, stdin)) {
lines.push_back(strdup(buffer));
}
free(buffer);
int output[lines.size()];
ExampleProcessor processor(num_labels, &classifiers, default_score, &lines, output);
parallel_for(blocked_range<unsigned int>(0, lines.size()), processor, auto_partitioner());
for(unsigned int line = 0 ; line < lines.size(); line++) {
if(output[line] == -1) fprintf(stdout, "\n"); // pass empty lines as is
else fprintf(stdout, "%s\n", labels[output[line]].c_str());
free(lines[line]);
}
return 0;
}