39 Star 235 Fork 71

OpenDocCN / pytorch-doc-zh

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
67.md 824 Bytes
一键复制 编辑 原始数据 按行查看 历史
片刻小哥哥 提交于 2020-03-04 14:15 . 更新 1.4 翻译版本

序列化语义

原文: https://pytorch.org/docs/stable/notes/serialization.html

最佳实务

推荐的模型保存方法

序列化和还原模型有两种主要方法。

第一个(推荐)仅保存和加载模型参数:

torch.save(the_model.state_dict(), PATH)

然后再:

the_model = TheModelClass(*args, **kwargs)
the_model.load_state_dict(torch.load(PATH))

第二个保存并加载整个模型:

torch.save(the_model, PATH)

Then later:

the_model = torch.load(PATH)

但是,在这种情况下,序列化的数据将绑定到所使用的特定类和确切的目录结构,因此在其他项目中使用时或经过一些严重的重构后,序列化的数据可能会以各种方式中断。

1
https://gitee.com/OpenDocCN/pytorch-doc-zh.git
git@gitee.com:OpenDocCN/pytorch-doc-zh.git
OpenDocCN
pytorch-doc-zh
pytorch-doc-zh
master

搜索帮助