我需要找到成本函数的最小值,该函数有数千个变量。成本函数只是一个最小二乘计算,并且可以通过numpy向量化轻松快速地计算。尽管如此,优化仍然需要漫长的时间。我猜测慢速运行发生在SciPy的最小化器中而不是我的成本函数中。如何更改SciPy的最小化器参数以加速运行时间?
示例代码:
import numpy as np
from scipy.optimize import minimize
# random data
x = np.random.randn(100, 75)
# initial weights guess
startingWeights = np.ones(shape=(100, 75))
# random y vector
y = np.random.randn(100)
def costFunction(weights):
# reshapes flattened weights into 2d matrix
weights = np.reshape(weights, newshape=(100, 75))
# weighted row-wise sum
weighted = np.sum(x * weights, axis=1)
# squared residuals
residualsSquared = (y - weighted) ** 2
return np.sum(residualsSquared)
result = minimize(costFunction, startingWeights.flatten())