Note that there are some explanatory texts on larger screens.

plurals
  1. POMinimisation problem in Python, fmin_bfgs won't work but fmin will, 'Matrices not aligned'
    primarykey
    data
    text
    <p>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:</p> <pre><code> 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_ </code></pre> <p>Here's how I call fmin_bfgs:</p> <pre><code> 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) </code></pre> <p>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:</p> <p>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</p> <p>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!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload