SciML / Optimization.jl

Mathematical Optimization in Julia. Local, global, gradient-based and derivative-free. Linear, Quadratic, Convex, Mixed-Integer, and Nonlinear Optimization in one simple, fast, and differentiable interface.
https://docs.sciml.ai/Optimization/stable/
MIT License
704 stars 77 forks source link

Accessing optimization iteration in callback function #617

Closed jClugstor closed 5 months ago

jClugstor commented 10 months ago

Is there a way that I could get the current iteration of the optimization inside of a callback. I would like to be able to access the number of times the loss function has been called from inside of the callback function.

Vaibhavdixit02 commented 5 months ago

It should be available from the iter field of the OptimizationState https://docs.sciml.ai/Optimization/stable/API/optimization_state/#optstate which is the first argument to the callback

SebastianM-C commented 5 months ago

For the number of calls to the loss function, the .original filed would be useful? The number of iterations would be a lower bound on that number.

Vaibhavdixit02 commented 5 months ago

Yeah, that's used to populate the fevals field in https://docs.sciml.ai/Optimization/stable/API/optimization_stats/ for some solvers if they return it. We might switch to an Optimization.jl native counter if it makes sense later.

jClugstor commented 5 months ago

Did the first argument to the callback recently change to be an OptimizationState?

jClugstor commented 5 months ago

I see, https://github.com/SciML/Optimization.jl/pull/649 did it, I think that solves it then, thanks!