Is my model loaded correctly? unexpected_keys

username: Umair_Javaid
cooked: <p><code>_IncompatibleKeys(missing_keys=['features.0.weight', 'features.0.bias', 'features.2.weight', 'features.2.bias', 'features.5.weight', 'features.5.bias', 'features.7.weight', 'features.7.bias', 'features.10.weight', 'features.10.bias', 'features.12.weight', 'features.12.bias', 'features.14.weight', 'features.14.bias', 'features.18.weight', 'features.18.bias', 'features.20.weight', 'features.20.bias', 'features.22.weight', 'features.22.bias', 'features.26.weight', 'features.26.bias', 'features.28.weight', 'features.28.bias', 'features.30.weight', 'features.30.bias', 'conv6.weight', 'conv6.bias', 'conv7.weight', 'conv7.bias', 'conv8.weight', 'conv8.bias', 'conv9.weight', 'conv9.bias'], unexpected_keys=['architecture', 'epoch', 'state_dict', 'optimizer'])</code></p>
<p>Getting the above output on <code>model5.load_state_dict(torch.load(path), strict=False)</code>. I want to know is my model loaded correctly? I want to use the loaded model to predict some images. I don’t have the [‘architecture’, ‘epoch’, ‘state_dict’, ‘optimizer’] on which the model was trained. Is it possible to load the model without these unexpected_keys=[‘architecture’, ‘epoch’, ‘state_dict’, ‘optimizer’])? If not, how do I pass these values to <code>model5.load_state_dict(torch.load(path), strict=False)</code></p>
user_suspended: yes

username: Umair_Javaid
cooked: <p>And also is there a way to compare the model before and after doing <code>.load_state_dict</code></p>
user_suspended: yes

username: Umair_Javaid
cooked: <p><a class="mention" href="/u/alband">@albanD</a> any suggestions?</p>
user_suspended: yes

username: jmandivarapu1
cooked: <p>The reason is your used <code>nn.Dataparallel</code>. Can u transfer the model to cpu then save it then load it back?.</p>
<p>you can compare the model before and after by looking at any layer weights <code>model.fc1.weight</code></p>

username: Umair_Javaid
cooked: <p>I actually transfer the model to gpu before loading, could that be the problem? I can not save the model, I have an already trained model.</p>
user_suspended: yes

username: jmandivarapu1
cooked: <aside class="quote no-group" data-username="Umair_Javaid" data-post="5" data-topic="80600">
<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/umair_javaid/48/18886_2.png" class="avatar"> Umair_Javaid:</div>
<blockquote>
<p>odel to gpu before loading, could that be the problem? I can not save the model, I have an already trained mo</p>
</blockquote>
</aside>
<p>Usually not.<br>
1.But sometimes when we save the model after transferring it to cpu<br>
2.then if u want to load that saved model again it’s better to load before moving into GPU</p>

username: jmandivarapu1
cooked: <p>Please refer to these below links which are similar</p>
<aside class="quote quote-modified" data-post="1" data-topic="1686">
  <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/wasiahmad/48/248_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/solved-keyerror-unexpected-key-module-encoder-embedding-weight-in-state-dict/1686">[solved] KeyError: 'unexpected key "module.encoder.embedding.weight" in state_dict'</a> 
  </div>
  <blockquote>
    I am getting the following error while trying to load a saved model. 
KeyError: 'unexpected key "module.encoder.embedding.weight" in state_dict' 
This is the function I am using to load a saved model. (as suggested in <a href="http://pytorch.org/docs/notes/serialization.html#recommend-saving-models" rel="noopener nofollow ugc">http://pytorch.org/docs/notes/serialization.html#recommend-saving-models</a>) 
def load_model_states(model, tag):
    """Load a previously saved model states."""
    filename = os.path.join(args.save_path, tag)
    with open(filename, 'rb') as f:
        model.load_state_dict(torch.loa…
  </blockquote>
</aside>

<aside class="quote quote-modified" data-post="12" data-topic="22379">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/l/96bed5/48.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/missing-keys-unexpected-keys-in-state-dict-when-loading-self-trained-model/22379/12">Missing keys &amp; unexpected keys in state_dict when loading self trained model</a> <a class="badge-category__wrapper " href="/c/vision/5"><span data-category-id="5" data-drop-close="true" class="badge-category " title="Topics related to either pytorch/vision or vision research related topics"><span class="badge-category__name">vision</span></span></a>
  </div>
  <blockquote>
    I use  torch.save(model.state_dict(), 'file_name.pt')  to save the model. If I don’t use  nn.DataParallel  to save model, I also don’t need it when I load it, right? 
If so, when I  torch.save(model.state_dict(), 'mymodel.pt')  in one py file during training, I try to load it in a new file with  model.load_state_dict(torch.load('mymodel.pt)) , it gives me the error 
RuntimeError: Error(s) in loading state_dict for model: Missing key(s) in state_dict: "l1.W", "l1.b", "l2.W", "l2.b". 
My model is …
  </blockquote>
</aside>


username: futscdav
cooked: <p>The <code>unexpected keys</code> are actually the keys that the loaded dictionary has. As it is right now, your model is not loaded correctly. Try <code>model5.load_state_dict(torch.load(path)['state_dict'], strict=True)</code>.</p>