0
0
PyTorchml~10 mins

Loading model state_dict 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 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'
Aload_state_dict
Bsave_state_dict
Csave_weights
Dload_weights
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
2fill in blank
medium

Complete 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'
A'cpu'
B'cuda'
C'gpu'
D'device'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cuda' when no GPU is available
Using an invalid device string like 'gpu'
3fill in blank
hard

Fix 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'
Aload_weights
Bload_model
Cload_state_dict
Dload_params
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent method like load_weights
Not passing the state dictionary to the correct method
4fill in blank
hard

Fill 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'
Aload_state_dict
Btrain
Ceval
Dsave_state_dict
Attempts:
3 left
💡 Hint
Common Mistakes
Calling train() instead of eval() after loading weights
Using save_state_dict instead of load_state_dict
5fill in blank
hard

Fill 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'
Aload_state_dict
B'cpu'
C'cuda'
Dsave_state_dict
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'