关于torch保存网络模型时报错(AttributeError: ‘collections.OrderedDict‘ object has no attribute ‘eval‘等)
使用该语句保存模型会导致评估模型时报错:用下列语句即可。
·
1.使用正确的保存模型语句
使用该语句保存模型会导致评估模型时报错:AttributeError: ‘collections.OrderedDict‘ object has no attribute ‘eval‘
torch.save(model.state_dict(),model_path)
用下列语句即可
#保存时
torch.save(model,'save_path/model')
#加载时
torch.load('save_path/model')
如果希望通过保存模型权重来加载,那么加载运行语句如下:

2.评估模型时没有导入网络模型报错
如果在评估模型的时候没有导入网络模型,会导致模型报错:torch.load 出现 AttributeError: Can‘t get attribute ‘Net‘ on module ‘__main__‘
我们只需要import模型类,或者直接把网络模型类复制到评估脚本中即可解决。
更多推荐



所有评论(0)