-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmexTrain.cpp
262 lines (224 loc) · 6.57 KB
/
mexTrain.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
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
/* Author : Subhransu Maji
*
* Matlab entry code for training
*
* Version 1.0
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "mex.h"
#include "additiveModel.h"
#include "matlabModel.h"
#if MX_API_VER < 0x07030000
typedef int mwIndex;
#endif
#define CMD_LEN 2048
#define Malloc(type,n) (type *)malloc((n)*sizeof(type))
#define INF HUGE_VAL
parameter param; // parameters set by parse_command_line
additiveModel *model; // spline model
int col_format_flag, nvec, dim; // other options
double **x, *y; // input training data/labels
// help
void exit_with_help(){
mexPrintf(
"Usage: model = train(training_label_vector, training_instance_matrix, 'options', 'col');\n"
"options:\n"
"-t type : 0: Spline, 1: Trigonometric, 2: Hermite (default=0)\n"
"-d degree : set the B-Spline degree (default=1) d={0,1,2,3}\n"
"-r reg : set the order of regularization (default=1) r={0,1,2,...}\n"
"-n bins : set the number of bins (default 10)\n"
"-c cost : set the parameter C (default 1)\n"
"-e epsilon : set tolerance of termination criterion\n"
" Dual maximal violation <= eps; similar to libsvm (default 0.1)\n"
"-B bias : if bias >= 0, instance x becomes [x; bias]; if < 0, no bias term added (default 1)\n"
"-wi weight : weights adjust the parameter C of different classes (see README for details)\n"
"col:\n"
" if 'col' is set, training_instance_matrix is parsed in column format, otherwise is in row format\n"
);
}
// parse options in command line
int parse_command_line(int nrhs, const mxArray *prhs[], char *model_file_name){
int i, argc = 1;
char cmd[CMD_LEN];
char *argv[CMD_LEN/2];
//set default parameters
param.encoding = SPLINE;
param.degree = 1;
param.reg = 1;
param.Cp = 1;
param.Cn = 1;
param.eps = 0.1;
param.numbins = 10;
param.bias = 10;
//per class weights
int nr_weight=0;
col_format_flag = 0;
if(nrhs <= 1) // incorrent number of arguments
return 1;
if(nrhs == 4){ // column format is enabled
mxGetString(prhs[3], cmd, mxGetN(prhs[3])+1);
if(strcmp(cmd, "col") == 0)
col_format_flag = 1;
}
// put options in argv[]
if(nrhs > 2){
mxGetString(prhs[2], cmd, mxGetN(prhs[2]) + 1);
if((argv[argc] = strtok(cmd, " ")) != NULL)
while((argv[++argc] = strtok(NULL, " ")) != NULL)
;
}
// parse options
for(i=1;i<argc;i++){
if(argv[i][0] != '-') break;
++i;
switch(argv[i-1][1]){
case 't':
param.encoding = atoi(argv[i]);
break;
case 'd':
param.degree = atoi(argv[i]);
break;
case 'r':
param.reg = atoi(argv[i]);
break;
case 'n':
param.numbins = atoi(argv[i]);
break;
case 'c':
param.Cn = param.Cp = atof(argv[i]);
break;
case 'e':
param.eps = atof(argv[i]);
break;
case 'B':
param.bias = atof(argv[i]);
break;
case 'w':
++nr_weight;
if(atoi(&argv[i-1][2]) > 0)
param.Cp *= atof(argv[i]);
else
param.Cn *= atof(argv[i]);
break;
default:
mexPrintf("unknown option\n");
return 1;
}
}
return 0;
}
//empty output constructor
static void fake_answer(mxArray *plhs[]){
plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL);
}
//read the input into internal data structures. keeps pointers only to avoid duplication of data
int read_problem_dense(const mxArray *label_vec, const mxArray *instance_mat){
int i, label_vector_row_num;
double *samples;
mxArray *instance_mat_col; // instance sparse matrix in column format
if(col_format_flag)
instance_mat_col = (mxArray *)instance_mat;
else{
// transpose instance matrix
mxArray *prhs[1], *plhs[1];
prhs[0] = mxDuplicateArray(instance_mat);
if(mexCallMATLAB(1, plhs, 1, prhs, "transpose")){
mexPrintf("Error: cannot transpose training instance matrix\n");
return -1;
}
instance_mat_col = plhs[0];
mxDestroyArray(prhs[0]);
}
// init number of instances and feature dimension
nvec = (int) mxGetN(instance_mat_col);
dim = (int) mxGetM(instance_mat_col);
// init number of labels
label_vector_row_num = (int) mxGetM(label_vec);
if(label_vector_row_num != nvec){
mexPrintf("Error: Length of label vector does not match # of instances.\n");
return -1;
}
// obtain pointers to the data/labels
y = mxGetPr(label_vec);
samples = mxGetPr(instance_mat_col);
x = new double*[nvec];
for(i=0;i<nvec;i++)
x[i] = &samples[i*dim];
return 0;
}
// check the parameters
const char* check_parameter(const parameter * param){
if(param->encoding < 0 || param->encoding > 3){
return "encoding type should be {0,1,2}";
}
if(param->encoding == 0 && (param->degree < 0 || param->degree > 3)){
return "degree should be {0,1,2,3} for splines";
}
if(param->reg < 0){
return "regularization should be >= 0";
}
if(param->numbins < 1){
return "numbins < 1";
}
if(param->Cp <= 0){
return "Cp < 0";
}
if(param->Cn <= 0){
return "Cn < 0";
}
if(param->eps <= 0){
return "eps <= 0";
}
return NULL;
}
// Interface function of matlab
// now assume prhs[0]: label prhs[1]: features
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]){
const char *error_msg;
// transform the input matrix to libspline format
if(nrhs > 0 && nrhs < 5){
int err=0;
if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) {
mexPrintf("Error: label vector and instance matrix must be double\n");
fake_answer(plhs);
return;
}
if(parse_command_line(nrhs, prhs, NULL)){ //failed to parse options
exit_with_help();
fake_answer(plhs);
return;
}
if(!mxIsSparse(prhs[1])) //requires features be dense
err = read_problem_dense(prhs[0], prhs[1]);
else{
mexPrintf("Error : training_instance_matrix must be dense\n");
fake_answer(plhs);
return;
}
error_msg = check_parameter(¶m);
if(err || error_msg){
if (error_msg != NULL)
mexPrintf("Error: %s\n", error_msg);
fake_answer(plhs);
delete [] x;
return;
}
const char *error_msg;
model = new additiveModel(¶m, x, dim, nvec); //initialize
model->train(x,y,nvec,¶m); //train the model
error_msg = model_to_matlab_structure(plhs, model);
if(error_msg)
mexPrintf("Error: can't convert spline model to matrix structure: %s\n", error_msg);
delete model;
delete [] x;
}else{
exit_with_help();
fake_answer(plhs);
return;
}
}