Learn Onnx - 10 Code Examples & CST Typing Practice Test
ONNX (Open Neural Network Exchange) is an open-source format and ecosystem for representing machine learning models, enabling interoperability between frameworks like PyTorch, TensorFlow, and scikit-learn, and allowing deployment across diverse platforms.
View all 10 Onnx code examples →
Learn ONNX with Real Code Examples
Updated Nov 24, 2025
Code Sample Descriptions
ONNX Model Inference Example
import onnxruntime as ort
import numpy as np
# Load ONNX model
session = ort.InferenceSession('model.onnx')
# Prepare input
input_name = session.get_inputs()[0].name
input_data = np.array([[1.0, 2.0, 3.0, 4.0]], dtype=np.float32)
# Run inference
outputs = session.run(None, {input_name: input_data})
print('Model output:', outputs)
A minimal example loading an ONNX model and performing inference using ONNX Runtime.
ONNX Image Classification Inference
import onnxruntime as ort
import numpy as np
from PIL import Image
# Load image
image = Image.open('image.jpg').resize((224,224))
input_data = np.array(image).astype(np.float32)
input_data = np.expand_dims(input_data, axis=0)
# Load model
session = ort.InferenceSession('resnet50.onnx')
input_name = session.get_inputs()[0].name
# Run inference
outputs = session.run(None, {input_name: input_data})
print('Predicted class:', np.argmax(outputs[0]))
Performing image classification using a pretrained ONNX model.
ONNX Batch Inference Example
import onnxruntime as ort
import numpy as np
# Batch input
batch_input = np.array([[1,2,3,4],[5,6,7,8]], dtype=np.float32)
# Load model
session = ort.InferenceSession('model.onnx')
input_name = session.get_inputs()[0].name
# Run batch inference
outputs = session.run(None, {input_name: batch_input})
print('Batch outputs:', outputs)
Performing inference on a batch of inputs using ONNX Runtime.
ONNX Regression Model Inference
import onnxruntime as ort
import numpy as np
# Sample input
input_data = np.array([[10.0, 20.0, 30.0]], dtype=np.float32)
# Load model
session = ort.InferenceSession('regression_model.onnx')
input_name = session.get_inputs()[0].name
# Run inference
pred = session.run(None, {input_name: input_data})
print('Regression prediction:', pred)
Inference example for a regression ONNX model.
ONNX GPU Inference Example
import onnxruntime as ort
import numpy as np
# Load model on GPU
session = ort.InferenceSession('model.onnx', providers=['CUDAExecutionProvider'])
input_name = session.get_inputs()[0].name
input_data = np.random.rand(1,4).astype(np.float32)
# Run inference
outputs = session.run(None, {input_name: input_data})
print('GPU inference output:', outputs)
Running ONNX model inference on GPU using ONNX Runtime with CUDA provider.
ONNX Multiple Outputs Example
import onnxruntime as ort
import numpy as np
# Load model
session = ort.InferenceSession('multi_output_model.onnx')
input_name = session.get_inputs()[0].name
input_data = np.random.rand(1,10).astype(np.float32)
# Run inference
outputs = session.run(None, {input_name: input_data})
print('Output 1:', outputs[0])
print('Output 2:', outputs[1])
Running inference for an ONNX model with multiple outputs.
ONNX Dynamic Input Shape Example
import onnxruntime as ort
import numpy as np
# Input with variable batch size
input_data = np.random.rand(5,4).astype(np.float32)
# Load model
session = ort.InferenceSession('dynamic_model.onnx')
input_name = session.get_inputs()[0].name
# Run inference
outputs = session.run(None, {input_name: input_data})
print('Dynamic input output:', outputs)
Inference with dynamic input shapes in ONNX Runtime.
ONNX Softmax Output Example
import onnxruntime as ort
import numpy as np
# Load model
session = ort.InferenceSession('classification_model.onnx')
input_name = session.get_inputs()[0].name
input_data = np.random.rand(1,10).astype(np.float32)
# Run inference
logits = session.run(None, {input_name: input_data})[0]
softmax = np.exp(logits) / np.sum(np.exp(logits), axis=1, keepdims=True)
print('Softmax probabilities:', softmax)
Performing inference and applying softmax to ONNX model outputs.
ONNX Text Model Inference Example
import onnxruntime as ort
import numpy as np
# Example input
input_data = np.random.rand(1,128).astype(np.float32) # e.g., token embeddings
# Load model
session = ort.InferenceSession('text_model.onnx')
input_name = session.get_inputs()[0].name
# Run inference
outputs = session.run(None, {input_name: input_data})
print('Text classification output:', outputs)
Performing inference using an ONNX text classification model.
ONNX Model Warmup Example
import onnxruntime as ort
import numpy as np
# Load model
session = ort.InferenceSession('model.onnx')
input_name = session.get_inputs()[0].name
# Warmup pass
for _ in range(5):
input_data = np.random.rand(1,4).astype(np.float32)
_ = session.run(None, {input_name: input_data})
print('ONNX model warmed up and ready for real inference.')
Performing a warmup pass for an ONNX model to optimize runtime performance.
Frequently Asked Questions about Onnx
What is Onnx?
ONNX (Open Neural Network Exchange) is an open-source format and ecosystem for representing machine learning models, enabling interoperability between frameworks like PyTorch, TensorFlow, and scikit-learn, and allowing deployment across diverse platforms.
What are the primary use cases for Onnx?
Exporting models from PyTorch, TensorFlow, or other frameworks. Cross-framework deployment without retraining. Hardware-accelerated inference on CPUs, GPUs, and specialized accelerators. Optimizing models with ONNX Runtime for production. Edge AI and mobile deployment of ML models
What are the strengths of Onnx?
Simplifies model transfer between different ML frameworks. Optimized inference using ONNX Runtime. Supports deployment on multiple hardware backends. Reduces need to rewrite models for different environments. Strong ecosystem with converter tools and runtime support
What are the limitations of Onnx?
Not all framework-specific features/operators are supported. Complex custom layers may require manual conversion. Primarily focused on inference; less used for training. Debugging model conversion issues can be tricky. Smaller community compared to primary frameworks like PyTorch/TensorFlow
How can I practice Onnx typing speed?
CodeSpeedTest offers 10+ real Onnx code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.