我已经训练了我的模型并将其保存以在 C++ 环境中使用。当我尝试用 C++ 预测结果时,与我的 pytorch 模型相比,它显示了不同的结果。我已经使用了 pytorch 教程中所示的确切内容。这是我的 C++ 代码。
#include <torch/script.h>
#include <memory>
#include <iostream>
int main(int argc, const char* argv[])
{
if (argc != 2) {
std::cerr << "usage: main <path-to-exported-script-module>\n";
return -1;
}
try {
// Deserialize the ScriptModule from a file using torch::jit::load().
torch::jit::script::Module module;
module = torch::jit::load("/home/sunny/neural/diffusion2.pt");
// Create a vector of inputs.
double x;
std::cout<<"Please enter the value of x ";
std::cin>>x;
double t;
std::cout<<"Please enter the value of t ";
std::cin>>t;
std::vector<torch::jit::IValue> inputs;
inputs.push_back(torch::tensor({{x,t}}));
// Execute the model and turn its output into a tensor.
torch::Tensor output = module.forward((inputs)).toTensor();
std::cout << "u(x,t) = " << output << std::endl;
}
catch (const c10::Error& e) {
std::cerr << "error loading the model\n";
return -1;
}
}
还有一件事。我多次尝试保存我的模型以在 C++ 中使用,并且每次都显示不同的结果。有谁知道出了什么问题?