onnxruntime/python/sources/auto_examples/plot_load_and_predict.rst.txt
Xavier Dupré e210853b0c
Update python documentation (rename folders prefixed by _) (#6575)
* Update python documentation

* remove two unnecessary files

Co-authored-by: xavier dupré <xavier.dupre@gmail.com>
2021-02-10 16:34:31 -08:00

184 lines
3.8 KiB
ReStructuredText

.. only:: html
.. note::
:class: sphx-glr-download-link-note
Click :ref:`here <sphx_glr_download_auto_examples_plot_load_and_predict.py>` 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.
.. code-block:: default
import onnxruntime as rt
import numpy
from onnxruntime.datasets import get_example
Let's load a very simple model.
The model is available on github `onnx...test_sigmoid <https://github.com/onnx/onnx/tree/master/onnx/backend/test/data/node/test_sigmoid>`_.
.. code-block:: default
example1 = get_example("sigmoid.onnx")
sess = rt.InferenceSession(example1)
Let's see the input name and shape.
.. code-block:: default
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
Out:
.. code-block:: none
input name x
input shape [3, 4, 5]
input type tensor(float)
Let's see the output name and shape.
.. code-block:: default
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
Out:
.. code-block:: none
output name y
output shape [3, 4, 5]
output type tensor(float)
Let's compute its outputs (or predictions if it is a machine learned model).
.. code-block:: default
import numpy.random
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
Out:
.. code-block:: none
[array([[[0.5723026 , 0.63803464, 0.6668191 , 0.5958905 , 0.6193227 ],
[0.72006834, 0.6733471 , 0.69727564, 0.677417 , 0.54019606],
[0.6529879 , 0.6253395 , 0.6622766 , 0.7127938 , 0.5429604 ],
[0.604758 , 0.7297679 , 0.5023199 , 0.6422848 , 0.72463864]],
[[0.7272017 , 0.6749091 , 0.6320263 , 0.53652936, 0.5730977 ],
[0.5092271 , 0.6188758 , 0.7302063 , 0.6986053 , 0.681966 ],
[0.71297586, 0.5980871 , 0.50415754, 0.5037554 , 0.555519 ],
[0.66070724, 0.5136699 , 0.61995924, 0.62644744, 0.53362054]],
[[0.71763974, 0.6305131 , 0.67285264, 0.61491245, 0.62528753],
[0.6300376 , 0.5060302 , 0.6701227 , 0.6823867 , 0.6090256 ],
[0.6845094 , 0.69262683, 0.5350911 , 0.7162322 , 0.6441792 ],
[0.51676244, 0.6735578 , 0.54448766, 0.64972466, 0.66511655]]],
dtype=float32)]
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 0.050 seconds)
.. _sphx_glr_download_auto_examples_plot_load_and_predict.py:
.. only :: html
.. container:: sphx-glr-footer
:class: sphx-glr-footer-example
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: plot_load_and_predict.py <plot_load_and_predict.py>`
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: plot_load_and_predict.ipynb <plot_load_and_predict.ipynb>`
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_