patrick-kidger / optimistix

Nonlinear optimisation (root-finding, least squares, ...) in JAX+Equinox. https://docs.kidger.site/optimistix/
Apache License 2.0
310 stars 15 forks source link

Question: errorhandling, BFGS minimization, vmap, and best practices #66

Closed HansN87 closed 3 months ago

HansN87 commented 3 months ago

Hi,

thank you for this incredibly useful library. Being able to efficiently vmap over an entire BFGS minimization procedure is invaluable to me.

Context: I am solving a large number (O(10^5)) of maximum likelihood problems by vmapping the individual BFGS optimization over different realizations of the input data (delivered in batches of O(10^3)).

Issue: There appear to be rare cases in which a solution can not be found within a reasonable number of steps, causing the entire batch to fail. I get thrown the following error:

EqxRuntimeError: The maximum number of steps was reached in the nonlinear solver. The problem may not be solveable (e.g., a root-find on a function that has no roots), or you may need to increase max_steps

Rather than seeing a RuntimeError (and loosing the entire batch that contains one problematic example) and continuing with the next batch, I'd prefer getting a batch of results (as usual) with the unsolved case being filled with whatever dummy value/s.

Question: Is such behavior possible - or is there a recommended alternative regarding how to deal with runtime errors and vmap?

Thank you!

patrick-kidger commented 3 months ago

Yup! optx.minimise(..., throw=False).

HansN87 commented 3 months ago

Thank you. Just tested. This is exactly what I needed. Shame on me for not seeing it in the function signature / FAQ. ;-)