Issue: UnpicklingError when loading model checkpoint with torch.load in PyTorch 2.6
Description: I encountered an UnpicklingError while loading a model checkpoint with the following code:
checkpoint = torch.load('model_checkpoint.pth') model.load_state_dict(checkpoint['model_state_dict']) optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
The error suggests that torch.load has changed its default behavior in PyTorch 2.6. Specifically, the weights_only parameter is now set to True by default, which prevents loading the full checkpoint file (including the model and optimizer state dictionaries).
Proposed Solution: To resolve this issue, you can explicitly set the weights_only parameter to False when loading the checkpoint, which will load the full checkpoint including both the model and optimizer state dictionaries. However, please note that this could potentially allow execution of arbitrary code, so make sure to trust the source of the checkpoint file.
checkpoint = torch.load('model_checkpoint.pth', weights_only=False) model.load_state_dict(checkpoint['model_state_dict']) optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
Hi @Vans-Lee,
First of all, thank you for supporting my work!
You're absolutely right, and I have already updated Chapters 2 and 2.1, as well as the files in the StepByStep folder to include the weights_only=False argument. I'm aware it's a security risk if it's used with files of unknown origin but, in the educational context of the book, it should be fine.
Thank you for pointing this out.
Best, Daniel