Python 中的最小化问题,fmin_bfgs 不起作用,但 fmin 可以,“矩阵未对齐”;

发布于 2024-12-06 10:30:54 字数 2222 浏览 0 评论 0原文

我在 python 中有一个函数,它接受一个向量并返回一个实数。我使用 scipy.optimize fmin 和 fmin_bfgs 函数来查找为函数提供近似最小值的参数。然而,当我使用 fmin 时,我得到了一个好的答案(相当慢),但是当我切换到 fmin_bfgs 时,我收到一条错误消息“矩阵未对齐”。这是我的函数:

    def norm(b_):
        b_ = b_.reshape(int(M),1)                   #M already given elsewhere
        Yb = np.dot(Y,b_)                           #Y already given elsewhere

        B = np.zeros((int(M),int(M))) 
        for j in xrange(int(M)):
            B[j][j] = -t[j+1]*np.exp(-t[j+1]*Yb[j]) #The t[j] are already known
        P = np.zeros((int(M),1))
        for j in xrange(int(M)):
            P[j][0] = np.exp(-t[j+1]*Yb[j])

        diff = np.zeros((int(M),1))                 #Functions d(i,b) are known
        for i in xrange(1,int(M)-1):
            diff[i][0] = d(i+1,b_) - d(i,b_)
        diff[0][0] = d(1,b_)
        diff[int(M)-1][0] = -d(int(M)-1,b_)

        term1_ = (1.0/N)*(np.dot((V - np.dot(c,P)).transpose(),W))
        term2_ = np.dot(W,V - np.dot(c,P))          #V,c,P,W already known
        term1_ = np.dot(term1_,term2_)
        term2_ = lambd*np.dot(Yb.transpose(),diff)

        return term1_ + term2_

这是我调用 fmin_bfgs 的方式:

    fmin_bfgs(norm, b_guess,fprime=None,
              args=(),gtol=0.0001,norm=0.00000000001,
              epsilon=1.4901161193847656e-08,maxiter=None,
              full_output=0, disp=1, retall=0, callback=None)

当我调用 fmin 时,它工作正常,只是太慢而没有用(我需要优化几次)。但是当我尝试 fmin_bfgs 时,我收到此错误:

回溯(最近一次调用最后): 文件“C:\Program Files\Wing IDE 101 4.0\src\debug\tserver_sandbox.py”,第 287 行,模块中 文件“C:\ Python27 \ Lib \ site-packages \ scipy \ optimize \ optimize.py”,第491行,在fmin_bfgs old_fval,old_old_fval中) 文件“C:\Python27\Lib\site-packages\scipy\optimize\linesearch.py​​”,第 239 行,line_search_wolfe2 derphi0、c1、c2、amax) 文件“C:\Python27\Lib\site-packages\scipy\optimize\linesearch.py​​”,第 339 行,在 scalar_search_wolfe2 phi0、derphi0、c1、c2 中) 文件“C:\Python27\Lib\site-packages\scipy\optimize\linesearch.py​​”,第 471 行,位于 _zoom derphi_aj = derphi(a_j) 文件“C:\Python27\Lib\site-packages\scipy\optimize\linesearch.py​​”,第 233 行,在 derphi 中返回 np.dot(gval[0], pk) ValueError:矩阵未对齐

您知道为什么会发生这种情况吗?我为该函数提供的所有矩阵均已正确对齐(并且该函数自 fmin 工作以来一直有效)。非常感谢帮助!

I have a function in python which takes a vector and returns a real number. I am using the scipy.optimize fmin and fmin_bfgs functions to find the argument which gives the function its approx minimum value. However, when I use fmin I get an alright answer (quite slowly) but when I switch to fmin_bfgs, I get an error saying "Matrices are not aligned". Here's my function:

    def norm(b_):
        b_ = b_.reshape(int(M),1)                   #M already given elsewhere
        Yb = np.dot(Y,b_)                           #Y already given elsewhere

        B = np.zeros((int(M),int(M))) 
        for j in xrange(int(M)):
            B[j][j] = -t[j+1]*np.exp(-t[j+1]*Yb[j]) #The t[j] are already known
        P = np.zeros((int(M),1))
        for j in xrange(int(M)):
            P[j][0] = np.exp(-t[j+1]*Yb[j])

        diff = np.zeros((int(M),1))                 #Functions d(i,b) are known
        for i in xrange(1,int(M)-1):
            diff[i][0] = d(i+1,b_) - d(i,b_)
        diff[0][0] = d(1,b_)
        diff[int(M)-1][0] = -d(int(M)-1,b_)

        term1_ = (1.0/N)*(np.dot((V - np.dot(c,P)).transpose(),W))
        term2_ = np.dot(W,V - np.dot(c,P))          #V,c,P,W already known
        term1_ = np.dot(term1_,term2_)
        term2_ = lambd*np.dot(Yb.transpose(),diff)

        return term1_ + term2_

Here's how I call fmin_bfgs:

    fmin_bfgs(norm, b_guess,fprime=None,
              args=(),gtol=0.0001,norm=0.00000000001,
              epsilon=1.4901161193847656e-08,maxiter=None,
              full_output=0, disp=1, retall=0, callback=None)

When I call fmin it works fine, just too slowly to be useful (I need to optimise several times). But when I try fmin_bfgs I get this error:

Traceback (most recent call last):
File "C:\Program Files\Wing IDE 101 4.0\src\debug\tserver_sandbox.py", line 287, in module
File "C:\Python27\Lib\site-packages\scipy\optimize\optimize.py", line 491, in fmin_bfgs old_fval,old_old_fval)
File "C:\Python27\Lib\site-packages\scipy\optimize\linesearch.py", line 239, in line_search_wolfe2 derphi0, c1, c2, amax)
File "C:\Python27\Lib\site-packages\scipy\optimize\linesearch.py", line 339, in scalar_search_wolfe2 phi0, derphi0, c1, c2)
File "C:\Python27\Lib\site-packages\scipy\optimize\linesearch.py", line 471, in _zoom derphi_aj = derphi(a_j)
File "C:\Python27\Lib\site-packages\scipy\optimize\linesearch.py", line 233, in derphi return np.dot(gval[0], pk)
ValueError: matrices are not aligned

Any ideas why this might happen? All the matrices I have supplied the function are aligned correctly (and the function works since fmin worked). Help much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟火散人牵绊 2024-12-13 10:30:54

似乎其中一个程序最终处理的数字太大而无法处理。遗憾的是它不能告诉我它做得正确。不过我解决了这个问题,所以没有更多问题。抱歉,如果这浪费了您的时间。

It seems that one of the programs just ended up dealing with numbers that were too large for it to handle. Shame it couldn't tell me it was doing that properly. I worked around it though, so no more problem. Sorry if this wasted your time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文