onnx / sklearn-onnx

Convert scikit-learn models and pipelines to ONNX
Apache License 2.0
555 stars 104 forks source link

sklearn float64 input type not supported? #558

Closed useric closed 2 years ago

useric commented 4 years ago

` from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestRegressor from sklearn.datasets import make_regression from sklearn.linear_model import Ridge, LinearRegression, Lasso

X,y=make_regression(n_samples=200, n_features=5,n_targets=1,noise=1.5,random_state=1)

X_train, X_test, y_train, y_test = train_test_split(X, y) clr = LinearRegression() clr.fit(X_train, y_train)

Convert into ONNX format

from skl2onnx import convert_sklearn, to_onnx from skl2onnx.common.data_types import FloatTensorType, DoubleTensorType

initial_type = [('input_features', DoubleTensorType([None, 5]))] onx = convert_sklearn(clr, initial_types=initial_type)

with open("reg_model.onnx", "wb") as f: f.write(onx.SerializeToString())

Compute the prediction with ONNX Runtime

import onnxruntime as rt import numpy sess = rt.InferenceSession("reg_model.onnx") input_name = sess.get_inputs()[0].name label_name = sess.get_outputs()[0].name pred_onx = sess.run(None, {input_name: X_test.astype(numpy.float64)})

`

ERROR:
NotImplemented: [ONNXRuntimeError] : 9 : NOT_IMPLEMENTED : Could not find an implementation for the node LinearRegressor:LinearRegressor(1)

if change to FloatTensorType and numpy.float32, everything would be ok.

Any suggestions to use numpy.float64 and DoubleTensorType?

Thanks

xadupre commented 4 years ago

Double is not supported for all models in ONNX specifications. For linear models, this requires a change to use operator MatMul instead of LinearClassifier because this operator only supports float attributes. Float precision is not enough in your case?

useric commented 4 years ago

@xadupre thanks for your response. currently float precision is not fully enough , cause the prediction precision is 1e-6 or 1e-7 . when dump the model to onnx format, i am wondering the prediction precision will be lost...

useric commented 4 years ago

@xadupre btw, you mentioned "Double is not supported for all models in ONNX specifications. " could you give the double supported model list ? Thanks

xadupre commented 4 years ago

I'm working on extending the list: #561.

useric commented 3 years ago

@xadupre Thanks for your support

xadupre commented 3 years ago

I'm working on classifiers now in #564.

useric commented 3 years ago

@xadupre I found sklearn-onnx latest version is 1.7.0. Does this version support float64 now?

useric commented 3 years ago

@xadupre btw, I found in my case, the prediction result difference between onnx model with FloatTensorType and python model would be large after 0.001. Strongly recommend sklearn-onnx support Float64Type.

xadupre commented 3 years ago

This also require some changes in onnxruntime: https://github.com/Microsoft/onnxruntime/pulls?q=is%3Apr+is%3Aopen+%22support+double%22. This is a work in progress.

xadupre commented 3 years ago

Changes have been brought in sklearn-onnx and onnxruntime. This will be available in the next release of both.

xadupre commented 3 years ago

Support for double is a lot better now. Let me know if you face issues with the latest release.

xadupre commented 2 years ago

A lot was done on this topic. I'll close the issue. Feel free to reopen it.