Complete the code to save the model's state dictionary.
torch.save(model.[1](), 'model.pth')
In PyTorch, the recommended way to save a model is to save its state_dict(), which contains all the learned parameters.
Complete the code to load the saved model state dictionary.
model = MyModel() model.[1](torch.load('model.pth'))
To load saved parameters into a PyTorch model, use load_state_dict() with the loaded state dictionary.
Fix the error in saving the best model during training.
if val_loss < best_loss: best_loss = val_loss torch.save(model.[1](), 'best_model.pth')
Saving the state_dict() ensures only the model parameters are saved, which is the correct way to save the best model.
Fill both blanks to implement a training loop that saves the best model based on validation accuracy.
best_acc = 0.0 for epoch in range(num_epochs): train() val_acc = validate() if val_acc [1] best_acc: best_acc = val_acc torch.save(model.[2](), 'best_model.pth')
We save the model when validation accuracy improves, so the comparison is val_acc > best_acc. The model's state_dict() is saved.
Fill all three blanks to load the best saved model and evaluate it.
model = MyModel() model.[1](torch.load('best_model.pth')) model.[2]() accuracy = evaluate(model, test_loader) print(f'Best model test accuracy: [3]')
eval() before evaluationLoad the saved parameters with load_state_dict(), set the model to evaluation mode with eval(), then print the accuracy variable.