1

Context

I'm fitting the parameters of a neural net using scipy's fmin_l_bfgs_b port of a limited memory BFGS code. I can monitor the value of the function being optimized on separate validation data by supplying a function to the callback argument of the fmin_l_bfgs_b routine and using global variables. However, in addition to monitoring the progress on the validation data, I would also like to be able terminate the optimization if progress on the validation data has stagnated or is worsening.

Question

Is it possible to provide a callback function to fmin_l_bfgs_b that will break the while loop of the optimization routine? See here for relevant portion of the scipy code.

I think this question is purely a python question by considering the following:

def infloop(callback):
    local_variables
    while True:
        callback()

I'm asking if a callback can be provided that:

  1. Can break the while loop.
  2. Access and modify local_variables in the outer function, infloop.

Is this possible? Or will I have to modify the scipy source? I'm using Python 2.7.12. Scipy is 0.18.0.

Matt Hancock
  • 3,870
  • 4
  • 30
  • 44
  • 1
    Here's an example using callback: http://stackoverflow.com/a/24827245/901925; raising an error is the way to go. The main other use of `callback` to to record intermediate results. – hpaulj May 11 '17 at 21:42
  • Nice. I thought of this, but I was so focused on `fmin_l_bfgs_b` terminating correctly, that I forgot that `callback` will be recording all the important stuff via global variables. So, it doesn't matter if bfgs raises an exception as long as it's caught. – Matt Hancock May 11 '17 at 22:13

0 Answers0