栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

.torch.save与torch.jit.save

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

.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.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/982547.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号