c++ - 用 C++ 训练的 pytorch 模型
问题描述
我已经训练了我的模型并将其保存以在 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++ 中使用,并且每次都显示不同的结果。有谁知道出了什么问题?
解决方案
推荐阅读
- regex - 免费代码营中级算法脚本:Pig Latin Regex 解决方案
- xamarin - Xamarin Forms IOS 在发布版本中不显示图像
- python - 减少使用 ModelChoiceField 时对数据库的头痛
- python - 将大型 numpy 数组保存为 .mat 文件
- r - 与以下类似但不同:将唯一值拆分为多个列的单独列
- python - 当我在项目根目录下的文件夹下运行脚本时,pip install --editable 如何更改 python 路径?
- python - 将 QStyles 添加到 Pyqt5
- string - 写入文本文件时字符串变量表现奇怪
- spring-boot - 如何使用注释创建在 dynamoDb 中自动生成的序列号
- mysql - Moodle 可配置报告 MySQL