How would I do load_state_dict in C++?

username: Jesper.Lindberg
cooked: <p>Hello!<br>
I would like to clone my network at every x-iteration. This is being done in python with the following code:<br>
<code>target_net.load_state_dict(policy_net.state_dict())</code> however, these functions do not seem to exist in C++?</p>
<p>Code from:<br>
<a href="https://pytorch.org/tutorials/intermediate/reinforcement_q_learning.html" rel="nofollow noopener">Reinforcement Learning (DQN) Tutorial</a></p>
<p>So what I am looking for is the equivalent to <code>target_net.load_state_dict(policy_net.state_dict())</code> in C++</p>

username: LeviViana
cooked: <p>I think the philosophy is to compile your model using PyTorch’s JIT and then loading it in CPP.</p>
<p>But, you can always redesign your architecture in CPP using the <code>torch::nn</code> namespace and then load a text-serialized version of the weights.</p>

username: Jesper.Lindberg
cooked: <p>Maybe I should have been more clear, both models are in C++. I do not have a model in python. That code-snippet is from <a href="https://pytorch.org/tutorials/intermediate/reinforcement_q_learning.html" rel="nofollow noopener">Reinforcement Learning (DQN) Tutorial</a>. and I would like to do the equivalent in C++</p>

username: LeviViana
cooked: <p>The current <a href="https://github.com/pytorch/pytorch/blob/d591a7bb823d33b2515bc06cff99cb02df2451ed/torch/nn/modules/module.py#L804" rel="nofollow noopener">implementation of load_state_dict is in Python</a>, and basically it parses the weights dictionary and <a href="https://github.com/pytorch/pytorch/blob/d591a7bb823d33b2515bc06cff99cb02df2451ed/torch/nn/modules/module.py#L786" rel="nofollow noopener">copies them into the model’s parameters</a>.</p>
<p>So I guess you’ll need to do the same in CPP.</p>

username: alireza_dizaji
cooked: <p>In most cases the following code works, though it is not as comprehensive as <code>load_state_dict</code> of Pytorch in Python. Beforehand, you need to store the state_dict as a dictionary type <code>(Dict[str, Tensor])</code> in python.</p>
<pre><code class="lang-auto">// Model class is inherited from public nn::Module
std::vector&lt;char&gt; Model::get_the_bytes(std::string filename) {
    std::ifstream input(filename, std::ios::binary);
    std::vector&lt;char&gt; bytes(
        (std::istreambuf_iterator&lt;char&gt;(input)),
        (std::istreambuf_iterator&lt;char&gt;()));

    input.close();
    return bytes;
}

void Model::load_parameters(std::string pt_pth) {
  std::vector&lt;char&gt; f = this-&gt;get_the_bytes(pt_pth);
  c10::Dict&lt;IValue, IValue&gt; weights = torch::pickle_load(f).toGenericDict();

  const torch::OrderedDict&lt;std::string, at::Tensor&gt;&amp; model_params = this-&gt;named_parameters();
  std::vector&lt;std::string&gt; param_names;
  for (auto const&amp; w : model_params) {
    param_names.push_back(w.key());
  }

  torch::NoGradGuard no_grad;
  for (auto const&amp; w : weights) {
      std::string name = w.key().toStringRef();
      at::Tensor param = w.value().toTensor();

      if (std::find(param_names.begin(), param_names.end(), name) != param_names.end()){
        model_params.find(name)-&gt;copy_(param);
      } else {
        std::cout &lt;&lt; name &lt;&lt; " does not exist among model parameters." &lt;&lt; std::endl;
      };

  }
}
</code></pre>

username: alireza_dizaji
cooked: <p>Also in order to load buffer tensors, e.g. batch norm running mean and var, replicate the steps for named_buffers().</p>