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

SVC gamma value of 'auto' crashes when export_data=True #27

Closed lichard49 closed 6 years ago

lichard49 commented 6 years ago

For SVC's, the default value of gamma is 'auto' which causes porter to crash when using exporting data is true.

>>> output = porter.export(export_data=True) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.7/dist-packages/sklearn_porter/Porter.py", line 189, in export method_name=method_name, **kwargs) File "/usr/local/lib/python2.7/dist-packages/sklearn_porter/estimator/classifier/SVC/__init__.py", line 192, in export export_append_checksum) File "/usr/local/lib/python2.7/dist-packages/sklearn_porter/estimator/classifier/SVC/__init__.py", line 239, in export_data 'gamma': float(self.gamma), ValueError: could not convert string to float: auto

The solution is to check if it's equal to 'auto'; if so, then set gamma equal to 1/n_features, such as:

>>> clf.gamma 'auto' >>> clf.gamma = 1/float(clf.support_vectors_.shape[1]) >>> output = porter.export(export_data=True) # success

nok commented 6 years ago

Hello @lichard49 ,

thanks for the hint. It's fixed with the last commit (https://github.com/nok/sklearn-porter/commit/972d498e469a648e206deca815bfdd584ebe9e26) on the master branch and added to the next release branch 0.7.0. For now you can use the following commands to install the fixed version:

$ pip uninstall -y sklearn-porter
$ pip install --no-cache-dir https://github.com/nok/sklearn-porter/zipball/master