【问题标题】:How to apply Gradient descent to the equation ax²+bx+c?如何将梯度下降应用于方程 ax²+bx+c?
【发布时间】:2020-10-27 07:27:55
【问题描述】:

我想对方程 ax²+bx+c 应用梯度下降,看看它是否在样本数据集上提供了更好的精度。 我已经使用了代码

cost = (1/2) * (sum(((y - y_current)**2)))
a_gradient = -sum(mul(X,X)*(y - y_current))
b_gradient = -sum(X * (y - y_current))
c_gradient = -sum(y - y_current)
        

其中 y current 是预测的 Y。 我认为问题在于未知变量(a,b,c)的差异,因为在使用此代码时遇到此错误。

<ipython-input-73-3e57c8f474a1>:11: RuntimeWarning: invalid value encountered in double_scalars
  a_current = a_current - (learning_rate * a_gradient)

且代价函数的值趋于无穷。

成本函数与迭代次数图:

请告诉我正确的区别或您能想到的任何其他解决方案 谢谢

【问题讨论】:

    标签: python machine-learning linear-regression gradient-descent


    【解决方案1】:

    的导数

    ax2+bx + c

    2ax+b

    为了应用 梯度下降,您需要减去导数 2ax+b 乘以 学习率 来自每一步计算的新值

    Yprevious = Y预测

    Ypredicted = Ypredicted - lr(2aYprevious+b)强>

    其中lr是学习率,Yprevious是初始值。

    例如函数 3x2+4x+1 的根是 -1-1/3,导数为 6x + 4. 。将起点设置为 3.0

    y0=3.0
    lr=0.01
    y=lr*(6*y0+4)
    while abs(y-y0)>0.00000001:
        y0=y
        y=y-lr*(6*y0+4)
    print(y)
    
    -0.6666666622832803
    

    是最小值。

    【讨论】:

      猜你喜欢
      • 2016-03-20
      • 1970-01-01
      • 2018-01-31
      • 1970-01-01
      • 1970-01-01
      • 2016-06-13
      • 1970-01-01
      • 1970-01-01
      • 2018-12-10
      相关资源
      最近更新 更多