【问题标题】:Python: Scipy.optimize Levenberg-marquardt methodPython:Scipy.optimize Levenberg-marquardt 方法
【发布时间】:2016-09-06 09:15:14
【问题描述】:

我有一个关于如何在 Python 中使用 Levenberg-Marquardt 优化方法的问题。在 SCIPY 图书馆里有很多optimization methods

我尝试了两种方法(Nelder-Mead 和 Basin-hopping),都可以很好地使用以下命令:

# Nelder-Mead
res0_10 = optimize.minimize(f0_10, x0, method='Nelder-Mead', options={'disp': True, 'maxiter': 2000})

# Basin-hopping
res0_10 = optimize.basinhopping(f0_10, x0, niter=100, disp=True)

使用Levenberg-Marquardt时出现问题(我只复制了错误的部分,因为程序很长)

def f0_10(x):
    m, u, z, s = x 
    for i in range(alt_max):
         if i==alt_min: suma=0
         if i > alt_min:
         suma = suma + (B(x, i)-b0_10(x, i))**2
    return np.sqrt(suma/alt_max)

x0 = np.array([40., 0., 500., 50.])

res0_10 = root(f0_10, x0, jac=True, method='lm')

我只改了最后一句(res0_10 = root...)。程序编译得很好,但是当我执行程序时:

Exception in Tkinter callback

Traceback (most recent call last):

File "C:\Users\Quini SB\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.7.4.3348.win-x86_64\lib\lib-tk\Tkinter.py", line 1536, in __call__
return self.func(*args)

File "C:\Users\Quini SB\Desktop\tfg\Steyn - levmar.py", line 384, in askopenfilename
res0_10 = root(f0_10, x0, jac=True, method='lm')

File "C:\Users\Quini SB\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\optimize\_root.py", line 188, in root
sol = _root_leastsq(fun, x0, args=args, jac=jac, **options)

File "C:\Users\Quini SB\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\optimize\_root.py", line 251, in _root_leastsq
factor=factor, diag=diag)

File "C:\Users\Quini SB\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\optimize\minpack.py", line 377, in leastsq
shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)

File "C:\Users\Quini SB\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\optimize\minpack.py", line 26, in _check_func
res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))

File "C:\Users\Quini SB\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\optimize\optimize.py", line 64, in __call__
self.jac = fg[1]

IndexError: invalid index to scalar variable.

为什么会出现这个错误?

【问题讨论】:

    标签: python optimization scipy levenberg-marquardt


    【解决方案1】:

    来自文档:

    jac : bool or callable, optional
    
        If jac is a Boolean and is True, fun is assumed to return the value
        of Jacobian along with the objective function. If False, the 
        Jacobian will be estimated numerically. jac can also be a callable 
        returning the Jacobian of fun. In this case, it must accept the
        same arguments as fun.
    

    因此,您的函数“f0_10”需要返回两个值,因为您将 jac 设置为 True

    【讨论】:

    • 回溯(最近一次调用最后):文件“C:\Users\Quini SB\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.7.4.3348.win-x86_64\lib\ lib-tk\Tkinter.py",第 1536 行,在 call 中返回 self.func(*args) 文件“C:\Users\Quini SB\Desktop\tfg\Steyn - levmar.py”,第 384 行,在 askopenfilename res0_10 = root(f0_10, x0, jac=False, method='lm')
    • 文件“C:\Users\Quini SB\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\optimize_root.py”,第 188 行,在根 sol = _root_leastsq( fun, x0, args=args, jac=jac, **options) 文件“C:\Users\Quini SB\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\optimize_root.py”,行251,在 _root_leastsq 因子=因子,diag=diag) 文件“C:\Users\Quini SB\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\optimize\minpack.py”中,第 380 行, in leastsq raise TypeError('Improper input: N=%s must not超过 M=%s' % (n, m)) TypeError: Improper input: N=4 must not超过 M=1
    • 您的 x0 的大小为 4,但您的输入 f0_10 的维度为 1。在做更高级的事情之前,您需要阅读、测试和理解文档中的示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    相关资源
    最近更新 更多