.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_output_onnx_single_probability.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_output_onnx_single_probability.py: Append onnx nodes to the converted model ======================================== This example show how to append some onnx nodes to the converted model to produce the desired output. In this case, it removes the second column of the output probabilies. To be completly accurate, most of the code was generated using a LLM and modified to accomodate with the latest changes. .. GENERATED FROM PYTHON SOURCE LINES 12-27 .. code-block:: Python from sklearn.datasets import load_iris from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split from skl2onnx import convert_sklearn from skl2onnx.common.data_types import FloatTensorType import onnx iris = load_iris() X, y = iris.data, iris.target X_train, X_test, y_train, y_test = train_test_split(X, y) clr = LogisticRegression(max_iter=500) clr.fit(X_train, y_train) .. raw:: html
LogisticRegression(max_iter=500)
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 28-29 model_to_convert refers to the scikit-learn classifier to convert. .. GENERATED FROM PYTHON SOURCE LINES 29-32 .. code-block:: Python model_to_convert = clr # model to convert X_test = X_test[:1] # data used to test or train, one row is enough .. GENERATED FROM PYTHON SOURCE LINES 33-34 Set the output filename for the modified ONNX model .. GENERATED FROM PYTHON SOURCE LINES 34-36 .. code-block:: Python output_filename = "output_file.onnx" # Replace with your desired output filename .. GENERATED FROM PYTHON SOURCE LINES 37-48 Step 1: Convert the model to ONNX format, disabling the output of labels. Define the input type for the ONNX model. The input type is a float tensor with shape [None, X_test.shape[1]], where None indicates that the number of input samples can be flexible, and X_test.shape[1] is the number of features for each input sample. A "tensor" is essentially a multi-dimensional array, commonly used in machine learning to represent data. A "float tensor" specifically contains floating-point numbers, which are numbers with decimals. .. GENERATED FROM PYTHON SOURCE LINES 48-50 .. code-block:: Python initial_type = [("float_input", FloatTensorType([None, X_test.shape[1]]))] .. GENERATED FROM PYTHON SOURCE LINES 51-62 Convert the model to ONNX format. - target_opset=18 specifies the version of ONNX operators to use. - options={...} sets parameters for the conversion: - "zipmap": False ensures that the output is a raw array - of probabilities instead of a dictionary. - "output_class_labels": False ensures that the output contains only probabilities, not class labels. ONNX (Open Neural Network Exchange) is an open format for representing machine learning models. It allows interoperability between different machine learning frameworks, enabling the use of models across various platforms. .. GENERATED FROM PYTHON SOURCE LINES 62-71 .. code-block:: Python onx = convert_sklearn( model_to_convert, initial_types=initial_type, target_opset={"": 18, "ai.onnx.ml": 3}, options={ id(model_to_convert): {"zipmap": False, "output_class_labels": False} }, # Ensures the output is only probabilities, not labels ) .. GENERATED FROM PYTHON SOURCE LINES 72-76 Step 2: Load the ONNX model for further modifications if needed Load the ONNX model from the serialized string representation. An ONNX file is essentially a serialized representation of a machine learning model that can be shared and used across different systems. .. GENERATED FROM PYTHON SOURCE LINES 76-78 .. code-block:: Python onnx_model = onnx.load_model_from_string(onx.SerializeToString()) .. GENERATED FROM PYTHON SOURCE LINES 79-82 Assuming the first output in this model should be the probability tensor Extract the name of the output tensor representing the probabilities. If there are multiple outputs, select the second one, otherwise, select the first. .. GENERATED FROM PYTHON SOURCE LINES 82-88 .. code-block:: Python prob_output_name = ( onnx_model.graph.output[1].name if len(onnx_model.graph.output) > 1 else onnx_model.graph.output[0].name ) .. GENERATED FROM PYTHON SOURCE LINES 89-93 Add a Gather node to extract only the probability of the positive class (index 1) Create a tensor to specify the index to gather (index 1), which represents the positive class. .. GENERATED FROM PYTHON SOURCE LINES 93-97 .. code-block:: Python indices = onnx.helper.make_tensor( "indices", onnx.TensorProto.INT64, (1,), [1] ) # Index 1 to gather positive class .. GENERATED FROM PYTHON SOURCE LINES 98-105 Create a "Gather" node in the ONNX graph to extract the probability of the positive class. - inputs: [prob_output_name, "indices"] specify the inputs to this node (probability tensor and index tensor). - outputs: ["positive_class_prob"] specify the name of the output of this node. - axis=1 indicates gathering along the columns (features) of the probability tensor. A "Gather" node is used to extract specific elements from a tensor. Here, it extracts the probability for the positive class. .. GENERATED FROM PYTHON SOURCE LINES 105-112 .. code-block:: Python gather_node = onnx.helper.make_node( "Gather", inputs=[prob_output_name, "indices"], outputs=["positive_class_prob"], axis=1, # Gather along columns (axis 1) ) .. GENERATED FROM PYTHON SOURCE LINES 113-114 Add the Gather node to the ONNX graph .. GENERATED FROM PYTHON SOURCE LINES 114-116 .. code-block:: Python onnx_model.graph.node.append(gather_node) .. rst-class:: sphx-glr-script-out .. code-block:: none input: "probabilities" input: "indices" output: "positive_class_prob" op_type: "Gather" attribute { name: "axis" i: 1 type: INT } .. GENERATED FROM PYTHON SOURCE LINES 117-119 Add the tensor initializer for indices (needed for the Gather node) Initializers in ONNX are used to define constant tensors that are used in the computation. .. GENERATED FROM PYTHON SOURCE LINES 119-121 .. code-block:: Python onnx_model.graph.initializer.append(indices) .. rst-class:: sphx-glr-script-out .. code-block:: none dims: 1 data_type: 7 int64_data: 1 name: "indices" .. GENERATED FROM PYTHON SOURCE LINES 122-124 Remove existing outputs and add only the new output for the positive class probability Clear the existing output definitions to replace them with the new output. .. GENERATED FROM PYTHON SOURCE LINES 124-126 .. code-block:: Python del onnx_model.graph.output[:] .. GENERATED FROM PYTHON SOURCE LINES 127-129 Define new output for the positive class probability Create a new output tensor specification with the name "positive_class_prob". .. GENERATED FROM PYTHON SOURCE LINES 129-134 .. code-block:: Python positive_class_output = onnx.helper.make_tensor_value_info( "positive_class_prob", onnx.TensorProto.FLOAT, [None, 1] ) onnx_model.graph.output.append(positive_class_output) .. rst-class:: sphx-glr-script-out .. code-block:: none name: "positive_class_prob" type { tensor_type { elem_type: 1 shape { dim { } dim { dim_value: 1 } } } } .. GENERATED FROM PYTHON SOURCE LINES 135-139 Step 3: Save the modified ONNX model Save the modified ONNX model to the specified output filename. The resulting ONNX file can then be loaded and used in different environments that support ONNX, such as inference servers or other machine learning frameworks. .. GENERATED FROM PYTHON SOURCE LINES 139-142 .. code-block:: Python onnx.save(onnx_model, output_filename) .. GENERATED FROM PYTHON SOURCE LINES 143-144 The model can be printed as follows. .. GENERATED FROM PYTHON SOURCE LINES 144-145 .. code-block:: Python print(onnx.printer.to_text(onnx_model)) .. rst-class:: sphx-glr-script-out .. code-block:: none < ir_version: 8, opset_import: ["ai.onnx.ml" : 1, "" : 18], producer_name: "skl2onnx", producer_version: "1.18.0", domain: "ai.onnx", model_version: 0, doc_string: "" > "2a0b430c8e794cac873666482db85939" (float[?,4] float_input) => (float[?,1] positive_class_prob) { [LinearClassifier] label, probability_tensor = ai.onnx.ml.LinearClassifier (float_input) [Normalizer] probabilities = ai.onnx.ml.Normalizer (probability_tensor) positive_class_prob = Gather (probabilities, indices) } .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.020 seconds) .. _sphx_glr_download_auto_examples_plot_output_onnx_single_probability.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_output_onnx_single_probability.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_output_onnx_single_probability.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_output_onnx_single_probability.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_