Hello fellow developers,
I am hoping to achieve the following function:
target_nn.load_state_dict(nn.state_dict())
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, “stream.pt”);
torch::load(target_model, “stream.pt”);
The Error on GCC
:
error: no match for ‘operator<<’ (operand types are ‘torch::serialize::OutputArchive’ and ‘const Net’)
44 | archive << value;
and
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.