.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gyexamples/plot_dbegin_options_list.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gyexamples_plot_dbegin_options_list.py: Black list operators when converting ==================================== .. index:: black list, white list Some runtimes do not implement a runtime for every available operator in ONNX. The converter does not know that but it is possible to black some operators. Most of the converters do not change their behaviour, they fail if they use a black listed operator, a couple of them produces a different ONNX graph. .. contents:: :local: GaussianMixture +++++++++++++++ The first converter to change its behaviour depending on a black list of operators is for model *GaussianMixture*. .. GENERATED FROM PYTHON SOURCE LINES 23-40 .. code-block:: default from pyquickhelper.helpgen.graphviz_helper import plot_graphviz from mlprodict.onnxrt import OnnxInference from timeit import timeit import numpy 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 TARGET_OPSET = 12 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 41-43 Default conversion ++++++++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 43-56 .. code-block:: default model_onnx = to_onnx( model, X_train[:1].astype(numpy.float32), options={id(model): {'score_samples': True}}, target_opset=TARGET_OPSET) sess = InferenceSession(model_onnx.SerializeToString(), providers=['CPUExecutionProvider']) xt = X_test[:5].astype(numpy.float32) print(model.score_samples(xt)) print(sess.run(None, {'X': xt})[2]) .. rst-class:: sphx-glr-script-out .. code-block:: none [-3.01613963 -1.52957155 -1.45603661 -1.72285316 -4.9982159 ] [[-3.0161407] [-1.5295727] [-1.4560378] [-1.7228539] [-4.9982166]] .. GENERATED FROM PYTHON SOURCE LINES 57-58 Display the ONNX graph. .. GENERATED FROM PYTHON SOURCE LINES 58-65 .. code-block:: default oinf = OnnxInference(model_onnx) ax = plot_graphviz(oinf.to_dot()) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) .. image-sg:: /gyexamples/images/sphx_glr_plot_dbegin_options_list_001.png :alt: plot dbegin options list :srcset: /gyexamples/images/sphx_glr_plot_dbegin_options_list_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 66-72 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 72-85 .. code-block:: default model_onnx2 = to_onnx( model, X_train[:1].astype(numpy.float32), options={id(model): {'score_samples': True}}, black_op={'ReduceLogSumExp'}, target_opset=TARGET_OPSET) sess2 = InferenceSession(model_onnx2.SerializeToString(), providers=['CPUExecutionProvider']) xt = X_test[:5].astype(numpy.float32) print(model.score_samples(xt)) print(sess2.run(None, {'X': xt})[2]) .. rst-class:: sphx-glr-script-out .. code-block:: none [-3.01613963 -1.52957155 -1.45603661 -1.72285316 -4.9982159 ] [[-3.0161407] [-1.5295727] [-1.4560378] [-1.7228539] [-4.9982166]] .. GENERATED FROM PYTHON SOURCE LINES 86-87 Display the ONNX graph. .. GENERATED FROM PYTHON SOURCE LINES 87-94 .. code-block:: default oinf = OnnxInference(model_onnx2) ax = plot_graphviz(oinf.to_dot()) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) .. image-sg:: /gyexamples/images/sphx_glr_plot_dbegin_options_list_002.png :alt: plot dbegin options list :srcset: /gyexamples/images/sphx_glr_plot_dbegin_options_list_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 95-97 Processing time +++++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 97-104 .. code-block:: default 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 1.012789909960702 1.2360701819416136 .. GENERATED FROM PYTHON SOURCE LINES 105-106 The model using ReduceLogSumExp is much faster. .. GENERATED FROM PYTHON SOURCE LINES 108-115 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 115-124 .. code-block:: default try: to_onnx( model, X_train[:1].astype(numpy.float32), options={id(model): {'score_samples': True}}, black_op={'ReduceLogSumExp', 'Add'}, target_opset=TARGET_OPSET) except RuntimeError as e: print('Error:', e) .. rst-class:: sphx-glr-script-out .. code-block:: none Error: Operator 'Add' is black listed. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 3.957 seconds) .. _sphx_glr_download_gyexamples_plot_dbegin_options_list.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_dbegin_options_list.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_dbegin_options_list.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_