PyTorch torch.load_state_dict function
PyTorch torch reference manual
torch.load_state_dict is a function in PyTorch used to load a state dictionary. It loads the state of a model, optimizer, etc. from a dictionary object.
Function definition
torch.load_state_dict(state_dict, strict=True)
Usage examples
Example
import
torch
import
torch.
nn
as
nn
Define a simple model
model
=
nn.
Linear
(
10
,
5
)
Save a model state dictionary
state_dict
=
model.
state_dict
(
)
torch.
save
(
state_dict
,
'model_state.pt'
)
Load a state dictionary
loaded_state
=
torch.
load
(
'model_state.pt'
)
model.
load_state_dict
(
loaded_state
)
(
"State dict loaded successfully!"
)