komiya-atsushi / xgboost-predictor-java

Pure Java implementation of XGBoost predictor for online prediction tasks.
Apache License 2.0
339 stars 108 forks source link

does this package support multithreading? #10

Open bryan-woods opened 7 years ago

bryan-woods commented 7 years ago

I know that the XGBoost package itself supports multithreading of the prediction task. Does this predictor implementation also support multithreading? If not, how could I go about adding it?

komiya-atsushi commented 7 years ago

Hi,

This library does not support multithreading yet, but instances of Predictor are thread-safe.

So if you have many FVec objects, you can share and use a Predictor across multiple threads like below:

// Predictor predictor = ...;
// List<SimpleEntry<Integer, FVec>> data = ...;

OptionalDouble logloss = data.parallelStream()
        .mapToDouble(pair -> {
            double predValue = predictor.predictSingle(pair.getValue());
            predValue = Math.min(Math.max(predValue, 1e-15), 1 - 1e-15);
            int actual = pair.getKey();
            return actual * Math.log(predValue) + (1 - actual) * Math.log(1 - predValue);
        })
        .average();

logloss.ifPresent(d -> System.out.println("Logloss (parallel): " + (-d)));