forked from bwaldvogel/liblinear-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrain.java
More file actions
343 lines (294 loc) · 12.5 KB
/
Copy pathTrain.java
File metadata and controls
343 lines (294 loc) · 12.5 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
336
337
338
339
340
341
342
343
package liblinear;
import static liblinear.Linear.atof;
import static liblinear.Linear.atoi;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Train {
public static void main(String[] args) throws IOException, InvalidInputDataException {
new Train().run(args);
}
private double bias = 1;
private boolean cross_validation = false;
private String inputFilename;
private String modelFilename;
private String weightFilename;
private int nr_fold;
private Parameter param = null;
private Problem prob = null;
private void do_cross_validation() {
int[] target = new int[prob.l];
long start, stop;
start = System.currentTimeMillis();
Linear.crossValidation(prob, param, nr_fold, target);
stop = System.currentTimeMillis();
System.out.println("time: " + (stop - start) + " ms");
int total_correct = 0;
for (int i = 0; i < prob.l; i++)
if (target[i] == prob.y[i]) ++total_correct;
System.out.printf("correct: %d%n", total_correct);
System.out.printf("Cross Validation Accuracy = %g%%%n", 100.0 * total_correct / prob.l);
}
private void exit_with_help() {
System.out.printf("Usage: train [options] training_set_file [model_file]%n" //
+ "options:%n"
+ "-s type : set type of solver (default 1)%n"
+ " 0 -- L2-regularized logistic regression (primal)%n"
+ " 1 -- L2-regularized L2-loss support vector classification (dual)%n"
+ " 2 -- L2-regularized L2-loss support vector classification (primal)%n"
+ " 3 -- L2-regularized L1-loss support vector classification (dual)%n"
+ " 4 -- multi-class support vector classification by Crammer and Singer%n"
+ " 5 -- L1-regularized L2-loss support vector classification%n"
+ " 6 -- L1-regularized logistic regression%n"
+ " 7 -- L2-regularized logistic regression (dual)%n"
+ "-c cost : set the parameter C (default 1)%n"
+ "-e epsilon : set tolerance of termination criterion%n"
+ " -s 0 and 2%n"
+ " |f'(w)|_2 <= eps*min(pos,neg)/l*|f'(w0)|_2,%n"
+ " where f is the primal function and pos/neg are # of%n"
+ " positive/negative data (default 0.01)%n"
+ " -s 1, 3, 4 and 7%n"
+ " Dual maximal violation <= eps; similar to libsvm (default 0.1)%n"
+ " -s 5 and 6%n"
+ " |f'(w)|_inf <= eps*min(pos,neg)/l*|f'(w0)|_inf,%n"
+ " where f is the primal function (default 0.01)%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"
+ "-v n: n-fold cross validation mode%n"
+ "-q : quiet mode (no outputs)%n"
+ "-W weight_file: set weight file%n"
);
System.exit(1);
}
Problem getProblem() {
return prob;
}
double getBias() {
return bias;
}
Parameter getParameter() {
return param;
}
void parse_command_line(String argv[]) {
int i;
// eps: see setting below
param = new Parameter(SolverType.L2R_L2LOSS_SVC_DUAL, 1, Double.POSITIVE_INFINITY);
// default values
bias = -1;
cross_validation = false;
weightFilename = null;
int nr_weight = 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.solverType = SolverType.values()[atoi(argv[i])];
break;
case 'c':
param.setC(atof(argv[i]));
break;
case 'e':
param.setEps(atof(argv[i]));
break;
case 'B':
bias = atof(argv[i]);
break;
case 'w':
++nr_weight;
{
int[] old = param.weightLabel;
param.weightLabel = new int[nr_weight];
System.arraycopy(old, 0, param.weightLabel, 0, nr_weight - 1);
}
{
double[] old = param.weight;
param.weight = new double[nr_weight];
System.arraycopy(old, 0, param.weight, 0, nr_weight - 1);
}
param.weightLabel[nr_weight - 1] = atoi(argv[i - 1].substring(2));
param.weight[nr_weight - 1] = atof(argv[i]);
break;
case 'v':
cross_validation = true;
nr_fold = atoi(argv[i]);
if (nr_fold < 2) {
System.err.println("n-fold cross validation: n must >= 2");
exit_with_help();
}
break;
case 'q':
Linear.disableDebugOutput();
break;
case 'W':
weightFilename = argv[i];
break;
default:
System.err.println("unknown option");
exit_with_help();
}
}
// determine filenames
if (i >= argv.length) exit_with_help();
inputFilename = argv[i];
if (i < argv.length - 1)
modelFilename = argv[i + 1];
else {
int p = argv[i].lastIndexOf('/');
++p; // whew...
modelFilename = argv[i].substring(p) + ".model";
}
if (param.eps == Double.POSITIVE_INFINITY) {
if (param.solverType == SolverType.L2R_LR || param.solverType == SolverType.L2R_L2LOSS_SVC) {
param.setEps(0.01);
} else if (param.solverType == SolverType.L2R_L2LOSS_SVC_DUAL || param.solverType == SolverType.L2R_L1LOSS_SVC_DUAL
|| param.solverType == SolverType.MCSVM_CS || param.solverType == SolverType.L2R_LR_DUAL) {
param.setEps(0.1);
} else if (param.solverType == SolverType.L1R_L2LOSS_SVC || param.solverType == SolverType.L1R_LR) {
param.setEps(0.01);
}
}
}
/**
* reads a problem from LibSVM format and additionally reads instance weights
* @throws IOException obviously in case of any I/O exception ;)
* @throws InvalidInputDataException if the input file is not correctly formatted
*/
public static Problem readProblem(File file, double bias, File weightFile) throws IOException, InvalidInputDataException {
Problem prob = readProblem(file, bias);
BufferedReader fp = new BufferedReader(new FileReader(weightFile));
try {
int lineNr = 0;
int i = 0;
while (true) {
String line = fp.readLine();
if (line == null) break;
lineNr++;
line = line.trim();
double weight;
try {
weight = atof(line);
} catch (NumberFormatException e) {
throw new InvalidInputDataException("invalid weight: " + line, file, lineNr, e);
}
if (weight < 0) throw new InvalidInputDataException("invalid weight: " + weight, file, lineNr);
if (i >= prob.l) throw new InvalidInputDataException("read too many weights", file, lineNr);
prob.W[i] = weight;
i++;
}
if (i != prob.l) {
throw new InvalidInputDataException("invalid number of weights: got " + i + " but require " + prob.l, file, lineNr);
}
return prob;
}
finally {
fp.close();
}
}
/**
* reads a problem from LibSVM format
* @throws IOException obviously in case of any I/O exception ;)
* @throws InvalidInputDataException if the input file is not correctly formatted
*/
public static Problem readProblem(File file, double bias) throws IOException, InvalidInputDataException {
BufferedReader fp = new BufferedReader(new FileReader(file));
try {
List<Integer> vy = new ArrayList<Integer>();
List<FeatureNode[]> vx = new ArrayList<FeatureNode[]>();
int max_index = 0;
int lineNr = 0;
while (true) {
String line = fp.readLine();
if (line == null) break;
lineNr++;
StringTokenizer st = new StringTokenizer(line, " \t\n\r\f:");
String token = st.nextToken();
try {
vy.add(atoi(token));
} catch (NumberFormatException e) {
throw new InvalidInputDataException("invalid label: " + token, file, lineNr, e);
}
int m = st.countTokens() / 2;
FeatureNode[] x;
if (bias >= 0) {
x = new FeatureNode[m + 1];
} else {
x = new FeatureNode[m];
}
int indexBefore = 0;
for (int j = 0; j < m; j++) {
token = st.nextToken();
int index;
try {
index = atoi(token);
} catch (NumberFormatException e) {
throw new InvalidInputDataException("invalid index: " + token, file, lineNr, e);
}
// assert that indices are valid and sorted
if (index < 0) throw new InvalidInputDataException("invalid index: " + index, file, lineNr);
if (index <= indexBefore) throw new InvalidInputDataException("indices must be sorted in ascending order", file, lineNr);
indexBefore = index;
token = st.nextToken();
try {
double value = atof(token);
x[j] = new FeatureNode(index, value);
} catch (NumberFormatException e) {
throw new InvalidInputDataException("invalid value: " + token, file, lineNr);
}
}
if (m > 0) {
max_index = Math.max(max_index, x[m - 1].index);
}
vx.add(x);
}
return constructProblem(vy, vx, max_index, bias);
}
finally {
fp.close();
}
}
void readProblem(String filename) throws IOException, InvalidInputDataException {
prob = Train.readProblem(new File(filename), bias);
}
void readProblem(String filename, String weightFilename) throws IOException, InvalidInputDataException {
prob = Train.readProblem(new File(filename), bias, new File(weightFilename));
}
private static Problem constructProblem(List<Integer> vy, List<FeatureNode[]> vx, int max_index, double bias) {
Problem prob = new Problem();
prob.bias = bias;
prob.l = vy.size();
prob.n = max_index;
prob.W = new double[prob.l];
if (bias >= 0) {
prob.n++;
}
prob.x = new FeatureNode[prob.l][];
for (int i = 0; i < prob.l; i++) {
prob.x[i] = vx.get(i);
prob.W[i] = 1;
if (bias >= 0) {
assert prob.x[i][prob.x[i].length - 1] == null;
prob.x[i][prob.x[i].length - 1] = new FeatureNode(max_index + 1, bias);
}
}
prob.y = new int[prob.l];
for (int i = 0; i < prob.l; i++)
prob.y[i] = vy.get(i);
return prob;
}
private void run(String[] args) throws IOException, InvalidInputDataException {
parse_command_line(args);
readProblem(inputFilename, weightFilename);
if (cross_validation)
do_cross_validation();
else {
Model model = Linear.train(prob, param);
Linear.saveModel(new File(modelFilename), model);
}
}
}