Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Microsoft Onnxruntime PlotCommonErrors

From Leeroopedia
Revision as of 15:47, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Microsoft_Onnxruntime_PlotCommonErrors.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Documentation, Python, Examples
Last Updated 2026-02-10 04:00 GMT

Overview

A Python example script demonstrating common errors encountered when using ONNX Runtime inference, including type mismatches, misspelled names, and dimension issues.

Description

The `plot_common_errors.py` script is a documentation example (Sphinx gallery format) that demonstrates typical error scenarios when running ONNX models with ONNX Runtime. It loads a pre-trained logistic regression model (`logreg_iris.onnx`) that expects 2D float32 input and illustrates:

1. Bad types: Passing `float64` input instead of `float32` triggers an `InvalidArgument` exception. 2. Misspelled output name: Requesting an output named `"misspelled"` instead of the actual output name raises an error. 3. None output: Passing `None` for output names returns all outputs (valid usage). 4. Misspelled input name: Using `"misspelled"` as an input feed key raises an error. 5. Dimension mismatches: Tests various input shapes including:

  - Shapes that are multiples of expected dimensions (may not fail).
  - Shapes with mismatched feature counts (3 instead of 2).
  - Higher-dimensional inputs (3D arrays).
  The script shows that ONNX Runtime may accept some unexpected shapes silently (e.g., `[4]` reshaped as `[2,2]`) while rejecting others (e.g., `[3]` which is not divisible by 2).

Each error case is wrapped in try/except blocks that print the exception type and message.

Usage

Use this script as a reference for understanding ONNX Runtime input validation behavior. It is rendered as part of the ONNX Runtime Python documentation via Sphinx gallery.

Code Reference

Source Location

Signature

import numpy
import onnxruntime as rt
from onnxruntime.capi.onnxruntime_pybind11_state import InvalidArgument
from onnxruntime.datasets import get_example

example2 = get_example("logreg_iris.onnx")
sess = rt.InferenceSession(example2, providers=rt.get_available_providers())

input_name = sess.get_inputs()[0].name
output_name = sess.get_outputs()[0].name

Import

import numpy
import onnxruntime as rt
from onnxruntime.capi.onnxruntime_pybind11_state import InvalidArgument
from onnxruntime.datasets import get_example

I/O Contract

Scenario Input Expected Behavior Description
Bad type float64 array InvalidArgument exception ONNX Runtime requires float32
Misspelled output "misspelled" Exception Output name not found in model
None output None Returns all outputs Valid; returns list of all model outputs
Misspelled input "misspelled" feed key Exception Input name not found in model
Wrong dimensions shape [1,3] or [3] May raise RuntimeError Feature count mismatch (3 vs expected 2)
Extra dimensions shape [1,1,2] May succeed with warning Higher rank input than expected

Usage Examples

import numpy
import onnxruntime as rt

# Load model
sess = rt.InferenceSession("logreg_iris.onnx", providers=rt.get_available_providers())
input_name = sess.get_inputs()[0].name

# Correct usage: float32, shape (N, 2)
x = numpy.array([[1.0, 2.0], [3.0, 4.0]], dtype=numpy.float32)
result = sess.run(None, {input_name: x})

# Common error: wrong dtype
try:
    x_bad = numpy.array([[1.0, 2.0]], dtype=numpy.float64)
    sess.run(None, {input_name: x_bad})
except Exception as e:
    print(f"Error: {e}")

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment