Export to ONNX
Run your model across runtimes.
Export to ONNX is a free Deep Learning Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Deep Learning Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
One Model, Many Runtimes
Sometimes your model must run somewhere PyTorch is not installed. ONNX is a shared format that lets one model run across many engines. 🌐
What ONNX Stands For
ONNX means Open Neural Network Exchange. It is a vendor-neutral file format that describes your model as a graph of standard operations.
Why Teams Love ONNX
With ONNX you train in PyTorch but deploy on ONNX Runtime, mobile, or the browser, without rewriting the model for each platform.
Export in One Call
You export by giving PyTorch the model and a sample input. The exporter traces the run and writes a portable graph to disk.
import torch
torch.onnx.export(model, sample_input, 'model.onnx')The Dummy Input Shapes the Graph
That sample tensor must match your real input shape and dtype, because the exporter records the operations the data triggers.
sample_input = torch.randn(1, 3, 224, 224)Name Your Inputs and Outputs
Give clear input and output names so the serving code can bind data by name instead of guessing positions.
torch.onnx.export(model, x, 'm.onnx',
input_names=['image'], output_names=['logits'])Allow Flexible Batch Sizes
By default the batch size is fixed. Mark it as a dynamic axis so the exported model accepts any number of inputs at once.
dynamic_axes={'image': {0: 'batch'}}Always Check Your Export
Load the file with the onnx library and run the built-in checker to confirm the graph is valid before you ship it. ✅
import onnx
onnx.checker.check_model(onnx.load('model.onnx'))Run It with ONNX Runtime
ONNX Runtime is a fast engine that executes the exported model on CPU or GPU, often quicker than plain Python inference.
import onnxruntime as ort
session = ort.InferenceSession('model.onnx')Confirm the Numbers Match
Run the same input through PyTorch and ONNX Runtime and compare outputs. They should match closely, proving the export is faithful.
Mind the Opset Version
Each export targets an opset version, the set of supported operations. Pick a version your target runtime understands to avoid errors.
torch.onnx.export(model, x, 'm.onnx', opset_version=17)Quick Check
You want a fixed batch to instead accept any size. What do you set?
Recap: Portable Across Runtimes
You exported a PyTorch model to ONNX with a sample input, named axes, validated it, and ran it on ONNX Runtime anywhere. 🎉
Frequently asked questions
Is the “Export to ONNX” lesson free?
Yes — the full text of “Export to ONNX” is free to read here on the web, and the Deep Learning Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Deep Learning Academy course, upgrade to CoddyKit PRO.
What will I learn in “Export to ONNX”?
Run your model across runtimes. You practise Deep Learning Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Deep Learning Academy?
No prior experience is required. Deep Learning Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Export to ONNX” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Deep Learning Academy lesson?
Yes. Every Deep Learning Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.