Problem loading saved state_dict

username: marcman411
cooked: <p>I am getting the following error trying to load my model:</p>
<blockquote>
<p>File “/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.py”, line 470, in load_state_dict<br>
for name, param in state_dict.items():<br>
AttributeError: ‘str’ object has no attribute ‘items’</p>
</blockquote>
<p>I save the model using:</p>
<pre><code>torch.save(model.state_dict(), save_path)
</code></pre>
<p>and then I try to load it using:</p>
<pre><code>model = MyModel((1000,1000))
model.load_state_dict(model_path)
</code></pre>
<p>What could be causing this?</p>

username: richard
cooked: <p>I think load_state_dict takes a state_dict, not a string path, so you’d need to do something like the following:</p>
<pre><code class="lang-auto">model = MyModel((1000,1000))
model.load_state_dict(torch.load(model_path))
</code></pre>
<p>as per <a href="http://pytorch.org/docs/master/notes/serialization.html" rel="nofollow noopener">http://pytorch.org/docs/master/notes/serialization.html</a></p>

username: marcman411
cooked: <p>Thanks, that resolved the issue.</p>
<p>However, I now get a KeyError when the parameters load:</p>
<blockquote>
<p>KeyError: ‘unexpected key “module.feature_extractor.conv_block1.0.weight” in state_dict’</p>
</blockquote>
<p><code>feature_extractor</code> is a custom module that is used as part of <code>MyModel</code> and <code>conv_block1</code> is a <code>nn.Sequential</code> class member of <code>feature_extractor</code>. Why can’t those weights be loaded? Again I saved the model as:</p>
<p><code>torch.save(model.state_dict(), save_path)</code></p>

username: marcman411
cooked: <aside class="quote no-group" data-username="marcman411" data-post="3" data-topic="9585">
<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/m/7cd45c/48.png" class="avatar"> marcman411:</div>
<blockquote>
<p>KeyError: ‘unexpected key “module.feature_extractor.conv_block1.0.weight” in state_dict’</p>
</blockquote>
</aside>
<p>Nevermind, this is answered here:</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/1">[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>


username: eshan87
cooked: <p>Sorry to interrupt, but i got an error with state_dict.copy(). it says that " AttributeError: ‘VGG’ object has no attribute ‘copy’". Can someone here help</p>
<p>Traceback (most recent call last):<br>
File “C:/AI_Projects/Pytorch_to_TensorFlow_API/test.py”, line 20, in <br>
convert.load_torch_model()<br>
File “C:\AI_Projects\Pytorch_to_TensorFlow_API\converter.py”, line 43, in load_torch_model<br>
self.torch_model.load_state_dict(torch.load(self.torch_model_path))<br>
File “C:\Python37\lib\site-packages\torch\nn\modules\module.py”, line 818, in load_state_dict<br>
state_dict = state_dict.copy()<br>
File “C:\Python37\lib\site-packages\torch\nn\modules\module.py”, line 591, in <strong>getattr</strong><br>
type(self).<strong>name</strong>, name))<br>
AttributeError: ‘VGG’ object has no attribute ‘copy’</p>