0
0
PyTorchml~10 mins

ONNX export in PyTorch - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to export a PyTorch model to ONNX format.

PyTorch
import torch
import torch.nn as nn

model = nn.Linear(10, 5)
dummy_input = torch.randn(1, 10)
torch.onnx.export(model, dummy_input, [1])
Drag options to blanks, or click blank then click option'
A"model.onnx"
Bmodel.onnx
Cdummy_input
Dtorch.onnx
Attempts:
3 left
💡 Hint
Common Mistakes
Not providing the file path as a string.
Passing the model or dummy input as the file path.
2fill in blank
medium

Complete the code to specify the input names when exporting to ONNX.

PyTorch
torch.onnx.export(model, dummy_input, "model.onnx", input_names=[[1]])
Drag options to blanks, or click blank then click option'
Ainput1
B["input1"]
C"input1"
D'input1'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the input name without quotes.
Passing a list inside the blank when brackets are already present.
3fill in blank
hard

Fix the error in the code to export the model with dynamic axes for variable batch size.

PyTorch
torch.onnx.export(model, dummy_input, "model.onnx", dynamic_axes={"input": [1])
Drag options to blanks, or click blank then click option'
A{"batch_size": "0"}
B{"0": "batch_size"}
C{"batch_size": 0}
D{"0": 0}
Attempts:
3 left
💡 Hint
Common Mistakes
Using integers instead of strings as keys.
Swapping keys and values in the dictionary.
4fill in blank
hard

Fill both blanks to export the model with verbose output and specifying output names.

PyTorch
torch.onnx.export(model, dummy_input, "model.onnx", verbose=[1], output_names=[[2]])
Drag options to blanks, or click blank then click option'
ATrue
BFalse
C"output1"
Doutput1
Attempts:
3 left
💡 Hint
Common Mistakes
Passing output names without quotes.
Passing verbose as a string instead of boolean.
5fill in blank
hard

Fill all three blanks to export the model with opset version 11, enable constant folding, and set training mode to evaluation.

PyTorch
torch.onnx.export(model, dummy_input, "model.onnx", opset_version=[1], do_constant_folding=[2], training=[3])
Drag options to blanks, or click blank then click option'
A10
B11
CTrue
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong opset version.
Not enabling constant folding.
Setting training to True instead of evaluation mode.