Menu

Saving and Loading Models in PyTorch: state_dict, Checkpoints and Resuming Training
📰

Saving and Loading Models in PyTorch: state_dict, Checkpoints and Resuming Training

#devtopytorch#devto#dev#model#state_dict
Reading 0:00
15s threshold

A trained model is saved for one of two reasons. The first is to use it later for predictions, which needs only the model's weights. The second is to continue training later, after a crash, a disconnected notebook or a planned stop, which needs the weights and also the optimizer's internal state. This article covers both: what a state_dict is, how to save and load one, how to keep the best model during training, and what a full checkpoint must contain for training to resume exactly where it stopped.

What a state_dict is

model.state_dict() returns an ordered dictionary. The keys are the names of the model's tensors and the values are the tensors themselves.

import torch import torch.nn as nn

model = nn . Sequential ( nn . Linear ( 10 , 32 ), nn . ReLU (), nn . Linear ( 32 , 3 ))

for k , v in model . state_dict (). items (): print ( f " { k : 12 s } { tuple ( v .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.