[英]Python scipy.optimize.minimize IndexError with [0,0] but not with .item()
I found that scipy.optimize.minimize works when I use.item() to retrieve a value from an numpy array in the objective function, but it fails when I retrieve by indexing [0,0]:我发现 scipy.optimize.minimize 在我使用.item() 从目标 function 中的 numpy 数组中检索值时起作用
def sigmoid(Z):
return 1 / (1 + np.exp(-Z))
def hyp_log(X, theta):
return sigmoid(X @ theta)
def cost_log(theta, X, Y, reg_const=0):
hyp = hyp_log(X, theta)
return (Y.T @ -np.log(hyp) + (1-Y).T @ -np.log(1-hyp)).item() / len(X) + reg_const * (theta[1:].T @ theta[1:]).item() / (2 * len(X))
result = minimize(cost_log, theta, args=(X,Y,reg_const), method='TNC')
If I use [0,0]
indexing instead of .item()
in the cost_log
function, the function itself works exactly the same as before, but minimize results in IndexError: too many indices for array
.如果我在 cost_log function 中使用[0,0]
索引而不是.item()
,则cost_log
本身的工作方式与以前完全相同,但最大限度地减少IndexError: too many indices for array
结果。 I want to understand why this happens and what I should be careful of in the objective function when using minimize.我想了解为什么会发生这种情况,以及在使用最小化时在目标 function 中应该注意什么。
Since you have not provided X
or Y
, I won't look at:由于您没有提供X
或Y
,我不会看:
(Y.T @ -np.log(hyp) + (1-Y).T @ -np.log(1-hyp))
but with:但有:
(theta[1:].T @ theta[1:]).item()
if theta
is (n,1):如果theta
是 (n,1):
In [15]: theta = np.arange(5)[:,None]
In [16]: theta.shape
Out[16]: (5, 1)
In [17]: (theta[1:].T @ theta[1:])
Out[17]: array([[30]])
In [18]: (theta[1:].T @ theta[1:])[0,0]
Out[18]: 30
In [19]: (theta[1:].T @ theta[1:]).item()
Out[19]: 30
But if you give that theta
to minimize
, it ravels it to a (n,) shape:但是,如果您将theta
赋予minimize
,它会将其分解为 (n,) 形状:
In [20]: theta=theta.ravel()
In [21]: (theta[1:].T @ theta[1:])
Out[21]: 30
In [22]: (theta[1:].T @ theta[1:]).shape
Out[22]: ()
In [23]: (theta[1:].T @ theta[1:]).item()
Out[23]: 30
In [24]: (theta[1:].T @ theta[1:])[0,0]
...
IndexError: invalid index to scalar variable.
I as wrote initially item
can be used with a single item array, regardless of dimensions.我最初写的item
可以与单个 item 数组一起使用,而不管尺寸如何。 [0,0]
only works with a 2d (or higher) array. [0,0]
仅适用于二维(或更高)数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.