【问题标题】:Struggling to minimize loss function努力最小化损失函数
【发布时间】:2021-11-25 12:34:14
【问题描述】:

我期待在下面最小化这个函数来估计正态分布的参数

Function image

我的代码如下所示:

import numpy as np
from scipy import stats
from scipy.optimize import minimize
x = [1,2,3,4,5]
def oro(theta, x):
    norma = 0 
    b = 1
    u = theta[0]
    o = theta[1]
    x = np.array(x)
    x0 = 0
    f0 = -(((1/(o*(2*3.14)**(0.5)))*(2.718)**-(((x0-u)**2)/(2*(o**2))))**b)**-1
    for i in range(x.size):
        f = (1/(o*(2*3.14)**(0.5)))*(2.718)**-(((x[i]-u)**2)/(2*(o**2)))**b
        norma += f0*f
    return norma
theta_init = [0, 1]
res = minimize(oro, theta_init, args=x)
res

但最后我明白了:

<ipython-input-81-ee81472a023a>:8: RuntimeWarning: divide by zero encountered in double_scalars
  f0 = -(((1/(o*(2*3.14)**(0.5)))*(2.718)**-(((x0-u)**2)/(2*(o**2))))**b)**-1
<ipython-input-81-ee81472a023a>:11: RuntimeWarning: invalid value encountered in double_scalars
  norma += f0*f
<ipython-input-81-ee81472a023a>:8: RuntimeWarning: divide by zero encountered in double_scalars
  f0 = -(((1/(o*(2*3.14)**(0.5)))*(2.718)**-(((x0-u)**2)/(2*(o**2))))**b)**-1
<ipython-input-81-ee81472a023a>:11: RuntimeWarning: invalid value encountered in double_scalars
  norma += f0*f
<ipython-input-81-ee81472a023a>:8: RuntimeWarning: divide by zero encountered in double_scalars
  f0 = -(((1/(o*(2*3.14)**(0.5)))*(2.718)**-(((x0-u)**2)/(2*(o**2))))**b)**-1
<ipython-input-81-ee81472a023a>:11: RuntimeWarning: invalid value encountered in double_scalars
  norma += f0*f
      fun: nan
 hess_inv: array([[9.57096191e+02, 2.41349815e+01],
       [2.41349815e+01, 8.33412317e-01]])
      jac: array([nan, nan])
  message: 'Desired error not necessarily achieved due to precision loss.'
     nfev: 357
      nit: 4
     njev: 119
   status: 2
  success: False
        x: array([165623.69347712,   1751.95100725])

请告诉我,我做错了什么?

在 1 个答案后更新(添加界限)。我得到的错误更少,但仍然不成功:

<ipython-input-271-b51d0c455468>:8: RuntimeWarning: divide by zero encountered in double_scalars
  f0 = -(((1/(std*(2*np.pi)**(0.5)))*(np.exp(1))**-(((x0-mean)**2)/(2*(std**2))))**b)**-1
<ipython-input-271-b51d0c455468>:11: RuntimeWarning: invalid value encountered in double_scalars
  norma += f0*f
      fun: nan
 hess_inv: <2x2 LbfgsInvHessProduct with dtype=float64>
      jac: array([-0.00012861,  0.00018581])
  message: 'ABNORMAL_TERMINATION_IN_LNSRCH'
     nfev: 75
      nit: 2
     njev: 25
   status: 2
  success: False
        x: array([250.13040562, 343.06899721])

【问题讨论】:

    标签: python function scipy-optimize-minimize


    【解决方案1】:

    minimize 不知道您的 theta 向量中的第二项(我假设是标准差)需要为正数。

    尝试添加bounds

    res = minimize(oro, theta_init, args=x, bounds=((None, None), (1e-4, None))
    

    【讨论】:

    • 似乎是解决此问题的正确方法,但我仍然收到错误
    【解决方案2】:

    您可以分解复杂的函数来识别有问题的表达式,添加断言以将其值限制为正确的值。

    添加边界以将变量限制在正确的值范围内。

    代码

    import numpy as np
    from scipy import stats
    from scipy.optimize import minimize
    x = [1,2,3,4,5]
    def oro(theta, x):
        norma = 0 
        b = 1
        u = theta[0]
        o = theta[1]
        x = np.array(x)
        x0 = 0
    
        assert o > 0
    
        t = (o*(2*3.14)**(0.5))
        assert t > 0
    
        s = (2*(o**2))
        assert s > 0
    
        z = (((x0-u)**2)/s)
    
        m = (((1/t)*(2.718)**-z)**b)
        assert m != 0
    
        # f0 = -(((1/(o*(2*3.14)**(0.5)))*(2.718)**-(((x0-u)**2)/(2*(o**2))))**b)**-1
        f0 = -m**-1
    
        for i in range(x.size):
            f = (1/(o*(2*3.14)**(0.5)))*(2.718)**-(((x[i]-u)**2)/(2*(o**2)))**b
            norma += f0*f
        return norma
    theta_init = [0, 1]
    res = minimize(oro, theta_init, args=x, bounds=((1, 10), (1, 10)))
    # res = minimize(oro, theta_init, args=x)
    print(res)
    

    输出

          fun: -1.932543956399183e+16
     hess_inv: <2x2 LbfgsInvHessProduct with dtype=float64>      
          jac: array([-9.65382468e+16,  1.44838865e+18])
      message: 'CONVERGENCE: NORM_OF_PROJECTED_GRADIENT_<=_PGTOL'
         nfev: 9
          nit: 2
         njev: 3
       status: 0
      success: True
            x: array([10.,  1.])
    

    【讨论】:

      猜你喜欢
      • 2018-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-13
      • 1970-01-01
      • 2011-01-17
      • 2018-08-04
      相关资源
      最近更新 更多