Hi, I got a strange error when I load my saved model

After training, I saved my model with following code:

state = {'value_net1': agent.value_net1.state_dict(),
         'value_optimizer1': agent.value_optimizer1.state_dict(),
         'value_net2': agent.value_net2.state_dict(),
         'value_optimizer2': agent.value_optimizer2.state_dict(),
         'q_approximation': agent.q_approximation.state_dict(),
         'q_optimizer': agent.q_optimizer.state_dict(),
         }
torch.save(state, self.output_path + 'epi{}_{}.pkl'.format(self.episode_number,name))

then I need to load the data with sentence:

checkpoint = torch.load(path_of_data, map_location=torch.device('cpu'))

I got an error:


It is really strange. I alway save and load my model in this way, but I never got an error. I don’t know what wrong with my code this time.

I’m unsure what’s causing the error as I cannot reproduce it locally by trying to pass invalid inputs to torch.load. Could you check what path_of_data contains?

Thank you for your reply! I stored the variable “state” in the data_path. the “state” is a dictionary variable, which includes three model : value_net1,value_net2 and q_approximation, and their optimizers.

I cannot reproduce the issue using some random modules and optimizers for the mentioned classes:

value_net1 = nn.Linear(1, 1)
value_optimizer1 = torch.optim.SGD(value_net1.parameters(), lr=1e-3)

value_net2 = nn.Linear(2, 2)
value_optimizer2 = torch.optim.SGD(value_net2.parameters(), lr=1e-3)

q_approximation = nn.Linear(3, 3)
q_optimizer = torch.optim.SGD(q_approximation.parameters(), lr=1e-3)


state = {'value_net1': value_net1.state_dict(),
         'value_optimizer1': value_optimizer1.state_dict(),
         'value_net2': value_net2.state_dict(),
         'value_optimizer2': value_optimizer2.state_dict(),
         'q_approximation': q_approximation.state_dict(),
         'q_optimizer': q_optimizer.state_dict(),
         }
torch.save(state, "tmp.pt")

checkpoint = torch.load("tmp.pt")

and can properly load the checkpoint.

Thank you! Maybe there is something wrong with my model. But what strange is that these model perform welI during training, and can be saved. I seems need to check my models again. thank you again!