Hi!
How to use L-BFGS in the C++ LibTorch?
Particularly, how to implement “closure function” in C++?
Would you please offer an example using L-BFGS?
Thank you!
Hi!
How to use L-BFGS in the C++ LibTorch?
Particularly, how to implement “closure function” in C++?
Would you please offer an example using L-BFGS?
Thank you!
torch::optim::LBFGS optimizer = ... // init
auto closure = [&](){
optimizer.zero_grad();
// must call model forward inside, and then calculate loss
torch::Tensor out = model->forward(...);
torch::Tensor loss = loss_func(out,...);
loss.backward();
return loss;
};
optimizer.step(closure);