Bot TutorialsGrok BotChatGPTClaudeGeminiCopilotPerplexityMore botsGuidesArticlesPapers
Topics
CookingHome automationFitnessCreative writingMusic productionCodingIndustrialPros (Professional track)
Search

Part 10 of 12

Export a Model to ONNX and Run It: A Deep Dive

Deep diveBy Bot Tutorials · Published · Facts last verified Nov 3, 2026

ML libraries series complete. "Review Your Training Code with a Bot: Checklist" was the finale. See every part in the series hub.

Part 10: convert a scikit-learn pipeline to ONNX, run it with ONNX Runtime, and prove the predictions match, with real output and GPU notes.

Video: Export a Model to ONNX and Run It: A Deep Dive (4:12). Narrated from this guide, with the real code and real output we ran. Captions included (caption file). Prefer reading? Everything in the video is in the guide below.

Training a model in one library and running it somewhere else is a common need: a web service, a small device, a program in another language. ONNX is an open format for machine learning models, and ONNX Runtime runs ONNX models on many platforms. In this deep dive we export the scikit-learn pipeline from Part 5 to ONNX, run it with ONNX Runtime, and check that the answers really match, which is the step people skip.

What the bot can and can't do here

It can:

It can't:

Check first, ship second

Never deploy an exported model without comparing its outputs with the original on the same inputs. Conversion can change numbers or, occasionally, behaviour.

Install

ONNX Runtime's install page gives pip install onnxruntime for CPU. For scikit-learn models, the skl2onnx converter from the sklearn-onnx project does the export. On our box this installed onnxruntime 1.31.1 and skl2onnx 1.20.0.

The script

import numpy as np, onnxruntime as ort
from sklearn.datasets import load_iris
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from skl2onnx import to_onnx

X, y = load_iris(return_X_y=True)
X = X.astype(np.float32)
model = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000)).fit(X, y)
onx = to_onnx(model, X[:1], options={"zipmap": False})
open("iris.onnx", "wb").write(onx.SerializeToString())
sess = ort.InferenceSession("iris.onnx", providers=["CPUExecutionProvider"])
print("providers:", sess.get_providers())
inp = sess.get_inputs()[0].name
labels, probs = sess.run(None, {inp: X})
agree = (labels == model.predict(X)).mean()
print("label agreement with scikit-learn:", agree)
print("max prob difference:", float(np.abs(probs - model.predict_proba(X)).max()))

Real output:

providers: ['CPUExecutionProvider']
label agreement with scikit-learn: 1.0
max prob difference: 1.4901161193847656e-07

What the script does

Reading the comparison

Ask a bot for this check every time:

Add a check that runs both the original model and the ONNX model on the same inputs, and reports label agreement and the maximum absolute difference in probabilities. Fail if agreement is below 1.0 or the difference is above 1e-5.

Choose the tolerance for your use. For a classifier, label agreement is usually what matters most.

GPU notes, documented

ONNX Runtime's CUDA Execution Provider page explains which package matches your stack. When we checked:

Combine this with Part 1's driver checker before installing a GPU build.

Exporting from PyTorch or TensorFlow

PyTorch has a built-in exporter, torch.onnx.export, and TensorFlow models are commonly converted with the tf2onnx project. We didn't test either here. The same rule applies: compare outputs on real inputs before you trust the export.

Security

An ONNX file describes a computation graph. Still, treat model files from unknown sources with care, and keep track of where each one came from and under which license.

Words you'll see

If a converter rejects your model, paste the exact error and the converter's version into the bot, and ask it which part of the model isn't supported according to the converter's docs. Don't accept a workaround that changes what the model computes.

Tested vs documented

Where to go next

The next part today is about reading error messages and finding slow code.

Safety, licensing and privacy: read this before you run anything

This series is about learning and small personal projects with machine learning libraries. A bot is a helper, not an authority.

FAQ

Why export to ONNX?

ONNX is an open format, and ONNX Runtime can run the same model from Python and other languages without scikit-learn or PyTorch installed.

Do the predictions match exactly?

In our test every label matched, and the largest probability difference was about 1.5e-07, which comes from using 32-bit floats.

Was the GPU tested?

No. We ran the CPU execution provider only. The CUDA requirements come from ONNX Runtime's docs.

Sources

  1. sklearn-onnx documentation
  2. ONNX Runtime: Install
  3. ONNX Runtime: Get started with Python
  4. ONNX Runtime: CUDA Execution Provider (requirements)
  5. NVIDIA: CUDA Compatibility
  6. scikit-learn: Model persistence

More in this series

  1. Part 1: GPU, Driver and CUDA Compatibility: A Bot-Assisted Check · Guide
  2. Part 2: How Much Memory Does a Model Need? A Data Brief · Data brief
  3. Part 3: Pin Versions and Build Reproducible ML Environments · Walkthrough
  4. Part 4: NumPy and pandas Basics with a Bot: Cheat Sheet · Cheat sheet
  5. Part 5: Your First scikit-learn Model, Built with a Bot · Walkthrough
  6. Part 6: PyTorch on the CPU: First Steps with a Bot · Guide
  7. Part 7: TensorFlow and Keras Install Requirements, Explained · Explainer
  8. Part 8: JAX on the CPU: Field Notes with a Bot · Field notes
  9. Part 9: Read Hugging Face Model Cards and Licenses with a Bot · Guide
  10. Part 10: Export a Model to ONNX and Run It: A Deep Dive (you are here) · Deep dive
  11. Part 11: Read ML Error Messages and Profile Code: Cheat Sheet · Cheat sheet
  12. Part 12: Review Your Training Code with a Bot: Checklist · Checklist

See the ML Libraries with a Bot series hub · Latest on Bot Tutorials

Bot advice can be wrong. We cite official docs; check them yourself before acting, especially for anything involving money, safety or your data.