-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathseq_test.cpp
More file actions
335 lines (254 loc) · 7.47 KB
/
seq_test.cpp
File metadata and controls
335 lines (254 loc) · 7.47 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <stdio.h>
#include <cblas.h>
#include <stdlib.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string.h>
#include <cblas.h>
#include <math.h>
#include <getopt.h>
#include <string>
using namespace std;
void populate_data(float** x, int* y);
void get_x(float* x, float* x_copy, int idx, int num_attributes);
float rbf_kernel(float* x1,float* x2);
float get_test_accuracy(float** x, int* y, float* alpha, float** x_model, int* y_model, int num_sv);
int get_num_sv();
void populate_model(float** x_model, int* y_model, float* alpha, int num_sv);
typedef struct {
int num_attributes;
int num_test_data;
float gamma;
char input_file_name[30];
char model_file_name[30];
} state_model;
//global structure for training parameters
static state_model state;
static void usage_exit() {
cerr <<
" Command Line:\n"
"\n"
" -a/--num-att : [REQUIRED] The number of attributes\n"
" /features\n"
" -x/--num-ex : [REQUIRED] The number of testing \n"
" examples\n"
" -f/--file-path : [REQUIRED] Path to the testing file\n"
" -g/--gamma : Parameter gamma of the radial basis\n"
" function: exp(-gamma*|u-v|^2)\n"
" (default: 1/num-att)"
" -m/--model : [REQUIRED] Path of model (output of training phase)\n"
"\n";
exit(-1);
}
static struct option longOptionsG[] =
{
{ "num-att", required_argument, 0, 'a' },
{ "num-ex", required_argument, 0, 'x' },
{ "gamma", required_argument, 0, 'g' },
{ "file-path", required_argument, 0, 'f' },
{ "model", required_argument, 0, 'm' },
{ 0, 0, 0, 0 }
};
static void parse_arguments(int argc, char* argv[]) {
// Default Values
state.num_attributes = -1;
state.num_test_data = -1;
state.gamma = -1;
strcpy(state.input_file_name, "");
strcpy(state.model_file_name, "");
// Parse args
while (1) {
int idx = 0;
int c = getopt_long(argc, argv, "a:x:g:f:m:", longOptionsG, &idx);
if (c == -1) {
// End of options
break;
}
switch (c) {
case 'a':
state.num_attributes = atoi(optarg);
break;
case 'x':
state.num_test_data = atoi(optarg);
break;
case 'g':
state.gamma = atof(optarg);
break;
case 'f':
strcpy(state.input_file_name, optarg);
break;
case 'm':
strcpy(state.model_file_name, optarg);
break;
default:
cerr << "\nERROR: Unknown option: -" << c << "\n";
// Usage exit
usage_exit();
}
}
if(strcmp(state.input_file_name,"")==0 || strcmp(state.model_file_name,"")==0) {
cerr << "Enter a valid file name\n";
usage_exit();
}
if(state.num_attributes <= 0 || state.num_test_data <= 0) {
cerr << "Missing a required parameter, or invalid parameter\n";
usage_exit();
}
if(state.gamma < 0) {
state.gamma = 1 / state.num_attributes;
}
}
int main(int argc, char *argv[]) {
//Obtain the command line arguments
parse_arguments(argc, argv);
//input data attributes and labels
float** x = new float*[state.num_test_data];
for(int i = 0 ; i < state.num_test_data; i++) {
x[i] = new float[state.num_attributes]();
}
int* y = new int[state.num_test_data];
//read data from input file
populate_data(x, y);
cout << "Populated test data\n";
//get no. of support vectors
int num_sv = get_num_sv();
cout << "Total number of Support Vectors: " << num_sv << "\n";
//training model data
//input data attributes and labels
float** x_model = new float*[num_sv];
for(int i = 0 ; i < num_sv; i++) {
x_model[i] = new float[state.num_attributes]();
}
int* y_model = new int[num_sv];
float* alpha = new float[num_sv];
//read data from model file
populate_model(x_model, y_model, alpha, num_sv);
cout << "Populated training model\n";
//obtain testing accuracy
float test_accuracy = get_test_accuracy(x, y, alpha, x_model, y_model, num_sv);
cout << "Test accuracy: " << test_accuracy << "\n";
//clear test data
for(int i = 0 ; i < state.num_test_data; i++) {
delete [] x[i];
}
delete [] x;
delete [] y;
//clear model data
for(int i = 0 ; i < num_sv; i++) {
delete [] x_model[i];
}
delete [] x_model;
delete [] y_model;
delete [] alpha;
return 0;
}
float get_test_accuracy(float** x, int* y, float* alpha, float** x_model, int* y_model, int num_sv) {
int num_correct = 0;
for(int i=0; i<state.num_test_data; i++) {
float dual = 0;
for(int j=0; j<num_sv; j++) {
dual += y_model[j]*alpha[j]*rbf_kernel(x_model[j], x[i]);
}
//dual += b;
int result = 1;
if(dual < 0) {
result = -1;
}
if(result == y[i]) {
num_correct++;
}
}
return ((float)num_correct/(state.num_test_data));
}
void populate_model(float** x_model, int* y_model, float* alpha, int num_sv) {
ifstream file(state.model_file_name);
if(!file.is_open())
{
cout << "Couldn't open model file";
exit(-1);
}
//std::vector<std::string> result;
string line;
int curr_example_num = 0;
//get gamma
getline(file, line);
state.gamma = stof(line);
while (curr_example_num < num_sv)
{
getline(file,line);
stringstream lineStream(line);
string cell;
getline(lineStream,cell,',');
alpha[curr_example_num] = stof(cell);
getline(lineStream,cell,',');
y_model[curr_example_num] = stoi(cell);
int curr_attr_num = 0;
while(getline(lineStream,cell,',')) {
x_model[curr_example_num][curr_attr_num++] = stof(cell);
}
++curr_example_num;
}
}
int get_num_sv() {
int num_sv = 0;
string sv_line;
ifstream model_file(state.model_file_name);
if(!model_file.is_open()) {
cout << "Model file " << state.model_file_name << " couldn't be opened.\n";
exit(-1);
}
while (getline(model_file, sv_line)) {
num_sv++;
}
//decrement a count for gamma
num_sv --;
return num_sv;
}
void populate_data(float** x, int* y)
{
ifstream file(state.input_file_name);
if(!file.is_open())
{
cout << "Couldn't open file";
return;
}
//std::vector<std::string> result;
string line;
int curr_example_num = 0;
while (curr_example_num < state.num_test_data)
{
getline(file,line);
stringstream lineStream(line);
string cell;
getline(lineStream,cell,',');
y[curr_example_num] = stoi(cell);
int curr_attr_num = 0;
while(getline(lineStream,cell,','))
{
x[curr_example_num][curr_attr_num++] = stof(cell);
}
++curr_example_num;
}
}
float rbf_kernel(float* x1,float* x2) {
float* x1_copy = new float[state.num_attributes];
//deep copy
get_x(x1, x1_copy, 0, state.num_attributes);
//get_x(x2, x2_copy, 0, state.num_attributes);
//TODO: See if BLAS has nicer functions
cblas_saxpy(state.num_attributes, -1, x2, 1, x1_copy, 1); // x1_copy = -x2_copy + x1_copy
float norm = cblas_snrm2(state.num_attributes, x1_copy, 1);
float result = (float)exp(-1 *(double)state.gamma*norm*norm);
delete [] x1_copy;
return result;
}
void get_x(float* x, float* x_copy, int idx, int num_attributes) {
int ctr = 0;
int start_index = (idx*num_attributes);
int end_index = start_index+num_attributes;
for(int i = start_index; i < end_index; i++) {
x_copy[ctr++] = x[i];
}
}