Challenge - 5 Problems
PyTorch Model Saver
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this PyTorch model saving code?
Consider the following PyTorch code that defines and saves a simple model. What will be the output when loading and printing the saved model's state_dict keys?
PyTorch
import torch import torch.nn as nn class SimpleModel(nn.Module): def __init__(self): super().__init__() self.linear = nn.Linear(2, 2) def forward(self, x): return self.linear(x) model = SimpleModel() torch.save(model, 'model.pth') loaded_model = torch.load('model.pth') print(list(loaded_model.state_dict().keys()))
Attempts:
2 left
💡 Hint
Think about how PyTorch names parameters inside nn.Module layers.
✗ Incorrect
When saving the entire model, the state_dict keys correspond to the layer names and their parameters. Here, the linear layer's weights and biases are named 'linear.weight' and 'linear.bias'.
❓ Model Choice
intermediate1:30remaining
Which method saves the entire PyTorch model including architecture and weights?
You want to save a PyTorch model so that you can load it later without redefining the model class. Which method should you use?
Attempts:
2 left
💡 Hint
Saving the entire model means saving both architecture and weights.
✗ Incorrect
torch.save(model, 'model.pth') saves the entire model object including architecture and weights. Saving state_dict only saves weights, not architecture.
❓ Hyperparameter
advanced1:30remaining
What is a key consideration when saving entire PyTorch models for future use?
When saving entire PyTorch models, which of the following is an important consideration to ensure the model can be loaded correctly later?
Attempts:
2 left
💡 Hint
Think about what happens when loading a saved model object.
✗ Incorrect
When saving the entire model object, PyTorch saves the class name and parameters but not the class code. The class definition must be present when loading.
🔧 Debug
advanced2:00remaining
Why does loading a saved entire PyTorch model raise an error?
You saved a PyTorch model using torch.save(model, 'model.pth'). Later, when you try to load it with torch.load('model.pth'), you get an error: ModuleNotFoundError: No module named 'mymodel'. What is the cause?
Attempts:
2 left
💡 Hint
Check if the model class is available when loading.
✗ Incorrect
Saving entire models requires the class definition to be present when loading. If the class is missing, Python cannot find it and raises ModuleNotFoundError.
❓ Metrics
expert2:30remaining
After loading an entire PyTorch model, how to verify it matches the original model?
You saved a PyTorch model using torch.save(model, 'model.pth') and later loaded it with torch.load('model.pth'). Which method correctly verifies that the loaded model's parameters match the original model's parameters?
PyTorch
import torch import torch.nn as nn class SimpleModel(nn.Module): def __init__(self): super().__init__() self.linear = nn.Linear(2, 2) def forward(self, x): return self.linear(x) original_model = SimpleModel() torch.save(original_model, 'model.pth') loaded_model = torch.load('model.pth') # Which code below correctly checks parameter equality?
Attempts:
2 left
💡 Hint
Compare tensors element-wise for equality.
✗ Incorrect
state_dict() returns dicts, so comparing dicts directly with == compares keys and values but not tensor content properly. Using torch.equal on each parameter tensor pair is the correct way.