Inference error:Expected at most 2 argument(s) for operator 'forward', but received 3 argumen

hisbug: <p>Here is my cnn network:</p>
<pre><code>class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.conv1 = nn.Sequential(  # input shape (1, 28, 28)
            nn.Conv2d(
                in_channels=1,
                out_channels=16,
                kernel_size=5,
                stride=1,
                padding=2,
            ),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2),
        )
        self.conv2 = nn.Sequential(
            nn.Conv2d(16, 32, 5, 1, 2),
            nn.ReLU(),
            nn.MaxPool2d(2),
        )
        self.out = nn.Linear(32 * 7 * 7, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = x.view(x.size(0), -1)
        output = self.out(x)
        return output, x
</code></pre>
<p>I save it as:</p>
<pre><code>1  sm = torch.jit.script(cnn)
2  sm.save("cnn.pt")
</code></pre>
<p>Then I load it in C++ and I am trying to read some data to make some inference:</p>
<pre><code>vector&lt;vector&lt;double&gt;&gt;images;
read_Mnist_Images("/home/heye/python/mnist/MNIST/raw/t10k-images-idx3-ubyte", images);
// Create a vector of inputs.
std::vector&lt;torch::jit::IValue&gt; inputs;
inputs.push_back(torch::ones({10, 1, 28, 28}));

auto tensor = torch::empty(10 * 1 * 28 * 28);
float *data = tensor.data&lt;float&gt;();

for (int i = 0; i &lt; 10; i++) {
  for (int j = 0; j &lt; 1; j++) {
    for (int k = 0; k &lt; 28; k++) {
      for (int l = 0; l &lt; 28; l++) {
        *data++ = images[i][28 * k + l] / 255;
      }
    }
  }
}
inputs.emplace_back(tensor.resize_({10, 1, 28, 28}));
// Run inference
auto output = module.forward(inputs).toTensor();
cout &lt;&lt; output.slice(/*dim=*/1, /*start=*/0, /*end=*/5) &lt;&lt; '\n';
std::cout &lt;&lt; "ok\n";
</code></pre>
<p>and I get the error message:</p>
<pre><code>terminate called after throwing an instance of 'c10::Error'
  what():  Expected at most 2 argument(s) for operator 'forward', but received 3 argument(s). Declaration: forward(ClassType&lt;CNN&gt; self, Tensor x) -&gt; ((Tensor, Tensor)) (checkAndNormalizeInputs at /pytorch/aten/src/ATen/core/function_schema_inl.h:245)
frame #0: c10::Error::Error(c10::SourceLocation, std::string const&amp;) + 0x33 (0x7fa4d08f2813 in /home/heye/torch1.3_cpu/torch/lib/libc10.so)
frame #1: &lt;unknown function&gt; + 0x323f830 (0x7fa4d3d56830 in /home/heye/torch1.3_cpu/torch/lib/libtorch.so)
frame #2: torch::jit::Function::operator()(std::vector&lt;c10::IValue, std::allocator&lt;c10::IValue&gt; &gt;, std::unordered_map&lt;std::string, c10::IValue, std::hash&lt;std::string&gt;, std::equal_to&lt;std::string&gt;, std::allocator&lt;std::pair&lt;std::string const, c10::IValue&gt; &gt; &gt; const&amp;) + 0x36 (0x7fa4d3d54ec6 in /home/heye/torch1.3_cpu/torch/lib/libtorch.so)
frame #3: torch::jit::script::Method::operator()(std::vector&lt;c10::IValue, std::allocator&lt;c10::IValue&gt; &gt;, std::unordered_map&lt;std::string, c10::IValue, std::hash&lt;std::string&gt;, std::equal_to&lt;std::string&gt;, std::allocator&lt;std::pair&lt;std::string const, c10::IValue&gt; &gt; &gt; const&amp;) + 0xc9 (0x7fa4d3d13529 in /home/heye/torch1.3_cpu/torch/lib/libtorch.so)
frame #4: torch::jit::script::Module::forward(std::vector&lt;c10::IValue, std::allocator&lt;c10::IValue&gt; &gt;) + 0xe2 (0x42fb76 in ./read_pt)
frame #5: main + 0x43e (0x4299ab in ./read_pt)
frame #6: __libc_start_main + 0xf5 (0x7fa4d00e8495 in /lib64/libc.so.6)
frame #7: ./read_pt() [0x428849]
</code></pre>
<p>how to solve this problem, what’s wrong with it, the code following from <a href="https://pytorch.org/tutorials/advanced/cpp_export.html" rel="nofollow noopener">https://pytorch.org/tutorials/advanced/cpp_export.html</a>, any help will be appreciated.</p>

hisbug: <p>I find the answer, thanks anyway.<br>
</p><aside class="onebox stackexchange">
  <header class="source">
      <a href="https://stackoverflow.com/questions/63725981/inference-errorexpected-at-most-2-arguments-for-operator-forward-but-recei" target="_blank" rel="nofollow noopener">stackoverflow.com</a>
  </header>
  <article class="onebox-body">
      <a href="https://stackoverflow.com/users/5500697/coco" target="_blank" rel="nofollow noopener">
    <img alt="Coco" src="https://www.gravatar.com/avatar/f4d29863484fb3c83b2971a579fef3de?s=128&amp;d=identicon&amp;r=PG&amp;f=1" class="thumbnail onebox-avatar" width="60" height="60">
  </a>
<h4>
  <a href="https://stackoverflow.com/questions/63725981/inference-errorexpected-at-most-2-arguments-for-operator-forward-but-recei" target="_blank" rel="nofollow noopener">Inference error:Expected at most 2 argument(s) for operator 'forward', but received 3 argumen</a>
</h4>

<div class="tags">
  <strong>c++, deep-learning, pytorch</strong>
</div>

<div class="date">
  asked by
  
  <a href="https://stackoverflow.com/users/5500697/coco" target="_blank" rel="nofollow noopener">
    Coco
  </a>
  on <a href="https://stackoverflow.com/questions/63725981/inference-errorexpected-at-most-2-arguments-for-operator-forward-but-recei" target="_blank" rel="nofollow noopener">02:40PM - 03 Sep 20 UTC</a>
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


Soonmyun_Jang: <p>Hi, could you share me the solution? I cannot open the link you posted.</p>