how to pass trained weight to neural network module

tsungruihon: <p>Suppose i used own data and trained a <code>conv1d</code>, how could we pass the weight to <code>conv1d</code> in c++ like what the <code>PyTorch</code>  acts ?</p>
<p>Noticed that the implementation of <code>conv1d</code> in <code>PyTorch</code>, we could update the parameters like <code>in_channels</code>, <code>out_channels</code>, etc in the <code>__init__</code> function. If we want to update the <code>weights</code> and <code>bias</code>, which are from pretrained model, we could rewrite the <code>Conv1d</code>, which may not so difficult.</p>
<pre><code class="lang-auto">class Conv1d(_ConvNd):

    def __init__(self, in_channels, out_channels, kernel_size, stride=1,
                 padding=0, dilation=1, groups=1,
                 bias=True, padding_mode='zeros'):
        kernel_size = _single(kernel_size)
        stride = _single(stride)
        padding = _single(padding)
        dilation = _single(dilation)
        super(Conv1d, self).__init__(
            in_channels, out_channels, kernel_size, stride, padding, dilation,
            False, _single(0), groups, bias, padding_mode)

    def forward(self, input):
        if self.padding_mode == 'circular':
            expanded_padding = ((self.padding[0] + 1) // 2, self.padding[0] // 2)
            return F.conv1d(F.pad(input, expanded_padding, mode='circular'),
                            self.weight, self.bias, self.stride,
                            _single(0), self.dilation, self.groups)
        return F.conv1d(input, self.weight, self.bias, self.stride,
                        self.padding, self.dilation, self.groups)
</code></pre>
<p>While notice the <code>conv1d</code> implementation in libtorch, noticed that</p>
<pre><code class="lang-auto">namespace nn {
Conv1dImpl::Conv1dImpl(
    Conv1dOptions options_)
    : ConvNdImpl(
        detail::ConvNdOptions&lt;1&gt;(
          /*in_channels=*/options_.in_channels(),
          /*out_channels=*/options_.out_channels(),
          /*kernel_size=*/options_.kernel_size())
          .stride(options_.stride())
          .padding(options_.padding())
          .dilation(options_.dilation())
          .transposed(false)
          .output_padding(0)
          .groups(options_.groups())
          .bias(options_.bias())
          .padding_mode(options_.padding_mode())) {}

Tensor Conv1dImpl::forward(const Tensor&amp; input) {
  if (c10::get_if&lt;enumtype::kCircular&gt;(&amp;options.padding_mode())) {
    std::vector&lt;int64_t&gt; expanded_padding = {((*options.padding())[0] + 1) / 2, (*options.padding())[0] / 2};
    return F::detail::conv1d(
      F::detail::pad(input, expanded_padding, torch::kCircular, 0),
      weight, bias,
      options.stride(),
      /*padding=*/0,
      options.dilation(),
      options.groups());
  }
  return F::detail::conv1d(
    input,
    weight,
    bias,
    options.stride(),
    options.padding(),
    options.dilation(),
    options.groups());
}
</code></pre>
<p>So how could we pass the weight in the c++ version ?</p>

21fa417e3fb06e56040c: <p>You can copy the weight as follows</p>
<pre><code class="lang-auto">	conv1-&gt;weight.copy_(weight);
	conv1-&gt;bias.copy_(bias);
</code></pre>
<p>But this method is not good.<br>
If you want to copy the weight of a Python model, use below code<br>
Currently you can use</p>
<p>but I know other ways to develop now</p>
<pre><code class="lang-python">#python
import torch
import torch.jit

cov1 = torch.nn.Conv1d(3,64,7)
torch.jit.script(cov1).save('cov1.pt')

#c++
torch::nn::Conv2d conv1(torch::nn::Conv2dOptions(3, 64, 7);
torch::load(conv1,"cov1.pt");

</code></pre>

tsungruihon: <p>Wow, thanks a lot.<img src="https://discuss.pytorch.org/images/emoji/apple/+1.png?v=9" title=":+1:" class="emoji" alt=":+1:"><img src="https://discuss.pytorch.org/images/emoji/apple/+1.png?v=9" title=":+1:" class="emoji" alt=":+1:"><img src="https://discuss.pytorch.org/images/emoji/apple/+1.png?v=9" title=":+1:" class="emoji" alt=":+1:"></p>