問題:
I was looking for alternative ways to save a trained model in PyTorch.我一直在尋找在 PyTorch 中保存訓練模型的替代方法。 So far, I have found two alternatives.到目前爲止,我找到了兩種選擇。
- torch.save() to save a model and torch.load() to load a model. torch.save()保存模型, torch.load()加載模型。
- model.state_dict() to save a trained model andmodel.load_state_dict() to load the saved model. model.state_dict()保存訓練好的模型,model.load_state_dict()加載保存的模型。
I have come across to thisdiscussion where approach 2 is recommended over approach 1.我遇到過這個討論,建議方法 2 優於方法 1。
My question is, why the second approach is preferred?我的問題是,爲什麼首選第二種方法? Is it only because torch.nn modules have those two function and we are encouraged to use them?僅僅是因爲torch.nn模塊具有這兩個功能並且我們被鼓勵使用它們嗎?
解決方案:
參考一: https://en.stackoom.com/question/2tB88參考二: https://stackoom.com/question/2tB88