.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_tutorial/plot_abegin_convert_pipeline.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_tutorial_plot_abegin_convert_pipeline.py: .. _l-simple-deploy-1: Train and deploy a scikit-learn pipeline ======================================== .. index:: pipeline, deployment This program starts from an example in :epkg:`scikit-learn` documentation: `Plot individual and voting regression predictions `_, converts it into ONNX and finally computes the predictions a different runtime. Training a pipeline +++++++++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 20-51 .. code-block:: Python import numpy from onnxruntime import InferenceSession from sklearn.datasets import load_diabetes from sklearn.ensemble import ( GradientBoostingRegressor, RandomForestRegressor, VotingRegressor, ) from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split from sklearn.pipeline import Pipeline from skl2onnx import to_onnx from onnx.reference import ReferenceEvaluator X, y = load_diabetes(return_X_y=True) X_train, X_test, y_train, y_test = train_test_split(X, y) # Train classifiers reg1 = GradientBoostingRegressor(random_state=1, n_estimators=5) reg2 = RandomForestRegressor(random_state=1, n_estimators=5) reg3 = LinearRegression() ereg = Pipeline( steps=[ ("voting", VotingRegressor([("gb", reg1), ("rf", reg2), ("lr", reg3)])), ] ) ereg.fit(X_train, y_train) .. raw:: html
Pipeline(steps=[('voting',
                     VotingRegressor(estimators=[('gb',
                                                  GradientBoostingRegressor(n_estimators=5,
                                                                            random_state=1)),
                                                 ('rf',
                                                  RandomForestRegressor(n_estimators=5,
                                                                        random_state=1)),
                                                 ('lr', LinearRegression())]))])
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


.. GENERATED FROM PYTHON SOURCE LINES 52-60 Converts the model ++++++++++++++++++ The second argument gives a sample of the data used to train the model. It is used to infer the input type of the ONNX graph. It is converted into single float and ONNX runtimes may not fully support doubles. .. GENERATED FROM PYTHON SOURCE LINES 60-63 .. code-block:: Python onx = to_onnx(ereg, X_train[:1].astype(numpy.float32), target_opset=12) .. GENERATED FROM PYTHON SOURCE LINES 64-68 Prediction with ONNX ++++++++++++++++++++ The first example uses :epkg:`onnxruntime`. .. GENERATED FROM PYTHON SOURCE LINES 68-77 .. code-block:: Python sess = InferenceSession(onx.SerializeToString(), providers=["CPUExecutionProvider"]) pred_ort = sess.run(None, {"X": X_test.astype(numpy.float32)})[0] pred_skl = ereg.predict(X_test.astype(numpy.float32)) print("Onnx Runtime prediction:\n", pred_ort[:5]) print("Sklearn rediction:\n", pred_skl[:5]) .. rst-class:: sphx-glr-script-out .. code-block:: none Onnx Runtime prediction: [[ 95.148315] [236.64844 ] [192.72498 ] [163.4321 ] [159.0243 ]] Sklearn rediction: [ 95.14831426 236.64842262 192.7249677 163.4320918 159.02430011] .. GENERATED FROM PYTHON SOURCE LINES 78-85 .. _l-diff-dicrepencies: Comparison ++++++++++ Before deploying, we need to compare that both *scikit-learn* and *ONNX* return the same predictions. .. GENERATED FROM PYTHON SOURCE LINES 85-96 .. code-block:: Python def diff(p1, p2): p1 = p1.ravel() p2 = p2.ravel() d = numpy.abs(p2 - p1) return d.max(), (d / numpy.abs(p1)).max() print(diff(pred_skl, pred_ort)) .. rst-class:: sphx-glr-script-out .. code-block:: none (np.float64(2.3916485787367492e-05), np.float64(1.4299860951284095e-07)) .. GENERATED FROM PYTHON SOURCE LINES 97-103 It looks good. Biggest errors (absolute and relative) are within the margin error introduced by using floats instead of doubles. We can save the model into ONNX format and compute the same predictions in many platform using :epkg:`onnxruntime`. .. GENERATED FROM PYTHON SOURCE LINES 105-112 Python runtime ++++++++++++++ A python runtime can be used as well to compute the prediction. It is not meant to be used into production (it still relies on python), but it is useful to investigate why the conversion went wrong. .. GENERATED FROM PYTHON SOURCE LINES 112-116 .. code-block:: Python oinf = ReferenceEvaluator(onx) print(oinf) .. rst-class:: sphx-glr-script-out .. code-block:: none ReferenceEvaluator(X) -> variable .. GENERATED FROM PYTHON SOURCE LINES 117-118 It works almost the same way. .. GENERATED FROM PYTHON SOURCE LINES 118-121 .. code-block:: Python pred_pyrt = oinf.run(None, {"X": X_test.astype(numpy.float32)})[0] print(diff(pred_skl, pred_pyrt)) .. rst-class:: sphx-glr-script-out .. code-block:: none (np.float64(2.3916485787367492e-05), np.float64(1.4092690429730078e-07)) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.319 seconds) .. _sphx_glr_download_auto_tutorial_plot_abegin_convert_pipeline.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_abegin_convert_pipeline.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_abegin_convert_pipeline.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_abegin_convert_pipeline.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_