Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to convert a PyTorch model to TorchScript using tracing.
PyTorch
import torch import torchvision.models as models model = models.resnet18(pretrained=True) model.eval() example_input = torch.randn(1, 3, 224, 224) scripted_model = torch.jit.[1](model, example_input)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using torch.jit.script instead of torch.jit.trace for models with fixed control flow.
Trying to call compile which is not a torch.jit method.
✗ Incorrect
Tracing records the operations of the model with example inputs to create a TorchScript version.
2fill in blank
mediumComplete the code to save the TorchScript model to a file.
PyTorch
scripted_model = torch.jit.trace(model, torch.randn(1, 3, 224, 224)) scripted_model.[1]('resnet18_scripted.pt')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using export or write which are not TorchScript methods.
Trying to use dump which is not a valid method here.
✗ Incorrect
The save() method writes the scripted model to disk for later use.
3fill in blank
hardFix the error in loading a TorchScript model from a file.
PyTorch
import torch scripted_model = torch.jit.[1]('resnet18_scripted.pt')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using open or read which are Python file functions but not for TorchScript models.
Using import which is not a torch.jit method.
✗ Incorrect
torch.jit.load() loads a saved TorchScript model from disk.
4fill in blank
hardFill both blanks to define a TorchScript function and save it.
PyTorch
import torch @torch.jit.[1] def add_tensors(x, y): return x + y scripted_fn = add_tensors scripted_fn.[2]('add_tensors.pt')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using trace decorator which does not exist.
Using export instead of save to write the file.
✗ Incorrect
Use @torch.jit.script to compile the function and save() to write it to a file.
5fill in blank
hardFill all three blanks to create a scripted model, run inference, and print the output shape.
PyTorch
import torch import torchvision.models as models model = models.mobilenet_v2(pretrained=True) model.eval() example_input = torch.randn(1, 3, 224, 224) scripted_model = torch.jit.[1](model, example_input) output = scripted_model([2]) print(output.[3])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using script instead of trace for this model.
Passing wrong input variable to the model.
Printing output instead of output.shape.
✗ Incorrect
Trace the model, pass example_input to get output, then print output.shape to see the tensor size.