# Load and predict with ONNX Runtime and a very simple model — ONNX Runtime 1.14.0 documentation

Note

Click here to download the full example code

# 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.

    import numpy

    import onnxruntime as rt
    from onnxruntime.datasets import get_example

Let’s load a very simple model. The model is available on github onnx…test_sigmoid.

    example1 = get_example("sigmoid.onnx")
    sess = rt.InferenceSession(example1, providers=rt.get_available_providers())

Let’s see the input name and shape.

    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)

    input name x
    input shape [3, 4, 5]
    input type tensor(float)

Let’s see the output name and shape.

    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)

    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).

    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)

    [array([[[0.7074605 , 0.66807246, 0.5468252 , 0.6794102 , 0.72581375],
            [0.7061233 , 0.7108102 , 0.7131539 , 0.5087233 , 0.7157812 ],
            [0.5101798 , 0.6822957 , 0.71132684, 0.63517916, 0.5935693 ],
            [0.6674769 , 0.71915364, 0.6055379 , 0.6265797 , 0.6334329 ]],

           [[0.7060813 , 0.65122193, 0.5852989 , 0.7020965 , 0.5418902 ],
            [0.70865536, 0.7239054 , 0.53950447, 0.6397851 , 0.61991036],
            [0.5108298 , 0.70998025, 0.5768114 , 0.70231366, 0.7083629 ],
            [0.5532729 , 0.6634668 , 0.68702626, 0.53754365, 0.63848865]],

           [[0.5546355 , 0.5326628 , 0.6045945 , 0.72216797, 0.5367474 ],
            [0.54598904, 0.683926  , 0.7086522 , 0.5805207 , 0.6906233 ],
            [0.71620524, 0.6052958 , 0.7310034 , 0.6245417 , 0.6648243 ],
            [0.6229709 , 0.5226055 , 0.67183244, 0.6684347 , 0.57293963]]],
          dtype=float32)]

Total running time of the script: ( 0 minutes 0.007 seconds)

Gallery generated by Sphinx-Gallery