0
0
PyTorchml~10 mins

ONNX Runtime inference 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 load an ONNX model using ONNX Runtime.

PyTorch
import onnxruntime as ort

session = ort.InferenceSession([1])
Drag options to blanks, or click blank then click option'
A"model.onnx"
Bmodel.onnx
Cload_model("model.onnx")
Donnx.load("model.onnx")
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the model filename without quotes causes a NameError.
Using onnx.load returns a model object, not a path string.
2fill in blank
medium

Complete the code to prepare input data as a dictionary for ONNX Runtime inference.

PyTorch
import numpy as np

input_name = session.get_inputs()[0].name
input_data = np.array([[1.0, 2.0, 3.0]], dtype=np.float32)
inputs = [1]
Drag options to blanks, or click blank then click option'
A{input_name: input_data}
B[input_name, input_data]
C(input_name, input_data)
Dinput_name + input_data
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list or tuple instead of a dictionary causes runtime errors.
Concatenating strings and arrays is invalid.
3fill in blank
hard

Fix the error in the code to run inference and get the output.

PyTorch
outputs = session.run(None, [1])
print(outputs[0])
Drag options to blanks, or click blank then click option'
Ainput_name
Binput_data
Cinputs
Dsession.get_inputs()
Attempts:
3 left
💡 Hint
Common Mistakes
Passing only the numpy array causes a TypeError.
Passing the input name string causes a runtime error.
4fill in blank
hard

Fill both blanks to get the name of the first output and print its shape.

PyTorch
output_name = session.get_outputs()[[1]].name
print(session.run([output_name], [2])[0].shape)
Drag options to blanks, or click blank then click option'
A0
Binputs
C1
Dinput_data
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 1 accesses a non-first output which may not exist.
Passing raw input data instead of dictionary causes errors.
5fill in blank
hard

Fill all three blanks to run inference on multiple inputs and get the second output.

PyTorch
input_names = [inp.name for inp in session.get_inputs()]
inputs = {name: np.random.rand(1, 3).astype(np.float32) for name in input_names}
outputs = session.run([session.get_outputs()[[1]].name], [2])
print(outputs[0].shape)

# Access input for first input name
first_input = inputs[[3]]
Drag options to blanks, or click blank then click option'
A1
Binputs
Cinput_names[0]
Dinput_names[1]
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong output index causes index errors.
Passing raw data instead of inputs dictionary causes errors.
Accessing inputs with wrong key causes KeyError.