0
0
PyTorchml~10 mins

TorchScript for production 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 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'
Atrace
Bscript
Ccompile
Doptimize
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.
2fill in blank
medium

Complete 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'
Asave
Bwrite
Cexport
Ddump
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.
3fill in blank
hard

Fix 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'
Aread
Bload
Copen
Dimport
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.
4fill in blank
hard

Fill 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'
Ascript
Bsave
Ctrace
Dexport
Attempts:
3 left
💡 Hint
Common Mistakes
Using trace decorator which does not exist.
Using export instead of save to write the file.
5fill in blank
hard

Fill 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'
Atrace
Bexample_input
Cshape
Dscript
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.