-
Notifications
You must be signed in to change notification settings - Fork 470
Description
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'])