.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_tutorial/plot_gbegin_cst.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_gbegin_cst.py: Store arrays in one onnx graph ============================== Once a model is converted it can be useful to store an array as a constant in the graph an retrieve it through an output. This allows the user to store training parameters or other informations like a vocabulary. Last sections shows how to remove an output or to promote an intermediate result to an output. .. contents:: :local: Train and convert a model +++++++++++++++++++++++++ We download one model from the :epkg:`ONNX Zoo` but the model could be trained and produced by another converter library. .. GENERATED FROM PYTHON SOURCE LINES 23-45 .. code-block:: default import pprint import numpy from onnx import load from onnxruntime import InferenceSession from sklearn.datasets import load_iris from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split from skl2onnx import to_onnx from skl2onnx.helpers.onnx_helper import ( add_output_initializer, select_model_inputs_outputs) data = load_iris() X, y = data.data.astype(numpy.float32), data.target X_train, X_test, y_train, y_test = train_test_split(X, y) model = LogisticRegression(penalty='elasticnet', C=2., solver='saga', l1_ratio=0.5) model.fit(X_train, y_train) onx = to_onnx(model, X_train[:1], target_opset=12, options={'zipmap': False}) .. rst-class:: sphx-glr-script-out .. code-block:: none somewhere/.local/lib/python3.9/site-packages/sklearn/linear_model/_sag.py:350: ConvergenceWarning: The max_iter was reached which means the coef_ did not converge warnings.warn( .. GENERATED FROM PYTHON SOURCE LINES 46-49 Add training parameter ++++++++++++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 49-55 .. code-block:: default new_onx = add_output_initializer( onx, ['C', 'l1_ratio'], [numpy.array([model.C]), numpy.array([model.l1_ratio])]) .. GENERATED FROM PYTHON SOURCE LINES 56-58 Inference +++++++++ .. GENERATED FROM PYTHON SOURCE LINES 58-66 .. code-block:: default sess = InferenceSession(new_onx.SerializeToString()) print("output names:", [o.name for o in sess.get_outputs()]) res = sess.run(None, {'X': X_test[:2]}) print("outputs") pprint.pprint(res) .. rst-class:: sphx-glr-script-out .. code-block:: none output names: ['label', 'probabilities', 'C', 'l1_ratio'] outputs [array([0, 1], dtype=int64), array([[9.7480935e-01, 2.5190569e-02, 8.4414566e-08], [4.1084783e-03, 5.2555680e-01, 4.7033474e-01]], dtype=float32), array([2.]), array([0.5])] .. GENERATED FROM PYTHON SOURCE LINES 67-77 The major draw back of this solution is increase the prediction time as onnxruntime copies the constants for every prediction. It is possible either to store those constant in a separate ONNX graph or to removes them. Select outputs ++++++++++++++ Next function removes unneeded outputs from a model, not only the constants. Next model only keeps the probabilities. .. GENERATED FROM PYTHON SOURCE LINES 77-89 .. code-block:: default simple_onx = select_model_inputs_outputs(new_onx, ['probabilities']) sess = InferenceSession(simple_onx.SerializeToString()) print("output names:", [o.name for o in sess.get_outputs()]) res = sess.run(None, {'X': X_test[:2]}) print("outputs") pprint.pprint(res) # Function *select_model_inputs_outputs* add also promote an intermediate # result to an output. # .. rst-class:: sphx-glr-script-out .. code-block:: none output names: ['probabilities'] outputs [array([[9.7480935e-01, 2.5190569e-02, 8.4414566e-08], [4.1084783e-03, 5.2555680e-01, 4.7033474e-01]], dtype=float32)] .. GENERATED FROM PYTHON SOURCE LINES 90-95 This example only uses ONNX graph in memory and never saves or loads a model. This can be done by using the following snippets of code. Save a model ++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 95-99 .. code-block:: default with open("simplified_model.onnx", "wb") as f: f.write(simple_onx.SerializeToString()) .. GENERATED FROM PYTHON SOURCE LINES 100-102 Load a model ++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 102-111 .. code-block:: default model = load("simplified_model.onnx", "wb") sess = InferenceSession(model.SerializeToString()) print("output names:", [o.name for o in sess.get_outputs()]) res = sess.run(None, {'X': X_test[:2]}) print("outputs") pprint.pprint(res) .. rst-class:: sphx-glr-script-out .. code-block:: none output names: ['probabilities'] outputs [array([[9.7480935e-01, 2.5190569e-02, 8.4414566e-08], [4.1084783e-03, 5.2555680e-01, 4.7033474e-01]], dtype=float32)] .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.173 seconds) .. _sphx_glr_download_auto_tutorial_plot_gbegin_cst.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_gbegin_cst.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_gbegin_cst.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_