【问题标题】:Save and load checkpoint pytorch保存和加载检查点 pytorch
【发布时间】:2018-11-29 11:35:43
【问题描述】:

我制作一个模型并将配置保存为:

def checkpoint(state, ep, filename='./Risultati/checkpoint.pth'):  
    if ep == (n_epoch-1):
        print('Saving state...')
        torch.save(state,filename)
checkpoint({'state_dict':rnn.state_dict()},ep) 

然后我想加载这个配置:

state_dict= torch.load('./Risultati/checkpoint.pth')
    rnn.state_dict(state_dict)

当我尝试时,这是错误:

Traceback (most recent call last):
File "train.py", line 288, in <module>
rnn.state_dict(state_dict)
File "/home/marco/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 593, in state_dict
destination._metadata[prefix[:-1]] = dict(version=self._version)
AttributeError: 'dict' object has no attribute '_metadata'

我哪里做错了?

提前谢谢

【问题讨论】:

    标签: python-3.x pytorch rnn checkpointing


    【解决方案1】:

    您需要加载存储在您加载的字典中的rnn.state_dict()

    rnn.load_state_dict(state_dict['state_dict'])
    

    查看load_state_dict 方法了解更多信息。

    【讨论】:

      猜你喜欢
      • 2021-11-29
      • 2019-06-13
      • 2021-12-15
      • 2019-09-26
      • 2021-10-05
      • 2019-05-16
      • 2023-01-26
      • 2022-11-07
      • 2021-01-15
      相关资源
      最近更新 更多