.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples_sklearn/plot_sklearn_with_sklearn_onnx.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_examples_sklearn_plot_sklearn_with_sklearn_onnx.py: .. _l-plot-sklearn-with-sklearn-onnx: Using sklearn-onnx to convert any scikit-learn estimator ========================================================= :func:`yobx.sklearn.to_onnx` ships with built-in converters for a curated set of :epkg:`scikit-learn` estimators (``StandardScaler``, ``LogisticRegression``, ``DecisionTree*``, ``MLP*``, and ``Pipeline``). For any estimator that is *not* covered by the built-in registry, you can supply your own converter through the ``extra_converters`` keyword argument. :func:`~yobx.sklearn.wrap_skl2onnx_converter` makes the conversion from :epkg:`sklearn-onnx`. One line of setup after looking up the skl2onnx converter function. .. GENERATED FROM PYTHON SOURCE LINES 17-25 .. code-block:: Python import numpy as np import onnxruntime from sklearn.neural_network import MLPClassifier from yobx import doc from yobx.sklearn import to_onnx, wrap_skl2onnx_converter .. GENERATED FROM PYTHON SOURCE LINES 26-30 Converter made for skl2onnx ---------------------------------- We take it from :epkg:`sklearn-onnx`. .. GENERATED FROM PYTHON SOURCE LINES 30-36 .. code-block:: Python import skl2onnx convert_sklearn_mlp_classifier = ( skl2onnx.operator_converters.multilayer_perceptron.convert_sklearn_mlp_classifier ) .. GENERATED FROM PYTHON SOURCE LINES 37-42 Convert to ONNX using the factory helper ---------------------------------------- :func:`~yobx.sklearn.wrap_skl2onnx_converter` makes the function look like a converter for `yobx`.` .. GENERATED FROM PYTHON SOURCE LINES 42-61 .. code-block:: Python X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]], dtype=np.float32) y = np.array([0, 0, 1, 1]) mlp = MLPClassifier(hidden_layer_sizes=(4,), activation="relu", max_iter=2000) mlp.fit(X, y) converter = wrap_skl2onnx_converter(convert_sklearn_mlp_classifier) artifact = to_onnx(mlp, (X,), extra_converters={MLPClassifier: converter}) ref_b = onnxruntime.InferenceSession( artifact.SerializeToString(), providers=["CPUExecutionProvider"] ) results_b = ref_b.run(None, {"X": X}) label_b, proba_b = results_b[0], results_b[1] np.testing.assert_array_equal(mlp.predict(X), label_b) np.testing.assert_allclose(mlp.predict_proba(X), proba_b, atol=1e-5) print("Predictions match ✓") .. rst-class:: sphx-glr-script-out .. code-block:: none Predictions match ✓ .. GENERATED FROM PYTHON SOURCE LINES 62-68 4. Visualise the exported ONNX graph ------------------------------------- :func:`yobx.doc.plot_dot` renders the :class:`onnx.ModelProto` as a directed graph using Graphviz. The nodes emitted by the skl2onnx converter are injected directly into the enclosing graph. .. GENERATED FROM PYTHON SOURCE LINES 68-70 .. code-block:: Python doc.plot_dot(artifact) .. image-sg:: /auto_examples_sklearn/images/sphx_glr_plot_sklearn_with_sklearn_onnx_001.png :alt: plot sklearn with sklearn onnx :srcset: /auto_examples_sklearn/images/sphx_glr_plot_sklearn_with_sklearn_onnx_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.385 seconds) .. _sphx_glr_download_auto_examples_sklearn_plot_sklearn_with_sklearn_onnx.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_sklearn_with_sklearn_onnx.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_sklearn_with_sklearn_onnx.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_sklearn_with_sklearn_onnx.zip ` .. include:: plot_sklearn_with_sklearn_onnx.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_