Load a saved model which is saved by torch.save([model, criterion, optimizer], f)

username: fbrah
cooked: <p>Hi,</p>
<p>I trained a model using an existing code. The model is saved like following during the training:</p>
<pre><code class="lang-auto">def model_save(fn):
    with open(fn, 'wb') as f:
        torch.save([model, criterion, optimizer], f)
</code></pre>
<p>During inference the original code loads the model like this:</p>
<pre><code class="lang-auto">    with open(path, 'rb') as f:
        model = torch.load(f, map_location=lambda storage, loc: storage)

    model.eval()
</code></pre>
<p>However since model is saved in a list, <code>model.eval()</code> will threw error.<br>
I used the first element of the list which is supposed to be model itself but I am not sure if the model state_dict() is loaded at all.</p>

username: ptrblck
cooked: <p>You could try to manually check, if all parameters were loaded appropriately.<br>
The recommended way is to store the <code>state_dicts</code> instead of the objects directly, as your approach might break in various ways (described <a href="https://pytorch.org/docs/stable/notes/serialization.html">here</a>).</p>

username: fbrah
cooked: <p>I see.<br>
besides the breaking issue, the loading code is fine?<br>
I mean we dont need the 2nd and third element of torch.load output, since they are criterion and optimizer, right? all parameters should be in model?</p>

username: ptrblck
cooked: <p>All parameters should be loaded properly, if you don’t see any error message.<br>
The optimizer’s <code>state_dict</code> might be necessary to continue your training, if your optimizer uses internal states, e.g. <code>Adam</code> uses running estimates of the parameters.<br>
If you don’t restore the optimizer, you might see a loss spike when continuing the training.</p>

username: fbrah
cooked: <p>Thanks for the explanation.<br>
Actually I trained the model again from scratch and saved the state_dict like <code>torch.save(model.state_dict(), fn)</code> and load the state_dict accordingly. However I am getting this error when I try to test the model:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "pytorch_src/test_evaluate.py", line 144, in &lt;module&gt;
    model_load(args.save)
  File "pytorch_src/test_evaluate.py", line 109, in model_load
    model.load_state_dict(torch.load(fn))
  File "/home/anaconda3/envs/py36/lib/python3.7/site-packages/torch/nn/modules/module.py", line 839, in load_state_dict
    self.__class__.__name__, "\n\t".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for RNNModel:
        Unexpected key(s) in state_dict: "rnns.0.module.weight_hh_l0", "rnns.1.module.weight_hh_l0", "rnns.2.module.weight_hh_l0".
</code></pre>
<p>I guess there must be a mismatch between model and the state_dict keys but not sure how that happened.<br>
‘rnns.0.module.’ prefix get added to original keys I guess. Do you have any idea?</p>

username: alex.veuthey
cooked: <p>Was the original model saved with DataParallel activated? If so, that explains the <code>module</code> prefix occuring in your state_dict. See <a href="https://discuss.pytorch.org/t/chexnet-code-error-runtimeerror-error-s-in-loading-state-dict-for-dataparallel-missing-key-s-in-state-dict-module-densenet121-features-conv0-weight/43790/3">my previous answer here</a> for details on how to fix this (in both saving and loading situations).</p>