开发者

Minimisation problem in Python, fmin_bfgs won't work but fmin will, 'Matrices not aligned'

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) ValueErro开发者_如何学JAVAr: 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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜