最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - Is torch::jit::trace not available in libtorch? - Stack Overflow

programmeradmin0浏览0评论

I am using libtorch in c++.

In python, torch.jit.trace function exists and works fine.

However, torch::jit::trace does not work in libtorch.

It said there is no "trace" member in namespace torch::jit.

Here is my simple example code.

#include <torch/torch.h>
#include <torch/script.h>
#include <iostream>

struct MyNetImpl : torch::nn::Module {
    torch::nn::Linear fc{ nullptr };
    MyNetImpl(int in_features, int out_features) {
        fc = register_module("fc", torch::nn::Linear(in_features, out_features));
    }
    torch::Tensor forward(torch::Tensor x) {
        return torch::relu(fc->forward(x));
    }
};
TORCH_MODULE(MyNet);

int main() {
    MyNet model(10, 2);
    model->eval();

    // Create an example input
    auto example_input = torch::randn({ 1, 10 });

    // NOTE: torch::jit::trace is a function:
    auto traced_model = torch::jit::trace(model, example_input);

    // Save TorchScript module
    traced_model.save("model_traced.pt");
    std::cout << "Traced model saved successfully!\n";

    return 0;
}

My code is wrong or torch::jit::trace is not available in libtorch?

What i was trying to do is to save model in c++ and load it in python.

发布评论

评论列表(0)

  1. 暂无评论