Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathsvm_python_interface.cpp
More file actions
304 lines (284 loc) · 11.4 KB
/
Copy pathsvm_python_interface.cpp
File metadata and controls
304 lines (284 loc) · 11.4 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//functions for python interface
#include <thundersvm/util/log.h>
#include <thundersvm/model/svc.h>
#include <thundersvm/model/svr.h>
#include <thundersvm/model/oneclass_svc.h>
#include <thundersvm/model/nusvc.h>
#include <thundersvm/model/nusvr.h>
#include <thundersvm/util/metric.h>
#include "thundersvm/cmdparser.h"
using std::fstream;
using std::stringstream;
DataSet dataset_python;
extern "C" {
DataSet* DataSet_new() {return new DataSet();}
void DataSet_load_from_python(DataSet *dataset, float *y, char **x, int len) {dataset->load_from_python(y, x, len);}
void thundersvm_train(int argc, char **argv) {
CMDParser parser;
parser.parse_command_line(argc, argv);
/*
parser.param_cmd.svm_type = SvmParam::NU_SVC;
parser.param_cmd.kernel_type = SvmParam::RBF;
parser.param_cmd.C = 100;
parser.param_cmd.gamma = 0;
parser.param_cmd.nu = 0.1;
parser.param_cmd.epsilon = 0.001;
*/
DataSet train_dataset;
char input_file_path[1024] = DATASET_DIR;
char model_file_path[1024] = DATASET_DIR;
strcat(input_file_path, "../python/");
strcat(model_file_path, "../python/");
strcat(input_file_path, parser.svmtrain_input_file_name.c_str());
strcat(model_file_path, parser.model_file_name.c_str());
train_dataset.load_from_file(input_file_path);
SvmModel *model = nullptr;
switch (parser.param_cmd.svm_type) {
case SvmParam::C_SVC:
model = new SVC();
break;
case SvmParam::NU_SVC:
model = new NuSVC();
break;
case SvmParam::ONE_CLASS:
model = new OneClassSVC();
break;
case SvmParam::EPSILON_SVR:
model = new SVR();
break;
case SvmParam::NU_SVR:
model = new NuSVR();
break;
}
//todo add this to check_parameter method
if (parser.param_cmd.svm_type == SvmParam::NU_SVC) {
train_dataset.group_classes();
for (int i = 0; i < train_dataset.n_classes(); ++i) {
int n1 = train_dataset.count()[i];
for (int j = i + 1; j < train_dataset.n_classes(); ++j) {
int n2 = train_dataset.count()[j];
if (parser.param_cmd.nu * (n1 + n2) / 2 > min(n1, n2)) {
printf("specified nu is infeasible\n");
return;
}
}
}
}
#ifdef USE_CUDA
CUDA_CHECK(cudaSetDevice(parser.gpu_id));
#endif
vector<float_type> predict_y, test_y;
if (parser.do_cross_validation) {
vector<float_type> test_predict = model->cross_validation(train_dataset, parser.param_cmd, parser.nr_fold);
int dataset_size = test_predict.size() / 2;
test_y.insert(test_y.end(), test_predict.begin(), test_predict.begin() + dataset_size);
predict_y.insert(predict_y.end(), test_predict.begin() + dataset_size, test_predict.end());
} else {
model->train(train_dataset, parser.param_cmd);
model->save_to_file(model_file_path);
//predict_y = model->predict(train_dataset.instances(), 10000);
//test_y = train_dataset.y();
}
/*
//perform svm testing
Metric *metric = nullptr;
switch (parser.param_cmd.svm_type) {
case SvmParam::C_SVC:
case SvmParam::NU_SVC: {
metric = new Accuracy();
break;
}
case SvmParam::EPSILON_SVR:
case SvmParam::NU_SVR: {
metric = new MSE();
break;
}
case SvmParam::ONE_CLASS: {
}
}
if (metric) {
LOG(INFO) << metric->name() << " = " << metric->score(predict_y, test_y);
}
*/
return;
}
void thundersvm_predict(int argc, char **argv){
CMDParser parser;
parser.parse_command_line(argc, argv);
char model_file_path[1024] = DATASET_DIR;
char predict_file_path[1024] = DATASET_DIR;
char output_file_path[1024] = DATASET_DIR;
strcat(model_file_path, "../python/");
strcat(predict_file_path, "../python/");
strcat(output_file_path, "../python/");
strcat(model_file_path, parser.svmpredict_model_file_name.c_str());
strcat(predict_file_path, parser.svmpredict_input_file.c_str());
strcat(output_file_path, parser.svmpredict_output_file.c_str());
fstream file;
file.open(model_file_path, std::fstream::in);
string feature, svm_type;
file >> feature >> svm_type;
CHECK_EQ(feature, "svm_type");
SvmModel *model = nullptr;
Metric *metric = nullptr;
if (svm_type == "c_svc") {
model = new SVC();
metric = new Accuracy();
} else if (svm_type == "nu_svc") {
model = new NuSVC();
metric = new Accuracy();
} else if (svm_type == "one_class") {
model = new OneClassSVC();
//todo determine a metric
} else if (svm_type == "epsilon_svr") {
model = new SVR();
metric = new MSE();
} else if (svm_type == "nu_svr") {
model = new NuSVR();
metric = new MSE();
}
#ifdef USE_CUDA
CUDA_CHECK(cudaSetDevice(parser.gpu_id));
#endif
model->load_from_file(model_file_path);
file.close();
file.open(output_file_path, std::fstream::out);
DataSet predict_dataset;
predict_dataset.load_from_file(predict_file_path);
vector<float_type> predict_y;
predict_y = model->predict(predict_dataset.instances(), 10000);
for (int i = 0; i < predict_y.size(); ++i) {
file << predict_y[i] << std::endl;
}
file.close();
if (metric) {
LOG(INFO) << metric->name() << " = " << metric->score(predict_y, predict_dataset.y());
}
}
void load_from_python_interface(float *y, char **x, int len){
dataset_python.load_from_python(y, x, len);
/*
dataset_python.y_.clear();
dataset_python.instances_.clear();
dataset_python.total_count_ = 0;
dataset_python.n_features_ = 0;
for(int i = 0; i < len; i++){
int i;
float_type v;
string line = x[i];
stringstream ss(line);
dataset_python.y_.push_back(y[i]);
dataset_python.instances_.emplace_back();
string tuple;
while(ss >> tuple){
CHECK_EQ(sscanf(tuple.c_str(), "%d:%f", &i, &v), 2) << "read error, using [index]:[value] format";
dataset_python.instances_[total_count_].emplace_back(i, v);
if(i > dataset_python.n_features_) dataset_python.n_features_ = i;
};
total_count_++;
}
*/
}
void thundersvm_train_after_parse(char **option, int len, char *file_name){
CMDParser parser;
parser.parse_python(len, option);
char model_file_path[1024] = DATASET_DIR;
strcat(model_file_path, "../python/");
strcat(model_file_path, file_name);
SvmModel *model = nullptr;
switch (parser.param_cmd.svm_type) {
case SvmParam::C_SVC:
model = new SVC();
break;
case SvmParam::NU_SVC:
model = new NuSVC();
break;
case SvmParam::ONE_CLASS:
model = new OneClassSVC();
break;
case SvmParam::EPSILON_SVR:
model = new SVR();
break;
case SvmParam::NU_SVR:
model = new NuSVR();
break;
}
//todo add this to check_parameter method
if (parser.param_cmd.svm_type == SvmParam::NU_SVC) {
dataset_python.group_classes();
for (int i = 0; i < dataset_python.n_classes(); ++i) {
int n1 = dataset_python.count()[i];
for (int j = i + 1; j < dataset_python.n_classes(); ++j) {
int n2 = dataset_python.count()[j];
if (parser.param_cmd.nu * (n1 + n2) / 2 > min(n1, n2)) {
printf("specified nu is infeasible\n");
return;
}
}
}
}
#ifdef USE_CUDA
CUDA_CHECK(cudaSetDevice(parser.gpu_id));
#endif
vector<float_type> predict_y, test_y;
if (parser.do_cross_validation) {
vector<float_type> test_predict = model->cross_validation(dataset_python, parser.param_cmd, parser.nr_fold);
int dataset_size = test_predict.size() / 2;
test_y.insert(test_y.end(), test_predict.begin(), test_predict.begin() + dataset_size);
predict_y.insert(predict_y.end(), test_predict.begin() + dataset_size, test_predict.end());
} else {
model->train(dataset_python, parser.param_cmd);
model->save_to_file(model_file_path);
//predict_y = model->predict(train_dataset.instances(), 10000);
//test_y = train_dataset.y();
}
}
void thundersvm_predict_after_parse(char *model_file_name, char *output_file_name, char **option, int len){
CMDParser parser;
parser.parse_python(len, option);
char model_file_path[1024] = DATASET_DIR;
char output_file_path[1024] = DATASET_DIR;
strcat(model_file_path, "../python/");
strcat(output_file_path, "../python/");
strcat(model_file_path, model_file_name);
strcat(output_file_path, output_file_name);
fstream file;
file.open(model_file_path, std::fstream::in);
string feature, svm_type;
file >> feature >> svm_type;
CHECK_EQ(feature, "svm_type");
SvmModel *model = nullptr;
Metric *metric = nullptr;
if (svm_type == "c_svc") {
model = new SVC();
metric = new Accuracy();
} else if (svm_type == "nu_svc") {
model = new NuSVC();
metric = new Accuracy();
} else if (svm_type == "one_class") {
model = new OneClassSVC();
//todo determine a metric
} else if (svm_type == "epsilon_svr") {
model = new SVR();
metric = new MSE();
} else if (svm_type == "nu_svr") {
model = new NuSVR();
metric = new MSE();
}
#ifdef USE_CUDA
CUDA_CHECK(cudaSetDevice(parser.gpu_id));
#endif
model->load_from_file(model_file_path);
file.close();
file.open(output_file_path, std::fstream::out);
vector<float_type> predict_y;
predict_y = model->predict(dataset_python.instances(), 10000);
for (int i = 0; i < predict_y.size(); ++i) {
file << predict_y[i] << std::endl;
}
file.close();
if (metric) {
LOG(INFO) << metric->name() << " = " << metric->score(predict_y, dataset_python.y());
}
}
}
You can’t perform that action at this time.
