-
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
sklearn binary classifier: compiled and Python model discrepancies? #87
Comments
@blokhin Is it possible to post your model for debugging purposes? If not, can you make a toy example to demonstrate the issue? |
@blokhin Any updates? |
@hcho3 I figured out the compiled random forest binary classifier outputs not the 0 vs. 1 (as expected), but the float in the range between 0 and 1. Not a big deal, as this float can be correctly rounded then. That was actually a reason of discrepancy. |
@blokhin Treelite will produce a number between 0 and 1, representing the fraction of the votes among the decision trees for the positive class (e.g. 0.75 means 75% of the trees predicted the positive class). |
This is a sample code to reproduce: import random
import numpy as np
from sklearn.ensemble import RandomForestClassifier
import treelite.gallery.sklearn
import treelite.runtime
# First, generate sample data
my_range = range(1, 100)
X_data = [[random.choice(my_range) for _ in range(125)] for _ in range(76)]
X_test = X_data.pop()
y_data = [random.choice([0, 1]) for _ in range(75)]
# Second, prepare a classifier and compile it
model_py = RandomForestClassifier(
n_estimators=100,
max_features=2,
max_depth=None,
min_samples_split=2,
min_samples_leaf=5,
bootstrap=True,
n_jobs=-1
)
model_py.fit(X_data, y_data)
model_file = './compiled.so'
icompiler = treelite.gallery.sklearn.import_model(model_py)
icompiler.export_lib(toolchain='clang', libpath=model_file, verbose=True, params={'parallel_comp': 8})
model_tr = treelite.runtime.Predictor(model_file, verbose=True)
# Third, compare: py vs. tr
result_py = model_py.predict([X_test])[0]
batch = treelite.runtime.Batch.from_npy2d(np.array([X_test]))
result_tr = model_tr.predict(batch)
print(result_py, result_tr)
assert result_py == result_tr # I would expect that, but... |
@blokhin Also, try using |
OK, got it! |
Dear treelite community,
what could be the reasons of discrepancies in predictions of a sklearn random forest binary classifier, between the original Python code and the compiled code?
I used gallery interface for compilation and treelite.runtime for deployment, if that may matter.
PS thanks for treelite, I really enjoy 10-15x speedup in my compiled regression models (they work just fine)
The text was updated successfully, but these errors were encountered: