Can't load properly a saved model

Reading this bear in mind that I’m a beginner with pytorch. I think saving the model went wine, I did it using :

	torch.save({
                'epoch': self.epochs,
                'model_state_dict': self.model.state_dict(),
                'optimizer_state_dict': self.optimizer.state_dict()
                }, 'sav

inside the for loop on epochs.

Then, to load the model, I do this after initializing the model and the optimizer.

    if os.path.exists('saved') :
        print('Loading saved model')
        checkpoint = torch.load('saved')
        model.load_state_dict(checkpoint['model_state_dict'])
        optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
        epoch = checkpoint['epoch']

Here’s the error :

Missing key(s) in state_dict: "frame_level_rnns.0.h0", "frame_level_rnns.0.input_expand.bias", "frame_level_rnns.0.input_expand.weight_g", "frame_level_rnns.0.input_expand.weight_v", "frame_level_rnns.0.rnn.weight_ih_l0", "frame_level_rnns.0.rnn.weight_hh_l0", "frame_level_rnns.0.rnn.bias_ih_l0", "frame_level_rnns.0.rnn.bias_hh_l0", "frame_level_rnns.0.rnn.weight_ih_l1", "frame_level_rnns.0.rnn.weight_hh_l1", "frame_level_rnns.0.rnn.bias_ih_l1", "frame_level_rnns.0.rnn.bias_hh_l1", "frame_level_rnns.0.upsampling.bias", "frame_level_rnns.0.upsampling.conv_t.weight_g", "frame_level_rnns.0.upsampling.conv_t.weight_v"

and then shortly after

	Unexpected key(s) in state_dict: "model.frame_level_rnns.0.h0", "model.frame_level_rnns.0.input_expand.bias", "model.frame_level_rnns.0.input_expand.weight_g", "model.frame_level_rnns.0.input_expand.weight_v", "model.frame_level_rnns.0.rnn.weight_ih_l0", "model.frame_level_rnns.0.rnn.weight_hh_l0", "model.frame_level_rnns.0.rnn.bias_ih_l0", "model.frame_level_rnns.0.rnn.bias_hh_l0", "model.frame_level_rnns.0.rnn.weight_ih_l1", "model.frame_level_rnns.0.rnn.weight_hh_l1", "model.frame_level_rnns.0.rnn.bias_ih_l1", "model.frame_level_rnns.0.rnn.bias_hh_l1", "model.frame_level_rnns.0.upsampling.bias"

So instead of loading ‘a’ it loads ‘model.a’ I guess, but I don’t get what I am doing wrong. Can you help me out please ?

Also, I’d like to add that the optimizer loads just fine (when putting the line about the optimizer just before the line for the model) and also the epochs.

So I hacked it, basically removing the “model.” part just before key. Still I’m not tagging it as solved because I wanna know why I had this error and also find a more elegant way to deal with it !

I guess the model attribute is added, as it seems you are using another class to store your self.model, self.optimizer and probably other objects.
Are you working with some high-level API or did you create this wrapper manually?