.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_load_and_predict.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_load_and_predict.py: .. _l-example-simple-usage: Load and predict with ONNX Runtime and a very simple model ========================================================== This example demonstrates how to load a model and compute the output for an input vector. It also shows how to retrieve the definition of its inputs and outputs. .. GENERATED FROM PYTHON SOURCE LINES 14-20 .. code-block:: Python import numpy import onnxruntime as rt from onnxruntime.datasets import get_example .. GENERATED FROM PYTHON SOURCE LINES 21-23 Let's load a very simple model. The model is available on github `onnx...test_sigmoid `_. .. GENERATED FROM PYTHON SOURCE LINES 23-27 .. code-block:: Python example1 = get_example("sigmoid.onnx") sess = rt.InferenceSession(example1, providers=rt.get_available_providers()) .. GENERATED FROM PYTHON SOURCE LINES 28-29 Let's see the input name and shape. .. GENERATED FROM PYTHON SOURCE LINES 29-37 .. code-block:: Python input_name = sess.get_inputs()[0].name print("input name", input_name) input_shape = sess.get_inputs()[0].shape print("input shape", input_shape) input_type = sess.get_inputs()[0].type print("input type", input_type) .. rst-class:: sphx-glr-script-out .. code-block:: none input name x input shape [3, 4, 5] input type tensor(float) .. GENERATED FROM PYTHON SOURCE LINES 38-39 Let's see the output name and shape. .. GENERATED FROM PYTHON SOURCE LINES 39-47 .. code-block:: Python output_name = sess.get_outputs()[0].name print("output name", output_name) output_shape = sess.get_outputs()[0].shape print("output shape", output_shape) output_type = sess.get_outputs()[0].type print("output type", output_type) .. rst-class:: sphx-glr-script-out .. code-block:: none output name y output shape [3, 4, 5] output type tensor(float) .. GENERATED FROM PYTHON SOURCE LINES 48-49 Let's compute its outputs (or predictions if it is a machine learned model). .. GENERATED FROM PYTHON SOURCE LINES 49-56 .. code-block:: Python import numpy.random # noqa: E402 x = numpy.random.random((3, 4, 5)) x = x.astype(numpy.float32) res = sess.run([output_name], {input_name: x}) print(res) .. rst-class:: sphx-glr-script-out .. code-block:: none [array([[[0.5160057 , 0.7166697 , 0.5675714 , 0.54535687, 0.5339971 ], [0.6529523 , 0.6951029 , 0.5401793 , 0.56377375, 0.6852754 ], [0.5730017 , 0.5257642 , 0.7282217 , 0.61076474, 0.5805669 ], [0.5228421 , 0.6778554 , 0.51807076, 0.52089953, 0.5925686 ]], [[0.6670459 , 0.64841074, 0.6030437 , 0.6005311 , 0.6947219 ], [0.6192975 , 0.65113866, 0.61059296, 0.59591657, 0.57756394], [0.6870088 , 0.5341192 , 0.5685398 , 0.5863307 , 0.5289824 ], [0.5137127 , 0.5811924 , 0.621683 , 0.54564 , 0.6946349 ]], [[0.5791891 , 0.6960951 , 0.66067314, 0.51487327, 0.61670446], [0.6313322 , 0.51381683, 0.52351415, 0.6139147 , 0.5511605 ], [0.5292715 , 0.5560558 , 0.5730416 , 0.6627769 , 0.54993975], [0.5996757 , 0.5937923 , 0.5990792 , 0.5196933 , 0.61731654]]], dtype=float32)] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.007 seconds) .. _sphx_glr_download_auto_examples_plot_load_and_predict.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_load_and_predict.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_load_and_predict.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_load_and_predict.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_