简体   繁体   English

scipy.optimize.minimize 跟踪目标函数

[英]scipy.optimize.minimize keep track of objective function

I'm working with scipy.optimize.minimize , and I'm optimizing 3 parameters with a function like this我正在使用scipy.optimize.minimize ,我正在使用这样的函数优化 3 个参数

def foo(A, x, y, z):
    test = my_function(A[0], A[1], A[2], x, y, z)
    return test

In this answer I found some insight: How to display progress of scipy.optimize function?在这个答案中,我发现了一些见解: How to display progress of scipy.optimize function? So I came up with this function:所以我想出了这个功能:

def callbackF(Xi, x, y, z)
    global Nfeval
    print '{0:4d}   {1: 3.6f}   {2: 3.6f}   {3: 3.6f}   {4: 3.6f}'.format(Nfeval, Xi[0], Xi[1], Xi[2], foo(Xi, x, y, z))
    Nfeval += 1

So my code will look like this所以我的代码看起来像这样

Optimal = minimize(fun=foo, x0=[fi, alfa, Ks], args=(x, y, z), 
                   method='BFGS', callback=callbackF, tol=1e-2)

but I get this error :但我收到此错误:

TypeError: callbackF() takes exactly 4 arguments (1 given)

I understand the error, but how should I avoid it?我理解错误,但我应该如何避免它?

You can always DIY it if you can instrument the function itself.如果您可以对功能本身进行检测,则可以随时进行 DIY。 The only tricky bit is the iteration count.唯一棘手的一点是迭代计数。 For it you can either use a global, or (IMO, better) attach the counter to the function itself:为此,您可以使用全局或(IMO,更好)将计数器附加到函数本身:

>>> import numpy as np
>>> from scipy.optimize import minimize
>>> 
>>> def f(x):
...     res = np.sum(x**2)
...     f.count += 1
...     print('x = ', x, ' res = ', res, '  j = ', f.count)
...     return res
... 
>>> f.count = 0
>>> minimize(f, x0=5)
x =  [ 5.]  res =  25.0   j =  1
x =  [ 5.00000001]  res =  25.000000149   j =  2
x =  [ 5.]  res =  25.0   j =  3
x =  [-5.]  res =  25.0   j =  4
x =  [-5.]  res =  25.0   j =  5
x =  [-4.99999999]  res =  24.999999851   j =  6
x =  [ 0.0005]  res =  2.5e-07   j =  7
x =  [ 0.0005]  res =  2.5e-07   j =  8
x =  [ 0.00050001]  res =  2.50014901383e-07   j =  9
x =  [ -7.45132485e-09]  res =  5.55222420558e-17   j =  10
x =  [ -7.45132485e-09]  res =  5.55222420558e-17   j =  11
x =  [  7.44983634e-09]  res =  5.55000615146e-17   j =  12
      fun: 5.552224205575604e-17
 hess_inv: array([[ 0.5]])
      jac: array([ -1.48851092e-12])
  message: 'Optimization terminated successfully.'
     nfev: 12
      nit: 2
     njev: 4
   status: 0
  success: True
        x: array([ -7.45132485e-09])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Scipy.optimize.minimize目标函数ValueError - Scipy.optimize.minimize objective function ValueError 将约束应用于scipy.optimize.minimize?中的目标函数? 如0 &lt;=目标&lt;= 1 - Apply constraints to the objective function in scipy.optimize.minimize? Such as 0 <= objective <= 1 Scipy.optimize.minimize 目标 function 必须返回一个标量 - Scipy.optimize.minimize Objective function must return a scalar 函数scipy.optimize.minimize的选项 - options of the function scipy.optimize.minimize 当我使用 scipy.optimize.minimize() 最小化它时,为什么我的目标 function 的矩阵参数发生了变化? - Why is a matrix argument of my objective function changed when I minimize it with scipy.optimize.minimize()? 当你想要计算梯度和目标函数时,如何使用scipy.optimize.minimize函数? - How to use scipy.optimize.minimize function when you want to compute gradient along with the objective function? 除了自变量外,如何为scipy.optimize.minimize的目标函数提供附加输入 - How to give additional input to objective function of scipy.optimize.minimize other than independent variables 我可以将目标函数和派生函数传递给scipy.optimize.minimize作为一个函数吗? - Can I pass the objective and derivative functions to scipy.optimize.minimize as one function? 为什么scipy.optimize.minimize尝试将奇怪的参数传递给我的目标函数? - Why is scipy.optimize.minimize trying to pass in weird arguments to my objective function? 如何使用 scipy.optimize.minimize() 指定目标 function 最小化的参数? - How to specify the parameter an objective function is minimized with respect to, using scipy.optimize.minimize()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM