details of callback functions to minimize

1,128 views
Skip to first unread message

Scott Norris

unread,
Jun 19, 2015, 2:35:38 PM6/19/15
to lmfi...@googlegroups.com
Hi,

I am continuing to really enjoy using this library, so thanks again for the great code, documentation, and support!

I am interested to learn more about the 'iter_cb' argument to minimize(), but I haven't been able to find much documentation for it.  For instance,
  1. exactly when is the callback function called during the fitting iteration?  
  2. what arguments does minimize() pass to the callback function by default?
  3. can this list of arguments be customized?
Thanks!

Scott Norris

Matt Newville

unread,
Jun 19, 2015, 9:43:50 PM6/19/15
to Scott Norris, lmfi...@googlegroups.com
Hi Scott,


On Fri, Jun 19, 2015 at 1:35 PM, Scott Norris <scott.no...@gmail.com> wrote:
>
> Hi,
>
> I am continuing to really enjoy using this library, so thanks again for the great code, documentation, and support!
>
> I am interested to learn more about the 'iter_cb' argument to minimize(), but I haven't been able to find much documentation for it.  For instance,
>
> 1. exactly when is the callback function called during the fitting iteration?

Lmfit calls the minimization routine (say, scipy.optimize.leastsq()) with its own residual function.  This does a few steps.  First it updates all the parameter values with the ones sent from the minimizer function.  Then it calls the user-supplied objective function.  Then it calls the iteration callback, and finally returns the residual array as calculated by the user-supplied objective.  This is all at https://github.com/lmfit/lmfit-py/blob/master/lmfit/minimizer.py#L229

In short: on each iteration, after the user-supplied objective function has returned.

> 2. what arguments does minimize() pass to the callback function by default?

The arguments sent to the iteration callback are (params, n_iter, resid, *args, **kwargs)

where params is the latest Parameters, n_iter is the iteration number, resid is the residual array from the objective function), and args and kwargs are the same ones sent to the objective function.

A simple example is at https://github.com/lmfit/lmfit-py/blob/master/examples/doc_model_with_iter_callback.py

> 3. can this list of arguments be customized?

Currently, no.  You might be able to use kwargs for whatever you need though.

Hope that helps,

--Matt

Reply all
Reply to author
Forward
0 new messages