-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
prediction of leaf ids #14
Comments
I think XGBoost already lets you produce leaf ids. What would be the benefit of having treelite support leaf outputs? The focus of this project is faster prediction performance, and I don't see the point of outputting leaf ids in cases where fast performance is required. I'm not familiar with what you are trying to achieve here, so a little bit of explanation would be appreciated. Thanks! |
Sure, no problem. I'm trying to improve an XGBoost model by doing a linear regression with regularizations. The linear model uses features = the ids of the leafs that XGBoost constructed. Similar to what Facebook was doing with their ads in this paper https://research.fb.com/publications/practical-lessons-from-predicting-clicks-on-ads-at-facebook/ I'm planning doing further pruning and maybe repeat linear regression. Once you do pruning, XGBoost model is gone and you have to create your own Tree structures. XGBoost just gives you initial partitioning. I have my own Tree python class that I currently use. Once I prune my trees, I can make new predictions, predictions of new leaf ids etc. But it's extremely slow. I'm doing predictions through python linked list. I see a great benefit from your package for doing just this. I could use leaf predictions from XGBoost to feed to your trees. However, I'd have to construct shared model files for each tree in the model. So, basically if you could add a functionality that the predict method optionally returns not just the sum of the predictions from all trees, but the whole sequence of the tree predictions in the model, that'd be all I need. For example, if I have 200 trees, return the array of predictions from each of them. |
I've seen papers in the past that uses sparse linear classifiers to prune trees: Is your approach similar to these papers? The facebook paper you linked appears to use random forests to create a non-linear feature transformer, but for a different purpose.
I see now how treelite helps your work here. Treelite has the model builder API with which you can build any arbitrary decision trees. |
Yes, those also seem similar to what I want to do. This idea has been floating around for some time. There is also RuleFit http://statweb.stanford.edu/~jhf/ftp/RuleFit.pdf paper by J.Friedman from 2005. And yes, I was going to use your model builder API. And I should be able to do it even now. The only problem is that with current treelite setup I would have to build my custom model for each tree and make my custom leaf id predictions for each of them separately. |
Ah ha, so if treelite starts supporting leaf id outputs, you could simply use the model builder API in treelite and be done with it. Are you currently satisfied with the current performance of treelite? Only concern for me is how much engineering effort would be necessary to support tree id outputs. It might be easier for me to build a model builder that produces an XGBoost model file, which you'd then feed into XGBoost to get leaf ids. |
And this I can do very easily, because I am quite familiar with XGBoost model format. |
Oh... That's an interesting idea. I think that that would work fine too! |
I don't know yet :) I just found it yesterday and it immediately clicked that this is exactly what I need. So I kept thinking about it all day. But the fact that it's makes an efficient C++ code makes me think that I will be satisfied with the performance. I'll check that on Monday. I'm also interested in the current prediction functionality too. Because I need low latency model evaluations. I also know that my data has no missing values. Can the missing values check be also removed optionally? |
For now, I'll go ahead and add an experimental functionality for exporting XGBoost models. This can be done most easily on my part.
For now, I don't think this is the case. I'll get into more details later, if you are interested how missing values are handled. |
@Denisevi4 I've added the exporting feature to the dev branch # model is of type treelite.Model
model.export_as_xgboost('test.model', name_obj='binary:logistic') (API doc for |
Beautiful! I will give it a try. |
Hm. The exported model predicts zeros. For some reason I can't attach the jupyter notebook file. Maybe this functionality is blocked at my work. But my code looks like this: `from sklearn.datasets import load_boston import xgboost This predicts wellbst.predict(dtrain) import treelite toolchain = 'clang' import treelite.runtime # runtime module batch = treelite.runtime.Batch.from_npy2d(X, rbegin=0, rend=10) This also predicts wellout_pred = predictor.predict(batch, verbose=True) model.export_as_xgboost('test.model', name_obj="reg:linear") import xgboost as xgb This one predicts zerosbst_new.predict(dtrain)` |
json Dump of the initial XGBoost (obtained by bst.get_dump(with_stats=True)): json dump of the exported model: |
However predict with pred_leaf=True option (prediction of node ids) works for both original and exported model and produces same output. So actually for the purpose that I want export_as_xgboost, it already works! It's probably because xgboost need all those cover and gains. I wouldn't know why though. It doesn't make sense to me. And those fields are not stored in treelite. |
Also, I tried using both Python2 and Python3. For compatibility I had to modify the code a little bit because subprocess in Python2 doesn't have DEVNULL in python/treelitecontrib/gcc.py: ` def _openmp_supported(toolchain): And same thing in in python/treelitecontrib/util.py: def _is_windows(): def _toolchain_exist_check(toolchain):
` |
Previously, tree_info vector was set to -1, which caused the issue #14 (comment)
I've pushed a small commit that fixes this problem. Thanks!
This is an oversight on my part. Let me write a fix shortly. |
As reported in #14 (comment), Python 2.7 does not have subprocess.DEVNULL. Use os.devnull instead.
As reported in #14 (comment), Python 2.7 does not have subprocess.DEVNULL. Use os.devnull instead. Release a postfix wheels to remedy this problem
@Denisevi4 The fixed package (0.31.post2) is now available on PyPI. Let me know if there's any other problem. |
It works now! I don't know what changed, because the json dump is identical to what I had before when it was predicting zeros. Thanks a lot! This is going to be very useful. Do you want to merge it to master? |
One more question/suggestion. Is it necessary to save the xgboost model to a file in model.export_as_xgboost? Could you instead return just the XGBoost model instead? And then I could save it to a file if I wanted to. In my pruning procedure that I'm planning, all those xgboost models that I construct are temporary. They don't need to be saved. All I need the model for is to predict the leaf ids. Thus, saving to disk and then immediately reading the models from files is an unnecessary operation. |
@Denisevi4 I've gone ahead and merged the feature into the master. (Keep in mind though that this is an experimental feature, so we won't provide any guarantee about its stability in the future.) If you run into an issue, feel free to open another issue post. As for returning XGBoost handles, let me get back to it later. Saving to the disk was easier, so that was what I ended up doing. |
Awesome project! Thanks!
Could you also add a prediction of leaf ids in each tree.
For instance if I have 10 trees in the model, then for each event I would get a vector of length 10 with ids for each of the tree.
These function is needed if one wants to get just the partition info about each event.
The text was updated successfully, but these errors were encountered: