Scipy优化:获取打印其迭代的功能

时间:2017-02-24 16:49:13

标签: python numpy optimization scipy

我正在使用scipy.optimize.minimize()来最小化某个功能。我想比较不同方法BFGSL-BFGS-B的性能,为此,我希望函数在优化时打印出它的值和误差范围。

L-BFGS-B实际上会自动执行此操作,它看起来如下所示:

At X0         0 variables are exactly at the bounds

At iterate    0    f=  7.73701D+04    |proj g|=  1.61422D+03

At iterate    1    f=  4.33415D+04    |proj g|=  1.16289D+03

At iterate    2    f=  9.97661D+03    |proj g|=  5.04925D+02

At iterate    3    f=  4.10666D+03    |proj g|=  3.04707D+02

....

At iterate  194    f=  3.34407D+00    |proj g|=  3.55117D-04

At iterate  195    f=  3.34407D+00    |proj g|=  3.36692D-04

At iterate  196    f=  3.34407D+00    |proj g|=  9.58307D-04

Tit   = total number of iterations
Tnf   = total number of function evaluations
Tnint = total number of segments explored during Cauchy searches
Skip  = number of BFGS updates skipped
Nact  = number of active bounds at final generalized Cauchy point
Projg = norm of the final projected gradient
F     = final function value

       * * *

N    Tit     Tnf  Tnint  Skip  Nact     Projg        F
243    196    205      1     0     0   9.583D-04   3.344D+00
F =   3.34407234824719

有谁知道我如何为BFGS做同样的事情?

注意:此问题与此处发布的较大问题有关:SciPy optimisation: Newton-CG vs BFGS vs L-BFGS,关于特定优化问题中这两种算法之间行为的差异。我想跟踪这两种算法的分歧。

1 个答案:

答案 0 :(得分:1)

我在这里找到了答案:How to display progress of scipy.optimize function?

callback的{​​{1}}选项允许我们提供一种方法,该方法可以访问optimize.minimize()在时间步x_n计算的变量optimize.minimize()。我们可以用它来打印出数据;我选择写出外部文件如下:

n

完美无缺!