【问题标题】:how to save a Pytorch model?如何保存 Pytorch 模型?
【发布时间】:2021-06-23 12:59:46
【问题描述】:

我是深度学习的新手,我想知道如何在 Pytorch 中保存最终模型?我尝试了一些提到的东西,但我对如何保存模型以及如何加载它感到困惑?

【问题讨论】:

标签: pytorch


【解决方案1】:

保存:

# save the weights of the model to a .pt file
torch.save(model.state_dict(), "your_model_path.pt")

加载:

# load your model architecture/module
model = YourModel()
# fill your architecture with the trained weights
model.load_state_dict(torch.load("your_model_path.pt"))

【讨论】:

  • 我使用 torch.save() 方法保存模型,但我现在无法理解如何加载它。我是否必须为此创建一个不同的程序,如果是,我必须传递哪些参数。加载函数将从保存的模型中取出多少东西?
  • “不同的程序”是什么意思?您可以从所需的任何脚本加载 .pt 文件。我通常有两个函数,train() 和 test()。在 train() 中,我训练模型并在每个 epoch 中保存它。在 test() 我只是加载模型..
  • 我们能不能把save函数和load函数放在同一个程序里,以后需要运行的时候?
猜你喜欢
  • 2022-10-09
  • 1970-01-01
  • 1970-01-01
  • 2021-12-15
  • 2019-09-26
  • 2020-11-15
  • 2021-07-15
  • 2018-04-29
  • 2021-06-13
相关资源
最近更新 更多