Scipy 优化在 1-d 矩阵与向量输入 st 中的行为不同。一维矩阵解是错误的

我已经经历了馈送scipy.optimize(形状的(N,1))1-d矩阵给出不同(错误)结果与在载体的形式给它相同的数据(矢量w和y在下面的MVE


import numpy as np

from scipy.optimize import minimize

X = np.array([[ 1.13042959,  0.45915372,  0.8007231 , -1.15704469,  0.42920652],

       [ 0.14131009,  0.9257914 ,  0.72182141,  0.86906652, -0.32328187],

       [-1.40969139,  1.32624329,  0.49157981,  0.2632826 ,  1.29010016],

       [-0.87733399, -1.55999729, -0.73784827,  0.15161383,  0.11189782],

       [-0.94649544,  0.10406324,  0.65316464, -1.37014083, -0.28934968]])


wtrue = np.array([3.14,2.78,-1,0, 1.6180])


y = X.dot(wtrue)

def cost_function(w, X, y):

    return np.mean(np.abs(y - X.dot(w)))


#  %%

w0 = np.zeros(5)

output = minimize(cost_function, w0, args=(X, y), options={'disp':False, 'maxiter':128})

print('Vector Case:\n', output.x, '\n', output.fun)


# Reshaping w0 and y to (N,1) will 'break things'

w0 = np.zeros(5).reshape(-1,1)

y = y.reshape(-1,1) #This is the problem, only commenting this out will make below work

output = minimize(cost_function, w0, args=(X, y), options={'disp':False, 'maxiter':128})

print('1-d Matrix Case:\n', output.x, '\n', output.fun)


矢量案例:[3.13999999e+00 2.77999996e+00 -9.99999940e-01 1.79002338e-08,1.61800001e+00] 1.72112269325408288


一维矩阵案例:[-0.35218177 -0.50008129 0.34958755 -0.42210756 0.79680766] 3.3810648518841924 // 错得离真正的解决方案很远


有谁知道为什么使用一维矩阵输入的解决方案“错误”?


我怀疑这是一路上的 b/c.minimize将参数向量转换为实际向量然后我知道 (2,) + (2,1) 给出了 (2,2) 矩阵而不是 (2,)或 (2,1)。这仍然让我觉得“奇怪”,我想知道我是否在这里遗漏了一些更重要的点。


侃侃无极
浏览 224回答 1
1回答

月关宝盒

In [300]: y                                                                               Out[300]: array([ 4.7197293 ,  1.7725223 ,  0.85632763, -6.17272225, -3.8040323 ])In [301]: w0                                                                              Out[301]: array([0., 0., 0., 0., 0.])In [302]: cost_function(w0,X,y)                                                           Out[302]: 3.465066756332最初改变的形状y不会改变成本:In [306]: cost_function(w0,X,y.reshape(-1,1))                                             Out[306]: 3.4650667563320003现在得到一个解决方案:在 [308]: output = optimize.minimize(cost_function, w0, args=(X, y), options={'disp':False ...: , 'maxiter':128})In [310]: output.x                                                                        Out[310]: array([ 3.14000001e+00,  2.77999999e+00, -9.99999962e-01, -5.58139763e-08,        1.61799993e+00])将成本评估为最优 xIn [311]: cost_function(output.x,X,y)                                                     Out[311]: 7.068144833866085e-08        # = output.fun但是对于 reshape y,成本是不同的:In [312]: cost_function(output.x,X,y.reshape(-1,1))                                       Out[312]: 4.377833258899681初始值x0已被代码展平(查看optimize.optimize._minimize_bfgs),因此更改 的形状w0无关紧要。但是args数组被传递给成本函数而没有改变。所以如果改变形状y改变成本计算,它会改变优化。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python