Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to load a PyTorch model for mobile deployment.
PyTorch
import torch model = torch.jit.load([1]) model.eval()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the file path string.
Using the wrong file extension like '.pth' instead of '.pt'.
✗ Incorrect
The model file path must be a string with quotes, like "model.pt".
2fill in blank
mediumComplete the code to convert a PyTorch model to TorchScript for mobile deployment.
PyTorch
import torch scripted_model = torch.jit.[1](model) scripted_model.save("model.pt")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using torch.jit.trace instead of torch.jit.script for models with control flow.
Confusing save and load methods.
✗ Incorrect
torch.jit.script converts the model to TorchScript by scripting it.
3fill in blank
hardFix the error in the code to run inference on a mobile model.
PyTorch
input_tensor = torch.randn(1, 3, 224, 224) output = model.[1](input_tensor) print(output)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like predict or run.
Trying to call the model object directly without parentheses.
✗ Incorrect
In PyTorch, the forward method runs the model on input data.
4fill in blank
hardFill both blanks to prepare input and run inference on a mobile model.
PyTorch
input_data = torch.randn([1]) output = model.[2](input_data) print(output)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong input shape missing batch dimension.
Using incorrect method names for inference.
✗ Incorrect
Input tensor shape must match model input; use forward to run inference.
5fill in blank
hardFill all three blanks to save a scripted model and load it for mobile deployment.
PyTorch
scripted_model = torch.jit.[1](model) scripted_model.[2]("model.pt") loaded_model = torch.jit.[3]("model.pt")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up save and load methods.
Using trace instead of script for models with control flow.
✗ Incorrect
Use script to convert, save to write file, and load to read file.