矩阵未对齐错误:Python SciPy fmin_bfgs [英] matrices are not aligned Error: Python SciPy fmin_bfgs

查看:71
本文介绍了矩阵未对齐错误:Python SciPy fmin_bfgs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题简介: 尝试使用scipy.optimize.fmin_bfgs最小化(优化)函数时,该函数将引发

Problem Synopsis: When attempting to use the scipy.optimize.fmin_bfgs minimization (optimization) function, the function throws a

derphi0 = np.dot(gfk,pk) ValueError:矩阵未对齐

derphi0 = np.dot(gfk, pk) ValueError: matrices are not aligned

错误.根据我的错误检查,这种情况发生在通过fmin_bfgs进行的第一次迭代的最后阶段,也就是在返回任何值或调用任何回调之前.

error. According to my error checking this occurs at the very end of the first iteration through fmin_bfgs--just before any values are returned or any calls to callback.

配置: Windows Vista 的Python 3.2.2 科学0.10 IDE =使用PyDev的Eclipse

Configuration: Windows Vista Python 3.2.2 SciPy 0.10 IDE = Eclipse with PyDev

详细说明: 我正在使用scipy.optimize.fmin_bfgs来最小化简单逻辑回归实现(从Octave转换为Python/SciPy)的成本.基本上,cost函数被称为cost_arr函数,而梯度下降是在gradient_descent_arr函数中.

Detailed Description: I am using the scipy.optimize.fmin_bfgs to minimize the cost of a simple logistic regression implementation (converting from Octave to Python/SciPy). Basically, the cost function is named cost_arr function and the gradient descent is in gradient_descent_arr function.

我已经手动测试并充分验证* cost_arr *和* gradient_descent_arr *可以正常工作并正确返回所有值.我还测试了是否将正确的参数传递给了* fmin_bfgs *函数.但是,在运行时,出现ValueError:矩阵未对齐.根据源审查,确切的错误发生在

I have manually tested and fully verified that *cost_arr* and *gradient_descent_arr* work properly and return all values properly. I also tested to verify that the proper parameters are passed to the *fmin_bfgs* function. Nevertheless, when run, I get the ValueError: matrices are not aligned. According to the source review, the exact error occurs in the

def line_search_wolfe1 在scipy软件包提供的#Minpack的Wolfe行和标量搜索中起作用.

def line_search_wolfe1 function in # Minpack's Wolfe line and scalar searches as supplied by the scipy packages.

值得注意的是,如果我改用 scipy.optimize.fmin ,则 fmin 函数将运行到完成.

Notably, if I use scipy.optimize.fmin instead, the fmin function runs to completion.

完全错误:

文件 "D:\ Users \ Shannon \ Programming \ Eclipse \ workspace \ SBML \ sbml \ LogisticRegression.py", 395行,位于fminunc_opt

File "D:\Users\Shannon\Programming\Eclipse\workspace\SBML\sbml\LogisticRegression.py", line 395, in fminunc_opt

optcost = scipy.optimize.fmin_bfgs(self.cost_arr, initialtheta, fprime=self.gradient_descent_arr, args=myargs, maxiter=maxnumit, callback=self.callback_fmin_bfgs, retall=True)   

文件 "C:\ Python32x32 \ lib \ site-packages \ scipy \ optimize \ optimize.py",行 533,在fmin_bfgs old_fval,old_old_fval中)
文件"C:\ Python32x32 \ lib \ site-packages \ scipy \ optimize \ linesearch.py​​",行 76,在line_search_wolfe1中 derphi0 = np.dot(gfk,pk) ValueError:矩阵未对齐

File "C:\Python32x32\lib\site-packages\scipy\optimize\optimize.py", line 533, in fmin_bfgs old_fval,old_old_fval)
File "C:\Python32x32\lib\site-packages\scipy\optimize\linesearch.py", line 76, in line_search_wolfe1 derphi0 = np.dot(gfk, pk) ValueError: matrices are not aligned

我用以下命令调用优化函数: optcost = scipy.optimize.fmin_bfgs(self.cost_arr,initialtheta,fprime = self.gradient_descent_arr,args = myargs,maxiter = maxnumit,callback = self.callback_fmin_bfgs,retall = True)

I call the optimization function with: optcost = scipy.optimize.fmin_bfgs(self.cost_arr, initialtheta, fprime=self.gradient_descent_arr, args=myargs, maxiter=maxnumit, callback=self.callback_fmin_bfgs, retall=True)

我花了几天时间尝试解决此问题,但似乎无法确定是什么原因导致了矩阵未对齐错误.

I have spent a few days trying to fix this and cannot seem to determine what is causing the matrices are not aligned error.

附录:2012-01-08 我做了很多工作,似乎缩小了问题的范围(但是对如何解决这些问题感到困惑).首先,fmin(仅使用fmin)使用这些函数(成本,梯度)工作.其次,成本和梯度函数都可以在手动实现中的一次迭代中测试时准确返回期望值(不使用fmin_bfgs).第三,我在error.optimize.linsearch中添加了错误代码,该错误似乎是在以下行中的def line_search_wolfe1处引发的:derphi0 = np.dot(gfk,pk). 在这里,根据我的测试,scipy.optimize.optimize pk = [[12.00921659] [11.26284221]] pk type =和scipy.optimize.optimizegfk = [[-12.00921659] [-11.26284221]] gfk type = 注意:根据我的测试,该错误是在fmin_bfgs的第一个迭代中引发的(即fmin_bfgs甚至不会完成单个迭代或更新).

ADDENDUM: 2012-01-08 I worked with this a lot more and seem to have narrowed the issues (but am baffled on how to fix them). First, fmin (using just fmin) works using these functions--cost, gradient. Second, the cost and the gradient functions both accurately return expected values when tested in a single iteration in a manual implementation (NOT using fmin_bfgs). Third, I added error code to optimize.linsearch and the error seems to be thrown at def line_search_wolfe1 in line: derphi0 = np.dot(gfk, pk). Here, according to my tests, scipy.optimize.optimize pk = [[ 12.00921659] [ 11.26284221]]pk type = and scipy.optimize.optimizegfk = [[-12.00921659] [-11.26284221]]gfk type = Note: according to my tests, the error is thrown on the very first iteration through fmin_bfgs (i.e., fmin_bfgs never even completes a single iteration or update).

我感谢任何指导或见解.

I appreciate ANY guidance or insights.

下面的我的代码(已删除日志,删除了文档): 假设theta = 2x1 ndarray(实际:theta Info Size =(2,1)Type =) 假设X = 100x2 ndarray(实际:X信息大小=(2,100)类型=) 假设y = 100x1 ndarray(实际:y信息大小=(100,1)类型=)

My Code Below (logging, documentation removed): Assume theta = 2x1 ndarray (Actual: theta Info Size=(2, 1) Type = ) Assume X = 100x2 ndarray (Actual: X Info Size=(2, 100) Type = ) Assume y = 100x1 ndarray (Actual: y Info Size=(100, 1) Type = )

def cost_arr(self, theta, X, y):

    theta = scipy.resize(theta,(2,1))         

    m = scipy.shape(X)

    m = 1 / m[1] # Use m[1] because this is the length of X
    logging.info(__name__ + "cost_arr reports m = " + str(m))         

    z = scipy.dot(theta.T, X) # Must transpose the vector theta               

    hypthetax = self.sigmoid(z)

    yones = scipy.ones(scipy.shape(y))

    hypthetaxones = scipy.ones(scipy.shape(hypthetax))

    costright = scipy.dot((yones - y).T, ((scipy.log(hypthetaxones - hypthetax)).T))

    costleft = scipy.dot((-1 * y).T, ((scipy.log(hypthetax)).T))


def gradient_descent_arr(self, theta, X, y):

    theta = scipy.resize(theta,(2,1)) 

    m = scipy.shape(X)

    m = 1 / m[1] # Use m[1] because this is the length of X

    x = scipy.dot(theta.T, X) # Must transpose the vector theta

    sig = self.sigmoid(x)

    sig = sig.T - y

    grad = scipy.dot(X,sig)

    grad = m * grad

    return grad

def fminunc_opt_bfgs(self, initialtheta, X, y, maxnumit):
    myargs= (X,y)

    optcost = scipy.optimize.fmin_bfgs(self.cost_arr, initialtheta, fprime=self.gradient_descent_arr, args=myargs, maxiter=maxnumit, retall=True, full_output=True)

    return optcost

推荐答案

万一其他人遇到此问题....

In case anyone else encounters this problem ....

1)错误1:如注释中所述,我错误地从渐变中将值作为多维数组(m,n)或(m,1)返回. fmin_bfgs似乎需要从渐变输出1d数组(也就是说,您必须返回(m,)数组,而不是(m,1)数组.如果不确定,请使用scipy.shape(myarray)检查尺寸.返回值.

1) ERROR 1: As noted in the comments, I incorrectly returned the value from my gradient as a multidimensional array (m,n) or (m,1). fmin_bfgs seems to require a 1d array output from the gradient (that is, you must return a (m,) array and NOT a (m,1) array. Use scipy.shape(myarray) to check the dimensions if you are unsure of the return value.

此修复程序涉及添加:

grad = numpy.ndarray.flatten(grad)

从梯度函数返回梯度之前.这会将数组从(m,1)拉平"到(m,). fmin_bfgs可以将其作为输入.

just before returning the gradient from your gradient function. This "flattens" the array from (m,1) to (m,). fmin_bfgs can take this as input.

2)错误2:请记住,fmin_bfgs似乎可以与非线性函数一起使用.就我而言,最初使用的示例是LINEAR函数.这似乎可以解释某些异常结果,即使在上述扁平化修复之后也是如此.对于LINEAR函数,fmin而不是fmin_bfgs可能会更好.

2) ERROR 2: Remember, the fmin_bfgs seems to work with NONlinear functions. In my case, the sample that I was initially working with was a LINEAR function. This appears to explain some of the anomalous results even after the flatten fix mentioned above. For LINEAR functions, fmin, rather than fmin_bfgs, may work better.

QED

这篇关于矩阵未对齐错误:Python SciPy fmin_bfgs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆