pytorch 加载模型参数报错:AttributeError: 'str' object has no attribute 'items'

同时用keras+tensorflow和pytorch真的是容易走火入魔,写着写着总能漏点啥,现在想加载训练好的参数:

model = my_model()
model.load_state_dict(the_path)

但是报错如下:


  File "C:\Python\Python35\site-packages\torch\nn\modules\module.py", line 476, in load_state_dict
    for name, param in state_dict.items():

AttributeError: 'str' object has no attribute 'items'

其实是因为load_state_dict的参数是torch.saved保存的对象,写的时候忘了写torch.load。正确代码如下

model = my_model()
model.load_state_dict(torch.load(the_path))
发布了55 篇原创文章 · 获赞 238 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/zkp_987/article/details/103648015