0
0
MLOpsdevops~10 mins

Model serialization formats (pickle, ONNX, TorchScript) in MLOps - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to save a model using pickle.

MLOps
import pickle
with open('model.pkl', '[1]') as f:
    pickle.dump(model, f)
Drag options to blanks, or click blank then click option'
Ar
Brb
Cwb
Dw
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'r' or 'rb' mode which is for reading files.
2fill in blank
medium

Complete the code to export a PyTorch model to ONNX format.

MLOps
import torch
model.eval()
dummy_input = torch.randn(1, 3, 224, 224)
torch.onnx.export(model, dummy_input, '[1]')
Drag options to blanks, or click blank then click option'
Amodel.onnx
Bmodel.script
Cmodel.pkl
Dmodel.pt
Attempts:
3 left
💡 Hint
Common Mistakes
Saving ONNX model with '.pt' or '.pkl' extensions.
3fill in blank
hard

Fix the error in saving a TorchScript model.

MLOps
import torch
scripted_model = torch.jit.script(model)
scripted_model.[1]('model.pt')
Drag options to blanks, or click blank then click option'
Aexport
Bsave
Cdump
Dwrite
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'export' or 'dump' which are not valid for TorchScript.
4fill in blank
hard

Fill both blanks to load a pickled model correctly.

MLOps
import pickle
with open('model.pkl', '[1]') as f:
    model = pickle.[2](f)
Drag options to blanks, or click blank then click option'
Arb
Bwb
Cload
Ddump
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'wb' mode or pickle.dump() when loading.
5fill in blank
hard

Fill all three blanks to export a TorchScript model and save it.

MLOps
import torch
scripted = torch.jit.[1](model)
scripted.[2]('model_scripted.[3]')
Drag options to blanks, or click blank then click option'
Atrace
Bsave
Cpt
Dscript
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'trace' instead of 'script' or wrong file extensions.