xgboost icon indicating copy to clipboard operation
xgboost copied to clipboard

[jvm-packages] Support for online prediction using a feature vector

Open nikunjgit opened this issue 10 years ago • 10 comments

Are there any plans to support making a prediction using a feature vector ?

For example, in ml.dmlc.xgboost4j.scala.spark.XGBoostClassificationModel : override protected def predict(features: MLVector): Double = { throw new Exception("XGBoost does not support online prediction ") }

If not, then could you please suggest a workaround ?

nikunjgit avatar Nov 22 '16 07:11 nikunjgit

No, this limitation comes from the native implementation of XGBoost

There is no feasible workaround, unless you can endure the slow performance

CodingCat avatar Nov 22 '16 15:11 CodingCat

Is this because a new Rabit task has to be started for each prediction ? If we bypass that and directly call something like mentioned below, where do you think the slow performance comes from ?

def predict(Iterator testSamples): Array[Array[Float]] { val dMatrix = new DMatrix(new JDMatrix(testSamples, null)) _booster.predict(dMatrix) }

Also, please let me know if there's a better workaround.

nikunjgit avatar Nov 22 '16 17:11 nikunjgit

it's nothing to do with Rabit, just you need to build a DMatrix for EACH feature vector

the code you show above is prediction for MULTIPLE feature vectors

CodingCat avatar Nov 22 '16 17:11 CodingCat

There is no feasible workaround, unless you can endure the slow performance

CodingCat avatar Nov 22 '16 17:11 CodingCat

Thanks for your quick response. In my case, the testSamples would just have size of one. Just so that we are on the same page, are you saying building DMatrix for each prediction is slow ? I'm trying to understand where this slowness is coming from so that I can estimate how slow its going to get. For my use case, 0.5ms per prediction should be reasonable.

nikunjgit avatar Nov 22 '16 18:11 nikunjgit

if you have 10000 test samples and you can build a single DMatrix for prediction

if you have only one test sample, you have to build a DMatrix in any case, then that's fine

if your test samples come one by one, you have to build a DMatrix for each of them, and in total, you have to build 10000 DMatrix which is costly...

CodingCat avatar Nov 22 '16 18:11 CodingCat

Yes, I agree online prediction will be slower if we had a big batch of test samples we need prediction on. However, that's not what we are trying to optimize for. For online prediction, we are looking for a way to get prediction on individual requests within some low latency (~0.5ms). We are not going to have millions of requests which need to be evaluated.

That's why I am trying to understand if there's anything particularly slow in using DMatrix, I can probably test that on my own.

Can you verify whether the above code I had mentioned work ? I'm particularly interested in knowing whether a prediction necessarily needs a Rabit task to be started or can we avoid it if we don't want a distributed mode.

nikunjgit avatar Nov 23 '16 19:11 nikunjgit

that code shall work no Rabit task is necessary

CodingCat avatar Nov 23 '16 19:11 CodingCat

You don't necessarily have to use XGBoost for online prediction. Based on your description, you need to productionize the trained model for low-latency model, and there are multiple ways of doing it without using XGBoost.

After all, the trained model is a gradient boost tree. You can convert the model to PMML, or convert it to a tree ensemble predictor in sklearn, or generate customized Java/Scala code from the model. So long as you find a library that can productionize a gradient boosting model, you should be able to convert the XGBoostModel to some production-ready online prediction code.

xydrolase avatar Nov 24 '16 19:11 xydrolase

If your data has no missing values, then each tree split is simply a if-else branch. You can dump the tree model, and then write a code generator that spout Scala/Java code implementing a gigantic decision tree with branches.

In our case, a tree model with 500 trees, ~80 features and max_depth=8, online prediction in Java code takes about ~50 micro seconds.

xydrolase avatar Nov 24 '16 20:11 xydrolase