Saving and loading model with libtorch C++

Hello,

How can I properly save and load my model with libtorch C++ ?

Indeed, my class module is defined like class Net : public torch::nn::Module {}.
After training my model, I saved it using torch::save(model, "my_model.pt"). And for Inference, I used torch::load(model, "my_model.pt"), but forward in model.forward(inputs) is not recognized.

I also tried to load with torch jit : this loading method doesn’t work.

Thanks in advance!

What is the exact error when you say the forward method is not recognized? If I understood correctly, you need to load a traced model via torch::jit::load(), the saved model should also be a traced model.

@beazt thanks for contributing!

Not recognized like forward is not a member of model.

I solved the problem by saving and loading the parameters instead of model.
Concretely, I saved model.parameters() as std::vector<torch::Tensor> after training using torch::save. And for Inference, I loaded the saved weights and didn’t use grad.

Thanks!

1 Like

I have the same problem with you. When I load the model and use it for inference, it throws out the error “Method forward is not defined.”. Could you share how you save the parameters and load it? Thanks!