torch save and read model

method one:

Save and read the entire model:

torch.save(model, PATH)
model = torch.load(PATH)

Method Two:

Only save the model's state_dict(), read:

torch.save(model.state_dict(), PATH)
state_dict = torch.load(PATH)

At this time, only the state_dict is read, and you need

new_model.load_state_dict(torch.load(PATH)) # 其中的new_model是新初始化的模型

problem

Insert picture description hereDamn , directly saving the model overturned, and an error similar to this kind of error occurred. In the future, I will only save the state_dict.

Guess you like

Origin blog.csdn.net/jokerxsy/article/details/108745606