-
Notifications
You must be signed in to change notification settings - Fork 767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python optimizer iteration hook #1127
Conversation
How does it relate to the exiting logging_optimizer.py? |
@dellaert Oh I didn't realize Although I think
One downside: the c++ UsageThis proposed PR: optimization_history = []
def iteration_hook(iter, error_before, error_after):
optimization_history.append(gtsam.Values(optimizer.values()))
params = gtsam.LevenbergMarquardtParams()
params.iterationHook = iteration_hook
optimizer = gtsam.LevenbergMarquardtOptimizer(graph, initial_values, params)
sol = optimizer.optimize() logging_optimizer.py: optimization_history = []
def iteration_hook(optimizer, error):
optimization_history.append(gtsam.Values(optimizer.values()))
params = gtsam.LevenbergMarquardtParams()
optimizer = gtsam.LevenbergMarquardtOptimizer(graph, initial_values, params)
sol = gtsam.utils.logging_optimizer.gtsam_optimize(optimizer, params, iteration_hook) |
Ok, that’s cool! Maybe it makes sense then to in this PR also modify logging_optimizer.py to use the c++ version? |
@gchenfc ping |
Quick timing benchmark (
Seems they're all comparable and within a margin of error. |
FYI,
Allowing non-const access to the params of an optimizer object would require adding a non-const "getter" virtual function and implementing for every optimizer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool!
@dellaert Actually I made a mistake (there's a cmake regeneration dependency missing, #1176 ) and neither of the existing logging functions can easily support using this c++ iteration hook version. I added a 3rd new function inside solution = optimize_using(gtsam.LevenbergMarquardtOptimizer, hook)(self.graph, self.initial) Let me know how you feel about this - if it's undesired then I'll just revert the last commit then merge. |
I think we talked about the solution? |
ping |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, thanks ! I will merge
In c++ there's an iteration hook that can be called on each iteration of the optimizer for logging/debugging purposes. Previously it wasn't wrapped in python.
It's a super easy addition to wrap in python thanks to the wonderful lambda's machinery of pybind11 / gtwrap.
While I was adding a unit test for the iteration hook, I also minorly refactored the python nonlinear optimizer unit test to put each test in its own function so that, in the event any fail, unittest will print out more obviously which one was the one that failed.