Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'r' or 'rb' mode which is for reading files.
✗ Incorrect
Use 'wb' to write a binary file when saving a model with pickle.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Saving ONNX model with '.pt' or '.pkl' extensions.
✗ Incorrect
ONNX models are saved with the '.onnx' extension.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'export' or 'dump' which are not valid for TorchScript.
✗ Incorrect
TorchScript models use the 'save' method to write to disk.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'wb' mode or pickle.dump() when loading.
✗ Incorrect
To load a pickled model, open the file in 'rb' mode and use pickle.load().
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'trace' instead of 'script' or wrong file extensions.
✗ Incorrect
Use torch.jit.script() to create a TorchScript model, then save() it with '.pt' extension.