【问题标题】:How does gtol parameter work in scipy.optimize.curve_fitgtol 参数在 scipy.optimize.curve_fit 中如何工作
【发布时间】:2022-01-24 09:18:36
【问题描述】:

我正在尝试使用 scipy 的 curve_fit 将高斯模型拟合到高斯分布数据 (x,y) 上。我正在尝试调整配件的参数,以获得更好的配件。我看到 curve_fit 使用方法 LM(Levenberg-Marquardt 方法)调用 scipy.optimize.least_sq 。在我看来,它构造了一个函数来评估每个数据点的最小二乘标准。在我的示例中,我有 8 个数据点。在我的理解中,根据 scipy 的文档,gtol 是“函数向量和雅可比列之间所需的正交性。”

popt, pcov = optimize.curve_fit(parametrized_gaussian, patch_indexes * pixel_size, sub_sig,
                                    p0=p0, jac=gaussian_derivative_wrt_param, maxfev=max_fev, gtol=1e-11, ftol=1e-11, xtol=1e-11)

parametrized_gaussian 很简单:

def parametrized_gaussian(x, a, x0, sigma) :

    res = a * np.exp(-(x - x0) ** 2 / (2 * sigma ** 2))

    return res.astype('float64')

而 gaussian_derivative_wrt_param 是

def gaussian_derivative_wrt_param(x, a, x0, sigma):
   
 return np.array([parametrized_gaussian(x, a, x0, sigma) / a,
                  2 * (x - x0) / (sigma ** 2) * parametrized_gaussian(x, a, x0, sigma),
                  (x - x0) ** 2 / (sigma ** 3) * parametrized_gaussian(x, a, x0, sigma)]). swapaxes(0, -1).astype('float64')

我想检查得到的最优参数处的雅可比值。我不明白我得到的价值观。当curve_fit调用leastsq时,它会使用:

        retval = _minpack._lmder(func, Dfun, x0, args, full_output,
                             col_deriv, ftol, xtol, gtol, maxfev,
                             factor, diag)

我打印 Dfun(retval[0]),因为 retval[0] 是最优参数的值。这就是我得到的。 0.18634,-6175.62246,5660.31995

0.50737, -10685.47212, 6223.84575

0.88394, -7937.93400, 1971.45501

0.98540、3054.98273、261.93803

0.70291、10670.53623、4479.93075

0.32083、8746.05579、6594.01140

0.09370、3686.25245、4010.79420

0.01751、900.40686、1280.50557

这如何尊重 gtol ??

Results for Dfun(optimal parameters on the grid of 8 points

这就是为什么我认为我不明白 gtol 的工作原理。

【问题讨论】:

    标签: python scipy


    【解决方案1】:

    从 scipy/optimize/minpack/lmder.f 中,我们找到了更详细的描述

    c       gtol is a nonnegative input variable. termination
    c         occurs when the cosine of the angle between fvec and
    c         any column of the jacobian is at most gtol in absolute
    c         value. therefore, gtol measures the orthogonality
    c         desired between the function vector and the columns
    c         of the jacobian.
    

    这只是意味着如果gtol=0,那么f(x_optimal) 和雅可比的列在收敛时是垂直的。如果是这种情况,那么f'(x_optimal).T @ f(x_optimal) 是一个零矩阵。由于此产品被用作迭代的一部分,因此在 0 时停止是有意义的,因为无法取得更多进展。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-29
      • 1970-01-01
      • 2015-10-20
      • 1970-01-01
      • 2013-08-22
      • 2020-08-11
      • 1970-01-01
      • 2012-05-02
      相关资源
      最近更新 更多