Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to load the saved model weights into the model.
PyTorch
model = MyModel() model.[1](torch.load('model_weights.pth'))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using save_state_dict instead of load_state_dict
Trying to load weights with a method that doesn't exist
✗ Incorrect
The method load_state_dict loads the saved weights into the model.
2fill in blank
mediumComplete the code to load the model weights onto the CPU device.
PyTorch
state_dict = torch.load('model_weights.pth', map_location=[1]) model.load_state_dict(state_dict)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cuda' when no GPU is available
Using an invalid device string like 'gpu'
✗ Incorrect
Using map_location='cpu' loads the model weights onto the CPU device.
3fill in blank
hardFix the error in loading the model weights by completing the code.
PyTorch
model = MyModel() state_dict = torch.load('weights.pth') model.[1](state_dict, strict=False)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent method like load_weights
Not passing the state dictionary to the correct method
✗ Incorrect
The correct method to load weights is load_state_dict, which accepts the state dictionary.
4fill in blank
hardFill both blanks to load the model weights and set the model to evaluation mode.
PyTorch
model = MyModel() model.[1](torch.load('model.pth')) model.[2]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling train() instead of eval() after loading weights
Using save_state_dict instead of load_state_dict
✗ Incorrect
First, load_state_dict loads the weights, then eval() sets the model to evaluation mode.
5fill in blank
hardFill all three blanks to load the model weights, move the model to GPU, and set it to evaluation mode.
PyTorch
model = MyModel() model.[1](torch.load('weights.pth', map_location=[2])) model.to([3]) model.eval()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Loading weights directly on GPU without map_location
Using save_state_dict instead of load_state_dict
Moving model to 'cpu' instead of 'cuda'
✗ Incorrect
Load weights with load_state_dict, load them on CPU with map_location='cpu', then move model to GPU with to('cuda').