Torch::save error in torch::serialize::OutputArchive. Can't save the model

Hello fellow developers,

I am hoping to achieve the following function:


in C++ . Apparently this is not as easily done in C++ as it is in Python.

I found this post in the Libtorch Github load_state_dict in C++. Basically, suggests to save and load the model like the following:

std::stringstream stream;
torch::save(policy_net, stream);
torch::load(target_net, stream);

I have also attempted this with the same error:

torch::save(model, “”);
torch::load(target_model, “”);

The Error on GCC:

error: no match for ‘operator<<’ (operand types are ‘torch::serialize::OutputArchive’ and ‘const Net’)
44 | archive << value;


error: no matching function for call to ‘torch::serialize::InputArchive::load_from(std::__cxx11::basic_ostringstream&)’

The Error on CLang:

error: invalid operands to binary expression (‘serialize::OutputArchive’ and ‘const RLNet’)
archive << value;

I am not sure where to start to fix this error.

If you have a better way to implement target_nn.load_state_dict(nn.state_dict()) in C++ please let me know. Also, a solution to this error would really help.

Thank you.