解决AttributeError: ‘collections.OrderedDict‘ object has no attribute ‘eval‘

报错

AttributeError: ‘collections.OrderedDict‘ object has no attribute ‘eval

原因

这个错误的原因就是说你这个collecttions的类对象没有eval这个属性。
绝大部分原因是因为使用了下面语句来保存模型。

torch.save(model.state_dict(),model_path)

但实际上它保存的不是模型文件,而是参数文件文件。在模型文件中,存储完整的模型,而在状态文件中,仅存储参数。因此,collections.OrderedDict只是模型的值。

解决方案

适用下面的语句来保存模型

#保存时
torch.save(model,'save_path')
#加载时
torch.load('save_path/model')

更多Ai资讯:公主号AiCharm
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/muye_IT/article/details/124956472