Problem loading model from state dict

kirk86: <p>Hi everyone I am getting an error wen trying to load the model from the saved checkpoint file.</p>
<p>Model:</p>
<pre><code class="lang-auto">class RecNet2(nn.Module):
    def __init__(self, in_shape, num_classes=12 ):
        super(RecNet2, self).__init__()

        self.layer1 = nn.GRU(in_shape[-1], 256)
        self.layer2 = nn.LSTM(256, 512)
        self.fc1 = nn.Linear(512, 256)
        self.fc2 = nn.Linear(40 * 256, num_classes)

    def forward(self, x):
        x, h_out = self.layer1(x)
        x = F.dropout(x, p=0.5)
        x, h_ou2 = self.layer2(x)
        x = F.dropout(x, p=0.3)
        x = self.fc1(x)
        x = self.fc2(x.view(-1, 40 * 256))

        return x  #logits
</code></pre>
<p>Error message:</p>
<pre><code class="lang-auto">&gt;&gt;&gt; model.load_state_dict(fle)
Traceback (most recent call last):
  File "/home/user/miniconda3/envs/torch/lib/python3.6/site-packages/torch/nn/modules/module.py", line 482, in load_state_dict
    own_state[name].copy_(param)
RuntimeError: invalid argument 2: sizes do not match at /opt/conda/conda-bld/pytorch_1512387374934/work/torch/lib/THC/generic/THCTensorCopy.c:101

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/home/user/miniconda3/envs/torch/lib/python3.6/site-packages/torch/nn/modules/module.py", line 487, in load_state_dict
    .format(name, own_state[name].size(), param.size()))
RuntimeError: While copying the parameter named layer1.weight_ih_l0, whose dimensions in the model are torch.Size([768]) and whose dimensions in the checkpoint are torch.Size([768, 101]).
</code></pre>
<p>The <code>.pth</code> file has been created during training of the above model. Any suggestions on how to investigate this and why are there mismatch tensor shape errors?</p>

smth: <aside class="quote no-group" data-username="kirk86" data-post="1" data-topic="12396">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kirk86/48/151_2.png" class="avatar"> kirk86:</div>
<blockquote>
<p>RuntimeError: While copying the parameter named layer1.weight_ih_l0, whose dimensions in the model are torch.Size([768]) and whose dimensions in the checkpoint are torch.Size([768, 101]).</p>
</blockquote>
</aside>
<p>that says that <code>layer</code> of your model has been changed from the checkpoint to the definition. In your case it is nn.GRU</p>

kirk86: <p>Thanks Soumith. Appreciate it!</p>

Dinesh_Shrestha: <p>RuntimeError                              Traceback (most recent call last)<br>
 in ()<br>
1 model = CaptionModel_B(2048, 50, 160, vocab_size, num_layers=1)<br>
----&gt; 2 model.load_state_dict(torch.load(‘im_caption_35.727_0.316_epoch_20.pth.tar’, map_location=‘cpu’))<br>
3 solver = NetSolver(data, model)</p>
<p>/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in load_state_dict(self, state_dict, strict)<br>
843         if len(error_msgs) &gt; 0:<br>
844             raise RuntimeError(‘Error(s) in loading state_dict for {}:\n\t{}’.format(<br>
–&gt; 845                                self.<strong>class</strong>.<strong>name</strong>, “\n\t”.join(error_msgs)))<br>
846         return _IncompatibleKeys(missing_keys, unexpected_keys)<br>
847</p>
<p>RuntimeError: Error(s) in loading state_dict for CaptionModel_B:<br>
size mismatch for rnn.embed.weight: copying a param with shape torch.Size([9080, 50]) from checkpoint, the shape in current model is torch.Size([8947, 50]).<br>
size mismatch for rnn.linear.weight: copying a param with shape torch.Size([9080, 160]) from checkpoint, the shape in current model is torch.Size([8947, 160]).<br>
size mismatch for rnn.linear.bias: copying a param with shape torch.Size([9080]) from checkpoint, the shape in current model is torch.Size([8947]).</p>
<p>Can you help me to solve this error?</p>