简体   繁体   中英

Pytorch issue: torch.load() does not correctly load a saved model from file after closing and reopening Spyder IDE

I followed the most basic code procedure for saving and loading neural network model parameters and it works perfectly fine. After training the network, it is saved to a specified file in a specified folder in the package using the standard torch.save(model.state_dict(), file) method; when I need to rerun the program to evaluate instead of train, it is loaded using the standard model.load_state_dict(torch.load(file)) method. However, as soon as I close the Spyder IDE application and reopen the IDE and project, the torch.load does not result in the desired saved model. Note that I am testing the 'correctness' of the model by feeding inputs and checking the outputs. I am not sure if this is a problem with Spyder or with Pytorch, though I'm learning towards Spyder because the loading does work.

In short, the program works if I run it consecutively while Spyder is open, but as soon as Spyder closes and reopens, it ceases to work properly. Does anyone have a clue?

It might be due to the fact that the state of your program is known while running the IDE but when closing it the state is lost resulting in the inability to know how to load the model (because the IDE doesnt know what model you are using). To solve this, try defining a new model and loading the parameters to it via load like so:

my_model = MyModelClass(parameters).to(device)
checkpoint = torch.load(path)
my_model.load_state_dict(checkpoint)

This way the IDE knows what class is your model and can treat it properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM