gpt4 book ai didi

python - 在 Azure ML 中保存 torch 模型

转载 作者:行者123 更新时间:2023-12-03 03:40:41 25 4
gpt4 key购买 nike

我正在使用 azure ml 文档 https://learn.microsoft.com/en-us/azure/machine-learning/tutorial-1st-experiment-bring-data训练深度学习模型。我想将模型保存在指定的路径中。这是我原来的脚本run-pytorch-data.py

train.py脚本如下: train.py

当我打印它给出的当前工作目录时,模型没有保存在指定位置:

  print (os.getcwd())
/mnt/batch/tasks/shared/LS_root/jobs/workspacecvazue/azureml/day1-experiment-data_1646679122_86b0ac64/wd/azureml/day1-experiment-data_1646679122_86b0ac64

如何将模型保存到所需位置?

最佳答案

您可以尝试以下任一方法在 Azure ML 中保存 torch 模型:

根据User Jadiel de Armas - Stack Overflow的建议:

<强>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)

<强>3。 供无法访问您的代码的其他人使用的模型:

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

**引用文献:python - Best way to save a trained model in PyTorch? - Stack Overflow , Saving and Loading Models — PyTorch Tutorials 1.10.1+cu102 documentationSave and Load the Model — PyTorch Tutorials 1.10.1+cu102 documentation

关于python - 在 Azure ML 中保存 torch 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71386206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com