How To Save and Load Models in PyTorch

训练好模型后,我们通常会保存模型以便后续使用,以下是pytorch保存模型的三种方法:

1. Use state_dict To Save And Load PyTorch Models (Recommended)

此种方法就是记录下model的参数,只保存模型参数字典

save :

torch.save(model.state_dict(), 'save/to/path/model.pth')

load :

model = MyModelDefinition(args)
model.load_state_dict(torch.load('load/from/path/model.pth'))

2. Save and Load the Entire PyTorch Model

保存整个模型,一般不推荐使用 

save :

torch.save(model, 'save/to/path/model.pt')

load :

torch.save(model, 'save/to/path/model.pt')

 3. Save and Load Your PyTorch Model From a Checkpoint

 要checkpoint时使用

 save :

torch.save({'epoch': EPOCH,
            'model_state_dict': model.state_dict(),
            'optimizer_state_dict': optimizer.state_dict(),
            'loss': LOSS}, 
	    'save/to/path/model.pth')

load : 

model = MyModelDefinition(args)
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)


checkpoint = torch.load('load/from/path/model.pth')
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
epoch = checkpoint['epoch']
loss = checkpoint['loss']

 

ps. 参考的是How To Save and Load Models in PyTorch – Weights & Biases (wandb.ai)

你可能感兴趣的:(pytorch,pytorch,深度学习,人工智能)