Model object has no attribute 'load_state_dict'

werdas34: <p>Hello,</p>
<p>I want to load a model from a file and determine the model parameters.</p>
<pre><code class="lang-auto">  model_path = os.path.normpath(args.model)
  model = model_path.split(os.sep)[-1]

  splitted = model.split("_")
  splitted = splitted[0:-3]
  section = "_".join(splitted)

  model = CorefModel(args.config_file, section) # &lt;- this works
  model.load_state_dict(torch.load(args.model)) # &lt;- that not

  total_params = sum(	param.numel() for param in model.parameters() )
  print("The model " + section + " has a parameter weight of " + total_params)
</code></pre>
<p>I get the Error AttributeError: ‘CorefModel’ object has no attribute ‘load_state_dict’</p>
<p>The save-Method:</p>
<pre><code class="lang-auto">  def save_weights(self):
      """ Saves trainable models as state dicts. """
      to_save: List[Tuple[str, Any]] = \
          [(key, value) for key, value in self.trainable.items()
           if self.config.bert_finetune or key != "bert"]
      to_save.extend(self.optimizers.items())
      to_save.extend(self.schedulers.items())

      if self.epochs_trained == self.config.train_epochs:
          time = datetime.strftime(datetime.now(), "%Y.%m.%d_%H.%M")
          path = os.path.join(self.config.data_dir,
                              f"{self.config.section}"
                              f"_(e{self.epochs_trained}_{time}).pt")
          savedict = {name: module.state_dict() for name, module in to_save}
          savedict["epochs_trained"] = self.epochs_trained  # type: ignore
          torch.save(savedict, path)
</code></pre>
<p>Do I need to implement load_state_dict myself? Or does this only work on a gpu? Currently I am testing it on a CPU?</p>

ptrblck: <p>Could you explain how <code>CorefModel</code> is defined and post its class definition?</p>

werdas34: <p><a href="https://github.com/vdobrovolskii/wl-coref/blob/master/coref/coref_model.py#L358" rel="noopener nofollow ugc">build_model()</a> and at the top is the CorefModel class.</p>

ptrblck: <p>Thanks for the link. Based on the code it’s not derived from <code>nn.Module</code> but a custom class, which doesn’t provide the <code>load_state_dict</code> method. You might want to use the <code>load_weights</code> function instead, which is also a custom method of this class.</p>

werdas34: <p>I added a return state_dicts in the load_weights method to access the dict. But I can’t apply load_state_dict to the dict and in load_weights return self.load_dict(state_dicts) doesn’t work.</p>
<pre><code class="lang-auto">def load_weights():
     # added below
     return state_dict # works but..
     return self.load_state_dict(state_dict) # does not work

# in my file
model = CorefModel(args.config_file, section)
model = model.load_weights(args.model) # cannot use load_state_dict

# and state_dict (the value) has no has no parameters attribute. Missing load_state_dict()
</code></pre>
<p>Is there a workaround?</p>

ptrblck: <aside class="quote no-group" data-username="werdas34" data-post="5" data-topic="162371">
<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/w/c37758/48.png" class="avatar"> werdas34:</div>
<blockquote>
<p>self.load_dict(state_dicts) doesn’t work.</p>
</blockquote>
</aside>
<p>That’s expected since your custom <code>CorefModel</code> class does not define a <code>load_state_dict</code> method and you would need to call <code>load_state_dict</code> on the actual model which is derived from <code>nn.Module</code> not your custom class.</p>

werdas34: <p>Ok I see. So the easy way does not work.<br>
And how do I get the model parameters? Do I have to run the dict myself and add up the values?<br>
Or do I have to return self.optimizers, self.schedulers and self.trainable and then apply model.parameters() to them?</p>