如何解决推理错误:预期运算符“ forward”最多有2个自变量,但收到3个自变量
这是我的CNN网络:
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.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
我将其另存为:
1 sm = torch.jit.script(cnn)
2 sm.save("cnn.pt")
然后我将其加载到C ++中,并尝试读取一些数据以进行推断:
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,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,28}));
// Run inference
auto output = module.forward(inputs).toTensor();
cout << output.slice(/*dim=*/1,/*start=*/0,/*end=*/5) << '\n';
std::cout << "ok\n";
当我运行代码时,我得到了错误消息:
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,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]
如何解决此问题,https://pytorch.org/tutorials/advanced/cpp_export.html之后的大多数代码将不胜感激。
解决方法
最后,我在阅读https://github.com/pytorch/pytorch/issues/14000,
之后找到了答案关于在pytorch中保存模型的两种方法仍然存在一些问题。
images是2维向量。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。