I am unable to load a simple RNN model

This is what my model look like

class myRNN(nn.Module):
    def __init__(self, input_size, hidden_size, output_size, corp_size):
        super(myRNN, self).__init__()
        self.hidden_size = hidden_size
        self.corp_size = corp_size
        self.op_size = output_size
        self.rnn = nn.RNN(input_size=input_size, hidden_size=hidden_size)
        self.fc = nn.Linear(hidden_size, output_size)
        self.softmax = nn.LogSoftmax(dim=1)

    def forward(self, input, h_0):
        out, h_n = self.rnn(input, h_0)
        out = out.reshape(self.corp_size, self.hidden_size)
        out = self.fc(out)
        out = self.softmax(out)
        return out, h_n
    
    def initHidden(self):
        return torch.zeros(1, self.hidden_size)

This is how I have saved it after training

MODEL_STORE_PATH = "/media/khurshed2504/Data/NLP/Models/Dino_Names_RNN"
torch.save(model.state_dict(),MODEL_STORE_PATH)

But, when I try to load the state directory in another notebook,

MODEL_STORE_PATH = "/media/khurshed2504/Data/NLP/Models/Dino_Names_RNN"
testmodel = myRNN(vocab_size, 64, vocab_size, corp_size)
testmodel.load_state_dict(torch.load(MODEL_STORE_PATH, map_location = torch.device(device)))
testmodel.to(device)

It gives me an error saying **AttributeError: ‘Tensor’ object has no attribute ‘copy’
**

I have saved and loaded many models and this is the first time that I am coming across this error

The error log looks like:

AttributeError                            Traceback (most recent call last)
<ipython-input-25-97d0abbc9b0a> in <module>
      3 # testmodel = torch.load(MODEL_STORE_PATH)
      4 # testmodel = testmodel.to(device)
----> 5 testmodel.load_state_dict(torch.load(MODEL_STORE_PATH, map_location = torch.device(device)))
      6 testmodel.to(device)
      7 # HIDDEN_STORE_PATH = "/media/khurshed2504/Data/NLP/Models/English_Names_Hidden"

~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in load_state_dict(self, state_dict, strict)
    801         # copy state_dict so _load_from_state_dict can modify it
    802         metadata = getattr(state_dict, '_metadata', None)
--> 803         state_dict = state_dict.copy()
    804         if metadata is not None:
    805             state_dict._metadata = metadata

AttributeError: 'Tensor' object has no attribute 'copy'

Could you recheck the path please and make sure that torch.load returns a valid state_dict?

Your code seems to work for me:

model = myRNN(1, 1, 1, 1)
sd = model.state_dict()

model = myRNN(1, 1, 1, 1)
model.load_state_dict(sd)
> <All keys matched successfully>