I get an error on the archive.load_from(std::istringstream(mem)); line
/home/iii/tor/m_gym/tensor.cpp: In member function ‘void Critic_Net::LoadMemory(const string&)’:
/home/iii/tor/m_gym/tensor.cpp:88:40: error: cannot bind non-const lvalue reference of type ‘std::istream&’ {aka ‘std::basic_istream<char>&’} to an rvalue of type ‘std::basic_istream<char>’
88 | archive.load_from(std::istringstream(mem));
If I remove the std::istringstream I get the other Unrecognized data format error.
Sorry, I know some C++, but I’m out of my depth with what you’ve introduced.
When I hover over the red underline it shows…
load_from
+4 overloads
Loads the `InputArchive` from a serialized representation stored in the
file at `filename`. Storage are remapped using device option. If device
is not specified, the module is loaded to the original device.
no instance of overloaded function "torch::serialize::InputArchive::load_from" matches the argument listC/C++(304)
Nope. I ended up using this. I found it online somewhere and don’t remember the source.
// Model in 2nd position becomes 1st
void loadstatedict(torch::nn::Module& model, torch::nn::Module& target_model) {
torch::autograd::GradMode::set_enabled(false); // make parameters copying possible
auto new_params = target_model.named_parameters(); // implement this
auto params = model.named_parameters(true /*recurse*/);
auto buffers = model.named_buffers(true /*recurse*/);
for (auto& val : new_params) {
auto name = val.key();
auto* t = params.find(name);
if (t != nullptr) {
t->copy_(val.value());
} else {
t = buffers.find(name);
if (t != nullptr) {
t->copy_(val.value());
}
}
}
torch::autograd::GradMode::set_enabled(true);
}
At least this saves a file called “model.pt” in the directory.
Is there a way to load the model somehow with InputArchive at the top of the main function?
int main() {
auto net = std::make_shared<Critic_Net>();
torch::load(net, "temp.pt");
auto net_optimizer = torch::optim::Adam(net->parameters(), 1e-3);
for (int e = 0; e<1000; e++) {
net_optimizer.zero_grad();
y_hat = net->forward(one);
loss = torch::smooth_l1_loss(target, y_hat);
loss.backward();
net_optimizer.step();
if (e % 50 == 0) {
std::cout << loss.item() << " " << "\n";
}
}
torch::save(net, "temp.pt");
}
I thought it wasn’t working because I expected it to load with a lower loss function. I dimly didn’t think about how the script starts with a new random tensor.
You’re loading from model.pt and saving to temp.pt, is that right ?
Also, Adam optimizer has some internal state which is not saved, so it may behave different after restart.
I just fixed that too. I was trying different model names because I was saving with different methods.
It has been a messy journey but looks like a ‘no duh’ situation in hind sight.
I saved the tensors too and load them and now I can save and load a model which improves every time I run the script, which was the goal. Thank you. It would have taken me another day to figure out. When I saw your single tensor example gave the same outputs from the models I knew where I f’d up.