Esportare in ONNX per la portabilità
Sposti un modello tra framework e runtime diversi.
Esportare in ONNX per la portabilità è una lezione MLOps Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento MLOps Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso MLOps Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
The Portability Problem
You trained in PyTorch but want to serve in C++ or the browser. A framework-locked file makes that painful. ONNX fixes it. 🌍
What ONNX Is
ONNX is an open, shared format for models. It describes the math as a graph that many frameworks and runtimes can read.
A Graph of Operators
Under the hood an ONNX model is a graph of standard operators like MatMul and Relu, plus the learned weights.
Train Once, Run Anywhere
Export once and the same file runs on servers, mobile, and edge devices through different runtimes, no retraining needed.
Export from PyTorch
PyTorch ships an exporter. You pass the model and a sample input to torch.onnx.export so it can trace the graph.
import torch
torch.onnx.export(model, sample, 'model.onnx')Why a Sample Input?
The exporter runs your model on the sample to record which ops fire. That traced run becomes the graph it saves.
Convert from sklearn
For scikit-learn use the skl2onnx library. You give it the model and the input shape to produce an ONNX graph.
from skl2onnx import to_onnx
onx = to_onnx(model, X[:1])Run with ONNX Runtime
ONNX Runtime is a fast, cross-platform engine. You load the .onnx file into a session and call run to get predictions.
import onnxruntime as ort
sess = ort.InferenceSession('model.onnx')Speed and Hardware
ONNX Runtime can use execution providers like CUDA or TensorRT to accelerate the same model on different hardware. ⚡
Opset Versions
Each export targets an opset version that defines available operators. Match it to what your runtime supports, or loading fails.
Always Verify Outputs
After export, run both models on the same input and compare. Confirm the ONNX result matches the original before you ship it. ✅
Quick Check
Let's see how well you understand ONNX.
Recap
You can export models to ONNX, run them with ONNX Runtime on any hardware, mind the opset, and always verify outputs match. 🎉
Domande Frequenti
La lezione «Esportare in ONNX per la portabilità» è gratuita?
Sì — il testo completo di «Esportare in ONNX per la portabilità» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso MLOps Academy, passa a CoddyKit PRO. Il corso MLOps Academy include 4 lezioni in totale.
Cosa imparerò in «Esportare in ONNX per la portabilità»?
Sposti un modello tra framework e runtime diversi. Eserciti MLOps Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare MLOps Academy?
Non è richiesta alcuna esperienza precedente. MLOps Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.
Quanto tempo richiede la lezione «Esportare in ONNX per la portabilità»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione MLOps Academy?
Sì. Ogni lezione MLOps Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Serializzare con Pickle e Joblib
- Il model flavor di MLflow
- Esportare in ONNX per la portabilità
- Definire signature e schema del modello