.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_black_op.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_plot_black_op.py: .. _l-black-op: Convert a model with a reduced list of operators ================================================ Some runtime dedicated to onnx do not implement all the operators and a converted model may not run if one of them is missing from the list of available operators. Some converters may convert a model in different ways if the users wants to blacklist some operators. GaussianMixture +++++++++++++++ The first converter to change its behaviour depending on a black list of operators is for model *GaussianMixture*. .. GENERATED FROM PYTHON SOURCE LINES 22-42 .. code-block:: Python import onnxruntime import onnx import numpy import os from timeit import timeit import numpy as np import matplotlib.pyplot as plt from onnx.tools.net_drawer import GetPydotGraph, GetOpNodeProducer from onnxruntime import InferenceSession from sklearn.mixture import GaussianMixture from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from skl2onnx import to_onnx data = load_iris() X_train, X_test = train_test_split(data.data) model = GaussianMixture() model.fit(X_train) .. raw:: html
GaussianMixture()
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 43-45 Default conversion ++++++++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 45-61 .. code-block:: Python model_onnx = to_onnx( model, X_train[:1].astype(np.float32), options={id(model): {"score_samples": True}}, target_opset=12, ) sess = InferenceSession( model_onnx.SerializeToString(), providers=["CPUExecutionProvider"] ) xt = X_test[:5].astype(np.float32) print(model.score_samples(xt)) print(sess.run(None, {"X": xt})[2]) .. rst-class:: sphx-glr-script-out .. code-block:: none [-1.68351474 -1.68463982 -2.27655683 -1.68930875 -3.40608478] [[-1.6835146] [-1.6846399] [-2.276558 ] [-1.6893082] [-3.4060864]] .. GENERATED FROM PYTHON SOURCE LINES 62-63 Display the ONNX graph. .. GENERATED FROM PYTHON SOURCE LINES 63-82 .. code-block:: Python pydot_graph = GetPydotGraph( model_onnx.graph, name=model_onnx.graph.name, rankdir="TB", node_producer=GetOpNodeProducer( "docstring", color="yellow", fillcolor="yellow", style="filled" ), ) pydot_graph.write_dot("mixture.dot") os.system("dot -O -Gdpi=300 -Tpng mixture.dot") image = plt.imread("mixture.dot.png") fig, ax = plt.subplots(figsize=(40, 20)) ax.imshow(image) ax.axis("off") .. image-sg:: /auto_examples/images/sphx_glr_plot_black_op_001.png :alt: plot black op :srcset: /auto_examples/images/sphx_glr_plot_black_op_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (np.float64(-0.5), np.float64(4796.5), np.float64(8425.5), np.float64(-0.5)) .. GENERATED FROM PYTHON SOURCE LINES 83-89 Conversion without ReduceLogSumExp ++++++++++++++++++++++++++++++++++ Parameter *black_op* is used to tell the converter not to use this operator. Let's see what the converter produces in that case. .. GENERATED FROM PYTHON SOURCE LINES 89-105 .. code-block:: Python model_onnx2 = to_onnx( model, X_train[:1].astype(np.float32), options={id(model): {"score_samples": True}}, black_op={"ReduceLogSumExp"}, target_opset=12, ) sess2 = InferenceSession( model_onnx2.SerializeToString(), providers=["CPUExecutionProvider"] ) xt = X_test[:5].astype(np.float32) print(model.score_samples(xt)) print(sess2.run(None, {"X": xt})[2]) .. rst-class:: sphx-glr-script-out .. code-block:: none [-1.68351474 -1.68463982 -2.27655683 -1.68930875 -3.40608478] [[-1.6835146] [-1.6846399] [-2.276558 ] [-1.6893082] [-3.4060864]] .. GENERATED FROM PYTHON SOURCE LINES 106-107 Display the ONNX graph. .. GENERATED FROM PYTHON SOURCE LINES 107-126 .. code-block:: Python pydot_graph = GetPydotGraph( model_onnx2.graph, name=model_onnx2.graph.name, rankdir="TB", node_producer=GetOpNodeProducer( "docstring", color="yellow", fillcolor="yellow", style="filled" ), ) pydot_graph.write_dot("mixture2.dot") os.system("dot -O -Gdpi=300 -Tpng mixture2.dot") image = plt.imread("mixture2.dot.png") fig, ax = plt.subplots(figsize=(40, 20)) ax.imshow(image) ax.axis("off") .. image-sg:: /auto_examples/images/sphx_glr_plot_black_op_002.png :alt: plot black op :srcset: /auto_examples/images/sphx_glr_plot_black_op_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (np.float64(-0.5), np.float64(4921.5), np.float64(13264.5), np.float64(-0.5)) .. GENERATED FROM PYTHON SOURCE LINES 127-129 Processing time +++++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 129-144 .. code-block:: Python print( timeit( stmt="sess.run(None, {'X': xt})", number=10000, globals={"sess": sess, "xt": xt} ) ) print( timeit( stmt="sess2.run(None, {'X': xt})", number=10000, globals={"sess2": sess2, "xt": xt}, ) ) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.19538224400093895 0.236580953001976 .. GENERATED FROM PYTHON SOURCE LINES 145-146 The model using ReduceLogSumExp is much faster. .. GENERATED FROM PYTHON SOURCE LINES 148-155 If the converter cannot convert without... ++++++++++++++++++++++++++++++++++++++++++ Many converters do not consider the white and black lists of operators. If a converter fails to convert without using a blacklisted operator (or only whitelisted operators), *skl2onnx* raises an error. .. GENERATED FROM PYTHON SOURCE LINES 155-168 .. code-block:: Python try: to_onnx( model, X_train[:1].astype(np.float32), options={id(model): {"score_samples": True}}, black_op={"ReduceLogSumExp", "Add"}, target_opset=12, ) except RuntimeError as e: print("Error:", e) .. rst-class:: sphx-glr-script-out .. code-block:: none Error: Operator 'Add' is black listed. .. GENERATED FROM PYTHON SOURCE LINES 169-170 **Versions used for this example** .. GENERATED FROM PYTHON SOURCE LINES 170-180 .. code-block:: Python import sklearn print("numpy:", numpy.__version__) print("scikit-learn:", sklearn.__version__) import skl2onnx print("onnx: ", onnx.__version__) print("onnxruntime: ", onnxruntime.__version__) print("skl2onnx: ", skl2onnx.__version__) .. rst-class:: sphx-glr-script-out .. code-block:: none numpy: 2.2.0 scikit-learn: 1.6.0 onnx: 1.18.0 onnxruntime: 1.21.0+cu126 skl2onnx: 1.18.0 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 12.799 seconds) .. _sphx_glr_download_auto_examples_plot_black_op.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_black_op.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_black_op.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_black_op.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_