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

Here is my cnn network:

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

I save it as:

1  sm = torch.jit.script(cnn)
2  sm.save("cnn.pt")

Then I load it in C++ and I am trying to read some data to make some inference:

vector<vector<double>>images;
read_Mnist_Images("/home/heye/python/mnist/MNIST/raw/t10k-images-idx3-ubyte", images);
// Create a vector of inputs.
std::vector<torch::jit::IValue> inputs;
inputs.push_back(torch::ones({10, 1, 28, 28}));

auto tensor = torch::empty(10 * 1 * 28 * 28);
float *data = tensor.data<float>();

for (int i = 0; i < 10; i++) {
  for (int j = 0; j < 1; j++) {
    for (int k = 0; k < 28; k++) {
      for (int l = 0; l < 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 << output.slice(/*dim=*/1, /*start=*/0, /*end=*/5) << '\n';
std::cout << "ok\n";

and I get the error message:

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<CNN> self, Tensor x) -> ((Tensor, Tensor)) (checkAndNormalizeInputs at /pytorch/aten/src/ATen/core/function_schema_inl.h:245)
frame #0: c10::Error::Error(c10::SourceLocation, std::string const&) + 0x33 (0x7fa4d08f2813 in /home/heye/torch1.3_cpu/torch/lib/libc10.so)
frame #1: <unknown function> + 0x323f830 (0x7fa4d3d56830 in /home/heye/torch1.3_cpu/torch/lib/libtorch.so)
frame #2: torch::jit::Function::operator()(std::vector<c10::IValue, std::allocator<c10::IValue> >, std::unordered_map<std::string, c10::IValue, std::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, c10::IValue> > > const&) + 0x36 (0x7fa4d3d54ec6 in /home/heye/torch1.3_cpu/torch/lib/libtorch.so)
frame #3: torch::jit::script::Method::operator()(std::vector<c10::IValue, std::allocator<c10::IValue> >, std::unordered_map<std::string, c10::IValue, std::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, c10::IValue> > > const&) + 0xc9 (0x7fa4d3d13529 in /home/heye/torch1.3_cpu/torch/lib/libtorch.so)
frame #4: torch::jit::script::Module::forward(std::vector<c10::IValue, std::allocator<c10::IValue> >) + 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]

how to solve this problem, what’s wrong with it, the code following from https://pytorch.org/tutorials/advanced/cpp_export.html, any help will be appreciated.

I find the answer, thanks anyway.

1 Like

Hi, could you share me the solution? I cannot open the link you posted.