.torch.save与torch.jit.save

1.torch.jit.save

torch.jit.save用来保存编译后的模型,支持跨平台,要注意模型中只能使用pytorch的函数。

jit.save支持保存script类型和trace类型的模型,其中script为全量模型,trace为仅保存运行过的路径

2.torch.save

torch.save有三种使用场景,参考:https://stackoverflow.com/questions/42703500/best-way-to-save-a-trained-model-in-pytorch

1.保存模型用来做测试

torch.save(model.state_dict(), filepath)
#Later to restore:
model.load_state_dict(torch.load(filepath))
model.eval()

2.用来恢复训练状态

state = {
    'epoch': epoch,
    'state_dict': model.state_dict(),
    'optimizer': optimizer.state_dict(),
    ...
}
torch.save(state, filepath)
#later
model.load_state_dict(state['state_dict'])
optimizer.load_state_dict(state['optimizer'])

Since you are resuming training, DO NOT call model.eval() once you restore the states when loading.

3.用来给不不知道你的网络结构的人使用

torch.save(model, filepath)
# Then later:
model = torch.load(filepath)

This way is still not bullet proof and since pytorch is still undergoing a lot of changes, I wouldn’t recommend it.

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