How to change a loaded model to evaluation mode in C++

I loaded a model in my C++ code in this way:

std::shared_ptr<torch::jit::script::Module> model = torch::jit::load("model.pt");

Is there an equivalent of the python model.eval() to turn it into evaluation mode in the C++ frontend?

Yes, just call model.eval()

related doc:
https://pytorch.org/cppdocs/api/classtorch_1_1nn_1_1_module.html

2 Likes

When I use model.eval(), the compiler says:

error: ‘struct torch::jit::script::Module’ has no member named ‘eval’
net->eval();

Do I need to include some additional header file?

You are right, unfortunately it is not that easy. The main problem is that jit::scriptmodule and nn::module do not have the same set of methods. I do not know the solution, but in this post they write that eval is not yet implemented in scriptmodule, but they suggest a workaround and there seems to be a brand new fix.

https://github.com/pytorch/pytorch/issues/13991

@Panpan_CAI calling model.eval() on a torch::jit::script::Module already works. Example: