This repository has been archived by the owner on Sep 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsvm_train.java
353 lines (316 loc) · 12.8 KB
/
svm_train.java
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import libsvm.*;
import java.io.*;
import java.util.*;
class svm_train {
private svm_parameter param; // set by parse_command_line
private svm_problem prob; // set by read_problem
private svm_model model;
private String input_file_name; // set by parse_command_line
private String model_file_name; // set by parse_command_line
private String error_msg;
private int cross_validation;
private int nr_fold;
private static svm_print_interface svm_print_null = new svm_print_interface() {
public void print(String s) {
}
};
private static void exit_with_help() {
System.out
.print("Usage: svm_train [options] training_set_file [model_file]\n"
+ "options:\n"
+ "-s svm_type : set type of SVM (default 0)\n"
+ " 0 -- C-SVC\n"
+ " 1 -- nu-SVC\n"
+ " 2 -- one-class SVM\n"
+ " 3 -- epsilon-SVR\n"
+ " 4 -- nu-SVR\n"
+ "-t kernel_type : set type of kernel function (default 2)\n"
+ " 0 -- linear: u'*v\n"
+ " 1 -- polynomial: (gamma*u'*v + coef0)^degree\n"
+ " 2 -- radial basis function: exp(-gamma*|u-v|^2)\n"
+ " 3 -- sigmoid: tanh(gamma*u'*v + coef0)\n"
+ " 4 -- precomputed kernel (kernel values in training_set_file)\n"
+ "-d degree : set degree in kernel function (default 3)\n"
+ "-g gamma : set gamma in kernel function (default 1/num_features)\n"
+ "-r coef0 : set coef0 in kernel function (default 0)\n"
+ "-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)\n"
+ "-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)\n"
+ "-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)\n"
+ "-m cachesize : set cache memory size in MB (default 100)\n"
+ "-e epsilon : set tolerance of termination criterion (default 0.001)\n"
+ "-h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)\n"
+ "-b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)\n"
+ "-wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)\n"
+ "-v n : n-fold cross validation mode\n"
+ "-q : quiet mode (no outputs)\n");
System.exit(1);
}
private void do_cross_validation() {
int total_correct = 0;
double total_error = 0;
double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
double[] target = new double[prob.l];
svm.svm_cross_validation(prob, param, nr_fold, target);
if (param.svm_type == svm_parameter.EPSILON_SVR
|| param.svm_type == svm_parameter.NU_SVR) {
for (int i = 0; i < prob.l; i++) {
double y = prob.y[i];
double v = target[i];
total_error += (v - y) * (v - y);
sumv += v;
sumy += y;
sumvv += v * v;
sumyy += y * y;
sumvy += v * y;
}
System.out.print("Cross Validation Mean squared error = "
+ total_error / prob.l + "\n");
System.out
.print("Cross Validation Squared correlation coefficient = "
+ ((prob.l * sumvy - sumv * sumy) * (prob.l * sumvy - sumv
* sumy))
/ ((prob.l * sumvv - sumv * sumv) * (prob.l * sumyy - sumy
* sumy)) + "\n");
} else {
int maxLabelsNum = 0;
for (int l = 0; l < prob.l; l++) {
if (prob.y[l] > maxLabelsNum)
maxLabelsNum = (int) prob.y[l];
}
int[][] classifMatrix = new int[maxLabelsNum+1][maxLabelsNum+1];
for (int l = 0; l < prob.l; l++) {
if (target[l] == prob.y[l])
++total_correct;
// store in matrix
classifMatrix[(int) prob.y[l]][(int) target[l]]++;
}
System.out.print("Cross Validation Accuracy = " + 100.0
* total_correct / prob.l + "%\n");
// find total classified as per label
int[] totalClassifiedAs = new int[maxLabelsNum+1];
// display matrix
System.out.printf("Classification Matrix : \n\n");
for (int i = 0; i <= maxLabelsNum; i++) {
int[] hits = classifMatrix[i];
StringBuffer buffer = new StringBuffer();
buffer.append(i).append("\t: ");
for (int j = 0; j < hits.length; j++) {
totalClassifiedAs[j] += hits[j];
buffer.append("\t");
if (i == j)
buffer.append("*");
buffer.append(hits[j]);
if (i == j)
buffer.append("*");
}
System.out.println(buffer.toString());
}
// get accuracy per type
System.out.printf("\n\nScores per label : \n\n");
for (int i = 0; i <= maxLabelsNum; i++) {
int[] hits = classifMatrix[i];
float sum = 0;
for (int h : hits) {
sum += h;
}
float success = hits[i];
float recall = 100 * success / sum;
float precision = 100 * success
/ (float) (totalClassifiedAs[i]);
System.out.println(i + "\tRECALL :\t" + recall
+ "\tPRECISION :\t" + precision);
}
}
}
private void run(String argv[]) throws IOException {
parse_command_line(argv);
read_problem();
error_msg = svm.svm_check_parameter(prob, param);
if (error_msg != null) {
System.err.print("ERROR: " + error_msg + "\n");
System.exit(1);
}
if (cross_validation != 0) {
do_cross_validation();
} else {
model = svm.svm_train(prob, param);
svm.svm_save_model(model_file_name, model);
}
}
public static void main(String argv[]) throws IOException {
svm_train t = new svm_train();
t.run(argv);
}
private static double atof(String s) {
double d = Double.valueOf(s).doubleValue();
if (Double.isNaN(d) || Double.isInfinite(d)) {
System.err.print("NaN or Infinity in input\n");
System.exit(1);
}
return (d);
}
private static int atoi(String s) {
return Integer.parseInt(s);
}
private void parse_command_line(String argv[]) {
int i;
svm_print_interface print_func = null; // default printing to stdout
param = new svm_parameter();
// default values
param.svm_type = svm_parameter.C_SVC;
param.kernel_type = svm_parameter.RBF;
param.degree = 3;
param.gamma = 0; // 1/num_features
param.coef0 = 0;
param.nu = 0.5;
param.cache_size = 100;
param.C = 1;
param.eps = 1e-3;
param.p = 0.1;
param.shrinking = 1;
param.probability = 0;
param.nr_weight = 0;
param.weight_label = new int[0];
param.weight = new double[0];
cross_validation = 0;
// parse options
for (i = 0; i < argv.length; i++) {
if (argv[i].charAt(0) != '-')
break;
if (++i >= argv.length)
exit_with_help();
switch (argv[i - 1].charAt(1)) {
case 's':
param.svm_type = atoi(argv[i]);
break;
case 't':
param.kernel_type = atoi(argv[i]);
break;
case 'd':
param.degree = atoi(argv[i]);
break;
case 'g':
param.gamma = atof(argv[i]);
break;
case 'r':
param.coef0 = atof(argv[i]);
break;
case 'n':
param.nu = atof(argv[i]);
break;
case 'm':
param.cache_size = atof(argv[i]);
break;
case 'c':
param.C = atof(argv[i]);
break;
case 'e':
param.eps = atof(argv[i]);
break;
case 'p':
param.p = atof(argv[i]);
break;
case 'h':
param.shrinking = atoi(argv[i]);
break;
case 'b':
param.probability = atoi(argv[i]);
break;
case 'q':
print_func = svm_print_null;
i--;
break;
case 'v':
cross_validation = 1;
nr_fold = atoi(argv[i]);
if (nr_fold < 2) {
System.err.print("n-fold cross validation: n must >= 2\n");
exit_with_help();
}
break;
case 'w':
++param.nr_weight;
{
int[] old = param.weight_label;
param.weight_label = new int[param.nr_weight];
System.arraycopy(old, 0, param.weight_label, 0,
param.nr_weight - 1);
}
{
double[] old = param.weight;
param.weight = new double[param.nr_weight];
System.arraycopy(old, 0, param.weight, 0,
param.nr_weight - 1);
}
param.weight_label[param.nr_weight - 1] = atoi(argv[i - 1]
.substring(2));
param.weight[param.nr_weight - 1] = atof(argv[i]);
break;
default:
System.err.print("Unknown option: " + argv[i - 1] + "\n");
exit_with_help();
}
}
svm.svm_set_print_string_function(print_func);
// determine filenames
if (i >= argv.length)
exit_with_help();
input_file_name = argv[i];
if (i < argv.length - 1)
model_file_name = argv[i + 1];
else {
int p = argv[i].lastIndexOf('/');
++p; // whew...
model_file_name = argv[i].substring(p) + ".model";
}
}
// read in a problem (in svmlight format)
private void read_problem() throws IOException {
BufferedReader fp = new BufferedReader(new FileReader(input_file_name));
Vector<Double> vy = new Vector<Double>();
Vector<svm_node[]> vx = new Vector<svm_node[]>();
int max_index = 0;
while (true) {
String line = fp.readLine();
if (line == null)
break;
StringTokenizer st = new StringTokenizer(line, " \t\n\r\f:");
vy.addElement(atof(st.nextToken()));
int m = st.countTokens() / 2;
svm_node[] x = new svm_node[m];
for (int j = 0; j < m; j++) {
x[j] = new svm_node();
x[j].index = atoi(st.nextToken());
x[j].value = atof(st.nextToken());
}
if (m > 0)
max_index = Math.max(max_index, x[m - 1].index);
vx.addElement(x);
}
prob = new svm_problem();
prob.l = vy.size();
prob.x = new svm_node[prob.l][];
for (int i = 0; i < prob.l; i++)
prob.x[i] = vx.elementAt(i);
prob.y = new double[prob.l];
for (int i = 0; i < prob.l; i++)
prob.y[i] = vy.elementAt(i);
if (param.gamma == 0 && max_index > 0)
param.gamma = 1.0 / max_index;
if (param.kernel_type == svm_parameter.PRECOMPUTED)
for (int i = 0; i < prob.l; i++) {
if (prob.x[i][0].index != 0) {
System.err
.print("Wrong kernel matrix: first column must be 0:sample_serial_number\n");
System.exit(1);
}
if ((int) prob.x[i][0].value <= 0
|| (int) prob.x[i][0].value > max_index) {
System.err
.print("Wrong input format: sample_serial_number out of range\n");
System.exit(1);
}
}
fp.close();
}
}