Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/main/java/de/bwaldvogel/liblinear/Linear.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,9 @@ public static Model loadModel(Reader inputReader) throws IOException {

Pattern whitespace = Pattern.compile("\\s+");

BufferedReader reader = null;
if (inputReader instanceof BufferedReader) {
reader = (BufferedReader)inputReader;
} else {
reader = new BufferedReader(inputReader);
}
BufferedReader reader = inputReader instanceof BufferedReader bufferedReader
? bufferedReader
: new BufferedReader(inputReader);

String line;
while ((line = reader.readLine()) != null) {
Expand Down
26 changes: 11 additions & 15 deletions src/main/java/de/bwaldvogel/liblinear/Predict.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
import static de.bwaldvogel.liblinear.Linear.*;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;
Expand Down Expand Up @@ -136,10 +132,12 @@ static void doPredict(BufferedReader reader, Writer writer, Model model, boolean
}

private static void exit_with_help() {
System.out.printf("Usage: predict [options] test_file model_file output_file%n" //
+ "options:%n" //
+ "-b probability_estimates: whether to output probability estimates, 0 or 1 (default 0); currently for logistic regression only%n" //
+ "-q quiet mode (no outputs)%n");
System.out.print("""
Usage: predict [options] test_file model_file output_file
options:
-b probability_estimates: whether to output probability estimates, 0 or 1 (default 0); currently for logistic regression only
-q quiet mode (no outputs)
""".replace("\n", System.lineSeparator()));
System.exit(1);
}

Expand Down Expand Up @@ -177,11 +175,9 @@ public static void main(String[] argv) throws IOException {
exit_with_help();
}

try (FileInputStream in = new FileInputStream(argv[i]);
BufferedReader reader = new BufferedReader(new InputStreamReader(in, Linear.FILE_CHARSET));
FileOutputStream out = new FileOutputStream(argv[i + 2]);
Writer writer = new BufferedWriter(new OutputStreamWriter(out, Linear.FILE_CHARSET))) {
Model model = Linear.loadModel(Paths.get(argv[i + 1]));
try (BufferedReader reader = Files.newBufferedReader(Path.of(argv[i]), Linear.FILE_CHARSET);
Writer writer = Files.newBufferedWriter(Path.of(argv[i + 2]), Linear.FILE_CHARSET)) {
Model model = Linear.loadModel(Path.of(argv[i + 1]));
doPredict(reader, writer, model, flag_predict_probability);
}
}
Expand Down
88 changes: 45 additions & 43 deletions src/main/java/de/bwaldvogel/liblinear/Train.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,49 +93,51 @@ private void do_cross_validation() {
}

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"
+ " for multi-class classification%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 -- 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"
+ " for regression%n"
+ " 11 -- L2-regularized L2-loss support vector regression (primal)%n"
+ " 12 -- L2-regularized L2-loss support vector regression (dual)%n"
+ " 13 -- L2-regularized L1-loss support vector regression (dual)%n"
+ " for outlier detection%n"
+ " 21 -- one-class support vector machine (dual)%n"
+ "-c cost : set the parameter C (default 1)%n"
+ "-p epsilon : set the epsilon in loss function of SVR (default 0.1)%n"
+ "-n nu : set the parameter nu of one-class SVM (default 0.5)%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 11%n"
+ " |f'(w)|_2 <= eps*|f'(w0)|_2 (default 0.0001)%n"
+ " -s 1, 3, 4, 7, and 21%n"
+ " Dual maximal violation <= eps; similar to libsvm (default 0.1 except 0.01 for -s 21)%n"
+ " -s 5 and 6%n"
+ " |f'(w)|_1 <= eps*min(pos,neg)/l*|f'(w0)|_1,%n"
+ " where f is the primal function (default 0.01)%n"
+ " -s 12 and 13%n"
+ " |f'(alpha)|_1 <= eps |f'(alpha0)|,%n"
+ " where f is the dual function (default 0.1)%n"
+ "-B bias : if bias >= 0, instance x becomes [x; bias]; if < 0, no bias term added (default -1)%n"
+ "-R : not regularize the bias; must with -B 1 to have the bias; DON'T use this unless you know what it is%n"
+ " (for -s 0, 2, 5, 6, 11)%n"
+ "-wi weight: weights adjust the parameter C of different classes (see README for details)%n"
+ "-v n: n-fold cross validation mode%n"
+ "-C : find parameters (C for -s 0, 2 and C, p for -s 11)%n"
+ "-q : quiet mode (no outputs)%n");
System.out.print("""
Usage: train [options] training_set_file [model_file]
options:
-s type : set type of solver (default 1)
for multi-class classification
0 -- L2-regularized logistic regression (primal)
1 -- L2-regularized L2-loss support vector classification (dual)
2 -- L2-regularized L2-loss support vector classification (primal)
3 -- L2-regularized L1-loss support vector classification (dual)
4 -- support vector classification by Crammer and Singer
5 -- L1-regularized L2-loss support vector classification
6 -- L1-regularized logistic regression
7 -- L2-regularized logistic regression (dual)
for regression
11 -- L2-regularized L2-loss support vector regression (primal)
12 -- L2-regularized L2-loss support vector regression (dual)
13 -- L2-regularized L1-loss support vector regression (dual)
for outlier detection
21 -- one-class support vector machine (dual)
-c cost : set the parameter C (default 1)
-p epsilon : set the epsilon in loss function of SVR (default 0.1)
-n nu : set the parameter nu of one-class SVM (default 0.5)
-e epsilon : set tolerance of termination criterion
-s 0 and 2
|f'(w)|_2 <= eps*min(pos,neg)/l*|f'(w0)|_2,
where f is the primal function and pos/neg are # of
positive/negative data (default 0.01)
-s 11
|f'(w)|_2 <= eps*|f'(w0)|_2 (default 0.0001)
-s 1, 3, 4, 7, and 21
Dual maximal violation <= eps; similar to libsvm (default 0.1 except 0.01 for -s 21)
-s 5 and 6
|f'(w)|_1 <= eps*min(pos,neg)/l*|f'(w0)|_1,
where f is the primal function (default 0.01)
-s 12 and 13
|f'(alpha)|_1 <= eps |f'(alpha0)|,
where f is the dual function (default 0.1)
-B bias : if bias >= 0, instance x becomes [x; bias]; if < 0, no bias term added (default -1)
-R : not regularize the bias; must with -B 1 to have the bias; DON'T use this unless you know what it is
(for -s 0, 2, 5, 6, 11)
-wi weight: weights adjust the parameter C of different classes (see README for details)
-v n: n-fold cross validation mode
-C : find parameters (C for -s 0, 2 and C, p for -s 11)
-q : quiet mode (no outputs)
""".replace("\n", System.lineSeparator()));
System.exit(1);
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/de/bwaldvogel/liblinear/LinearTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,10 @@ void testLoadIllegalModel(@TempDir Path tempDir) throws Exception {
"bias -1.0",
"w",
"0.1 0.2 0.3 ",
"0.4 0.5 " + repeat("0", 1024));
"0.4 0.5 " + "0".repeat(1024));
writeToFile(file, lines);

String x = repeat("0", 128);
String x = "0".repeat(128);

assertThatExceptionOfType(RuntimeException.class)
.isThrownBy(() -> Model.load(file))
Expand Down
8 changes: 0 additions & 8 deletions src/test/java/de/bwaldvogel/liblinear/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,4 @@ static void writeToFile(Path file, List<String> lines) throws IOException {
bufferedWriter.flush();
}
}

static String repeat(String stringToRepeat, int numTimes) {
StringBuilder longString = new StringBuilder();
for (int i = 0; i < numTimes; i++) {
longString.append(stringToRepeat);
}
return longString.toString();
}
}
Loading