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

report error

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

reason

The reason for this error is that the class object of your collecttions does not have the eval attribute.
Most of the reason is because the following statement is used to save the model.

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

But in fact it is not a model file, but a parameter file. In the model file, the complete model is stored, while in the state file, only the parameters are stored. So collections.OrderedDict is just the value of the model.

solution

Apply the following statement to save the model

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

More Ai information: Princess AiCharm
insert image description here

Guess you like

Origin blog.csdn.net/muye_IT/article/details/124956472