|
| 1 | +package org.lightgbm.predict4j; |
| 2 | + |
| 3 | +import java.io.FileInputStream; |
| 4 | +import java.io.FileNotFoundException; |
| 5 | +import java.io.IOException; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import org.apache.commons.io.IOUtils; |
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
| 13 | + |
| 14 | +/** |
| 15 | + * @author lyg5623 |
| 16 | + */ |
| 17 | +public class Application { |
| 18 | + private static final Logger logger = LoggerFactory.getLogger(Application.class); |
| 19 | + private OverallConfig config = new OverallConfig(); |
| 20 | + |
| 21 | + public static void main(String[] args) throws FileNotFoundException, IOException { |
| 22 | + args = "config=cluster_test.conf".split("\\s+"); |
| 23 | + String modelPath = "LightGBM_model.txt"; |
| 24 | + Application app = new Application(args); |
| 25 | + String dataPath = "lightgbm_test.txt"; |
| 26 | + String outputPath = "LightGBM_predict_result.txt"; |
| 27 | + app.run(modelPath, dataPath, outputPath); |
| 28 | + } |
| 29 | + |
| 30 | + public Application(String[] argv) throws FileNotFoundException, IOException { |
| 31 | + loadParameters(argv); |
| 32 | + } |
| 33 | + |
| 34 | + private void loadParameters(String[] argv) throws FileNotFoundException, IOException { |
| 35 | + Map<String, String> params = new HashMap<String, String>(); |
| 36 | + for (int i = 0; i < argv.length; ++i) { |
| 37 | + String[] tmp_strs = argv[i].split("="); |
| 38 | + if (tmp_strs.length == 2) { |
| 39 | + String key = tmp_strs[0].trim(); |
| 40 | + String value = tmp_strs[1].trim(); |
| 41 | + if (key.length() <= 0) { |
| 42 | + continue; |
| 43 | + } |
| 44 | + params.put(key, value); |
| 45 | + } else { |
| 46 | + logger.warn(String.format("Unknown parameter in command line: %s", argv[i])); |
| 47 | + } |
| 48 | + } |
| 49 | + // check for alias |
| 50 | + ParameterAlias.keyAliasTransform(params); |
| 51 | + // read parameters from config file |
| 52 | + if (params.containsKey("config_file")) { |
| 53 | + List<String> lines = IOUtils.readLines(new FileInputStream(params.get("config_file"))); |
| 54 | + if (lines != null) { |
| 55 | + for (String line : lines) { |
| 56 | + line = line.trim(); |
| 57 | + // remove str after "#" |
| 58 | + if (line.startsWith("#")) |
| 59 | + continue; |
| 60 | + if (line.length() == 0) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + String[] tmp_strs = line.split("="); |
| 64 | + if (tmp_strs.length == 2) { |
| 65 | + String key = tmp_strs[0].trim(); |
| 66 | + String value = tmp_strs[1].trim(); |
| 67 | + if (key.length() <= 0) { |
| 68 | + continue; |
| 69 | + } |
| 70 | + // Command-line has higher priority |
| 71 | + if (!params.containsKey(key)) |
| 72 | + params.put(key, value); |
| 73 | + } else { |
| 74 | + logger.warn("Unknown parameter in config file: " + line); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + // check for alias again |
| 80 | + ParameterAlias.keyAliasTransform(params); |
| 81 | + // load configs |
| 82 | + config.set(params); |
| 83 | + logger.info("Finished loading parameters"); |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + private void run(String modelPath, String dataPath, String outputPath) throws FileNotFoundException, IOException { |
| 88 | + Boosting boosting = initPredict(modelPath); |
| 89 | + predict(dataPath, outputPath, boosting); |
| 90 | + } |
| 91 | + |
| 92 | + private Boosting initPredict(String modelPath) throws FileNotFoundException, IOException { |
| 93 | + Boosting boosting = Boosting.createBoosting(modelPath); |
| 94 | + logger.info("Finished initializing prediction"); |
| 95 | + return boosting; |
| 96 | + } |
| 97 | + |
| 98 | + private void predict(String dataPath, String outputPath, Boosting boosting) throws IOException { |
| 99 | + boosting.setNumIterationForPred(config.getIoConfig().getNumIterationPredict()); |
| 100 | + // create predictor |
| 101 | + Predictor predictor = new Predictor(boosting, config.getIoConfig().isPredictRawScore(), |
| 102 | + config.getIoConfig().isPredictLeafIndex()); |
| 103 | + predictor.Predict(dataPath, outputPath); |
| 104 | + logger.info("Finished prediction"); |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments