Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Microsoft Onnxruntime InferenceSession Run For Validation

From Leeroopedia


Metadata

Field Value
Implementation Name InferenceSession_Run_For_Validation
Repository Microsoft_Onnxruntime
Source Repository https://github.com/microsoft/onnxruntime
Type API Doc
Language Python
Domain ML_Inference, Model_Conversion
Last Updated 2026-02-10
Workflow Train_Convert_Predict
Pair 4 of 5

Overview

API documentation for using onnxruntime.InferenceSession to validate that an ONNX-converted model produces predictions matching the original scikit-learn model.

API Signature

onnxruntime.InferenceSession(onnx_path).run([label_name], {input_name: X_test})

Compared against:

model.predict(X_test)

Import

import onnxruntime as rt
import numpy
from sklearn.metrics import confusion_matrix

Code Reference

Reference Location
Validation example docs/python/examples/plot_train_convert_predict.py:L66-80

I/O Contract

Inputs

Parameter Type Required Description
ONNX model file str (path) Yes Path to the ONNX model file produced by the conversion step.
Test dataset numpy.ndarray Yes The same test data used to validate the original scikit-learn model, cast to numpy.float32.
Original predictions numpy.ndarray Yes Predictions from the original scikit-learn model for comparison.

Outputs

Output Type Description
ONNX predictions numpy.ndarray Predictions from the ONNX model via session.run().
Confusion matrix numpy.ndarray Comparison matrix confirming predictions match (should be diagonal).

Usage Example

import onnxruntime as rt
import numpy
from sklearn.metrics import confusion_matrix

# Load the converted ONNX model
sess = rt.InferenceSession("logreg_iris.onnx", providers=rt.get_available_providers())

# Get input/output names
input_name = sess.get_inputs()[0].name
label_name = sess.get_outputs()[0].name

# Run inference with ONNX Runtime
pred_onx = sess.run([label_name], {input_name: X_test.astype(numpy.float32)})[0]

# Compare with original sklearn predictions
print(confusion_matrix(pred, pred_onx))

From the source at docs/python/examples/plot_train_convert_predict.py:L66-80:

import onnxruntime as rt

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

print(f"input name='{sess.get_inputs()[0].name}' and shape={sess.get_inputs()[0].shape}")
print(f"output name='{sess.get_outputs()[0].name}' and shape={sess.get_outputs()[0].shape}")

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

import numpy

pred_onx = sess.run([label_name], {input_name: X_test.astype(numpy.float32)})[0]
print(confusion_matrix(pred, pred_onx))

A successful validation produces a diagonal confusion matrix, confirming that every prediction from the ONNX model matches the original scikit-learn prediction.

Related Pages

Page Connections

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