nok / sklearn-porter

Transpile trained scikit-learn estimators to C, Java, JavaScript and others.
BSD 3-Clause "New" or "Revised" License
1.28k stars 170 forks source link

Error when trying to convert RandomForestClassifier to javascript #28

Closed aflugge closed 6 years ago

aflugge commented 6 years ago

Hi,

I am trying to run the command: python -m sklearn_porter -i estimator.pkl --js as instructed on the github readme, with a sklearn random forest classifier that I saved into estimator.pkl as instructed. I am using Python 3.6 from Anaconda on a Ubuntu 16.04 LTS. But it fails with following error: Traceback (most recent call last): File "/home/user/anaconda3/lib/python3.6/runpy.py", line 193, in _run_module_as_main "main", mod_spec) File "/home/user/anaconda3/lib/python3.6/runpy.py", line 85, in _run_code exec(code, run_globals) File "/home/user/anaconda3/lib/python3.6/site-packages/sklearn_porter/main.py", line 153, in main() File "/home/user/anaconda3/lib/python3.6/site-packages/sklearn_porter/main.py", line 105, in main estimator = joblib.load(input_path) File "/home/user/anaconda3/lib/python3.6/site-packages/sklearn/externals/joblib/numpy_pickle.py", line 578, in load obj = _unpickle(fobj, filename, mmap_mode) File "/home/user/anaconda3/lib/python3.6/site-packages/sklearn/externals/joblib/numpy_pickle.py", line 508, in _unpickle obj = unpickler.load() File "/home/user/anaconda3/lib/python3.6/pickle.py", line 1050, in load dispatchkey[0] KeyError: 239

nok commented 6 years ago

Hello @aflugge ,

can you set the compression level to 0? In Python 3.x the default value is 3 I guess.

from sklearn.externals import joblib
# ...
joblib.dump(clf, 'estimator.pkl', compress=0)

Darius

aflugge commented 6 years ago

Thanks for the reply. Setting compress=0 does not seem to make any difference. However, with "from sklearn.externals import joblib" rather than "import joblib" as I have done before, I get the different error message as below:

Traceback (most recent call last): File "/home/user/anaconda3/lib/python3.6/runpy.py", line 193, in _run_module_as_main "main", mod_spec) File "/home/user/anaconda3/lib/python3.6/runpy.py", line 85, in _run_code exec(code, run_globals) File "/home/user/anaconda3/lib/python3.6/site-packages/sklearn_porter/main.py", line 153, in main() File "/home/user/anaconda3/lib/python3.6/site-packages/sklearn_porter/main.py", line 148, in main with open(destpath, 'w') as file: PermissionError: [Errno 13] Permission denied: '/RandomForestClassifier.js'

I did however manage to export my RandomForestClassifier from within Python with porter = Porter(clf, language='js') output = porter.export(embed_data=True) print(output) So that way it seems to work :)

nok commented 6 years ago

Okay, I'm glad that it's working now 😄,

nevertheless, apparently the module hasn't the permission to create a file. Alternatively you can use python -m sklearn_porter -i estimator.pkl --js --pipe > estimator.js to save the output to a file dynamically.

Darius