0
0
PyTorchml~20 mins

ONNX export in PyTorch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ONNX Export Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
ONNX export output shape verification
Consider the following PyTorch model and ONNX export code. What will be the shape of the output tensor when running the ONNX model with the given input shape?
PyTorch
import torch
import torch.nn as nn
import onnx

class SimpleModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.linear = nn.Linear(10, 5)
    def forward(self, x):
        return self.linear(x)

model = SimpleModel()
model.eval()
x = torch.randn(3, 10)

# Export to ONNX
onnx_path = "simple_model.onnx"
torch.onnx.export(model, x, onnx_path, input_names=["input"], output_names=["output"], opset_version=11)

# Assume we load and run the ONNX model with input shape (3, 10)
# What is the shape of the output tensor?
A(3, 5)
B(10, 5)
C(3, 10)
D(5, 3)
Attempts:
2 left
💡 Hint
Think about how the linear layer transforms the input shape.
Model Choice
intermediate
1:30remaining
Choosing correct opset version for ONNX export
You want to export a PyTorch model using torch.onnx.export. Which opset_version should you choose to ensure compatibility with most ONNX runtimes and support for common operators?
Aopset_version=7
Bopset_version=11
Copset_version=15
Dopset_version=9
Attempts:
2 left
💡 Hint
Check the default recommended opset version in PyTorch documentation.
🔧 Debug
advanced
2:30remaining
ONNX export error due to dynamic axes
You try to export a PyTorch model to ONNX with dynamic batch size using this code: torch.onnx.export(model, x, "model.onnx", dynamic_axes={"input": {0: "batch_size"}}) But you get an error about missing output dynamic axes. What is the most likely cause?
AYou must specify dynamic axes for both input and output tensors.
BThe model does not support dynamic batch size.
CYou forgot to set opset_version=15.
DYou need to convert the model to eval mode before export.
Attempts:
2 left
💡 Hint
Dynamic axes must be declared for all tensors that vary in size.
Metrics
advanced
2:00remaining
Verifying ONNX model correctness with output comparison
After exporting a PyTorch model to ONNX, you want to verify the ONNX model produces the same output as the PyTorch model for the same input. Which metric is best to check this?
ACross-entropy loss between PyTorch and ONNX outputs
BAccuracy score between PyTorch and ONNX outputs
CMean squared error (MSE) between PyTorch and ONNX outputs
DConfusion matrix of PyTorch and ONNX outputs
Attempts:
2 left
💡 Hint
You want to measure numerical closeness of outputs.
🧠 Conceptual
expert
3:00remaining
Understanding ONNX export limitations with control flow
Which of the following statements about exporting PyTorch models with control flow (like loops or conditionals) to ONNX is TRUE?
AControl flow must be removed from the model before exporting to ONNX.
BONNX export can handle control flow only if it is traced, not scripted.
CONNX supports all Python control flow constructs natively during export.
DONNX supports control flow if the model is scripted with torch.jit.script before export.
Attempts:
2 left
💡 Hint
Think about how PyTorch JIT scripting helps ONNX export.