1
$\begingroup$

I was looking at the scipy.minimize documentation to see if I could find a way to terminate optimization when the loss gets below some cut-off, and I couldn't see an obvious way to do it.

The callback option only lets me pass it the xk, but I need to pass additional parameters to evaluate my loss.

Any thoughts on a straightforward way to do this?

$\endgroup$
  • $\begingroup$ I think they have tolerances for the vector and the function. $\endgroup$ – nicoguaro May 18 at 3:09
  • $\begingroup$ I tried them, I think those tolerances are for the difference in loss between steps for some number of steps, rather than the actual value of the loss. $\endgroup$ – user49404 May 18 at 3:10
  • $\begingroup$ Yes, you're right. The callback options seems to see the way to go, maybe some of the solvers let you to pass a function value. $\endgroup$ – nicoguaro May 18 at 3:20
  • $\begingroup$ I went ahead and just modified the scipy optimize.py source to do what I wanted. <_<. The callback doesn't work for me here because I need to pass additional parameters that scipy isn't allowing. $\endgroup$ – user49404 May 18 at 3:28
2
$\begingroup$

So I went ahead and just modified the scipy optimize.py source to do what I wanted. I assume this is not best practices, but... moving on. So I used the inspect module to figure out where the corresponding functions are. So for the Powell optimization routine, I found:

def _minimize_powell

on line 2509 of scipy/optimize/optimize.py. Then I went to line 2593, where I noticed a couple of termination conditions, and I added my own termination condition:

        if fval < 0.1:
        break

which now terminates optimization and returns the optimization struct as desired when this condition is met.

| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.