Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Not providing the file path as a string.
Passing the model or dummy input as the file path.
✗ Incorrect
The second argument to torch.onnx.export is the dummy input, and the third argument is the file path as a string where the ONNX model will be saved.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the input name without quotes.
Passing a list inside the blank when brackets are already present.
✗ Incorrect
The input_names argument expects a list of strings. Since the list brackets are already in the code, just provide the string name with quotes.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using integers instead of strings as keys.
Swapping keys and values in the dictionary.
✗ Incorrect
The dynamic_axes argument expects a dictionary mapping input names to dictionaries that map axis indices (as strings) to axis names.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing output names without quotes.
Passing verbose as a string instead of boolean.
✗ Incorrect
verbose expects a boolean value, so True or False. output_names expects a list of strings, so the string name with quotes is correct.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong opset version.
Not enabling constant folding.
Setting training to True instead of evaluation mode.
✗ Incorrect
opset_version should be 11 for compatibility, do_constant_folding is a boolean to optimize constants, and training should be set to torch.onnx.TrainingMode.EVAL which is represented here as False for no training.