检索 scipy 最小化函数最低错误

Retrieve scipy minimize function lowest error

有没有办法在 scipy.minimize 收敛后直接检索最小化误差,或者必须将其直接编码到成本函数中?

我好像只能检索收敛到的系数。

def errorFunction(params,series,loss_function,slen = 12):
    alpha, beta, gamma = params
    breakUps = int(len(series) / slen)
    end = breakUps * slen
    test = series[end:]
    errors = []

    for i in range(2,breakUps+1):
        model = HoltWinters(series=series[:i * 12], slen=slen,
                            alpha=alpha, beta=beta, gamma=gamma, n_preds=len(test))
        model.triple_exponential_smoothing()
        predictions = model.result[-len(test):]
        actual = test
        error = loss_function(predictions, actual)
        errors.append(error)
    return np.mean(np.array(errors))

opt = scipy.optimize.minimize(errorFunction, x0=x,
                   args=(train, mean_squared_log_error),
                   method="L-BFGS-B", bounds = ((0, 1), (0, 1), (0, 1))
                  )
#gets the converged values
optimal values = opt.x
#I would like to know what the error with errorFunction is when using opt.x values, without having to manually run the script again
#Is the minimum error stored somewhere in the returned object opt

据我从函数 scipy.optimize.minimize 的文档中了解到,结果作为 OptimizeResult 对象返回。

根据此 class (here) 的文档,它有一个属性 fun,即 "values of objective function"。

因此,如果您这样做 opt.fun,您应该会获得您要查找的结果。 (您可以检索更多值,例如 Jacobian opt.jac、Hessian opt.hess,...如文档中所述)