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 Prediction

From Leeroopedia


Metadata

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

Overview

API documentation for using onnxruntime.InferenceSession.run() to perform optimized production inference on a validated ONNX model, retrieving both class labels and probability outputs.

API Signature

session.run([output_name], {input_name: X_test.astype(numpy.float32)})[0]

Import

import onnxruntime as rt
import numpy

Code Reference

Reference Location
Prediction and probability retrieval docs/python/examples/plot_train_convert_predict.py:L79-101

I/O Contract

Inputs

Parameter Type Required Description
InferenceSession rt.InferenceSession Yes A loaded ONNX inference session with the validated model.
Test data numpy.ndarray (float32) Yes Input feature data cast to numpy.float32 matching the model's input specification.

Outputs

Output Type Description
Predictions (labels) numpy.ndarray Predicted class labels as an integer array.
Probabilities numpy.ndarray Probability distributions over classes for each sample.

Usage Example

import onnxruntime as rt
import numpy

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

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

# Get predicted labels
pred = sess.run([label_name], {input_name: X_test.astype(numpy.float32)})[0]

# Get probability distributions
probs = sess.run([prob_name], {input_name: X_test.astype(numpy.float32)})[0]

From the source at docs/python/examples/plot_train_convert_predict.py:L79-101:

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

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

prob_name = sess.get_outputs()[1].name
prob_rt = sess.run([prob_name], {input_name: X_test.astype(numpy.float32)})[0]

Single-Sample Inference (Web Service Pattern)

def sess_predict(x):
    return sess.run([label_name], {input_name: x.astype(numpy.float32)})[0]

def sess_predict_proba(x):
    return sess.run([prob_name], {input_name: x.astype(numpy.float32)})[0]

# Process one sample at a time
for i in range(X_test.shape[0]):
    sample = X_test[i:i+1]
    prediction = sess_predict(sample)
    probability = sess_predict_proba(sample)

Related Pages

Page Connections

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