Julia,Gradient下降实施不起作用

时间:2017-11-08 21:25:31

标签: julia gradient-descent

我正在尝试从头开始实施渐变下降算法,以找到线性拟合线的斜率和截距值。

使用包并计算斜率和截距,我得到斜率= 0.04和截距= 7.2但是当我使用我的梯度下降算法得到同样的问题时,我得到斜率并截取两个值=( - 无穷大, - 无穷大)< / p>

这是我的代码

x= [1,2,3,4,5,6,7,8,9,10,11,12,13,141,5,16,17,18,19,20]
y=[2,3,4,5,6,7,8,9,10,11,12,13,141,5,16,17,18,19,20,21]    

function GradientDescent()
   m=0
   c=0
   for i=1:10000
      for k=1:length(x)
         Yp = m*x[k] + c   
         E = y[k]-Yp             #error in predicted value

         dm = 2*E*(-x[k])    # partial derivation of cost function w.r.t slope(m)
         dc = 2*E*(-1)       # partial derivate of cost function w.r.t. Intercept(c)
         m = m + (dm * 0.001)
         c = c + (dc * 0.001)
      end
   end
   return m,c
end

Values  = GradientDescent()   # after running values = (-inf,-inf)

2 个答案:

答案 0 :(得分:1)

我没有做过数学计算,而是写了测试。分配m和c时似乎出现了符号错误。

另外,编写测试确实有帮助,而Julia简单易懂:)

function GradientDescent(x, y)
   m=0.0
   c=0.0
   for i=1:10000
      for k=1:length(x)                       
        Yp = m*x[k] + c   
        E = y[k]-Yp
        dm = 2*E*(-x[k])
        dc = 2*E*(-1)

        m = m - (dm * 0.001)
        c = c - (dc * 0.001)
       end
   end
   return m,c
end

using Base.Test

@testset "gradient descent" begin
    @testset "slope $slope" for slope in [0, 1, 2]
        @testset "intercept for $intercept" for intercept in [0, 1, 2]
            x = 1:20
            y = broadcast(x -> slope * x + intercept, x)

            computed_slope, computed_intercept = GradientDescent(x, y)
            @test slope ≈ computed_slope atol=1e-8
            @test intercept ≈ computed_intercept atol=1e-8
        end
    end
end

答案 1 :(得分:0)

我无法得到您的确切数字,但这很接近。也许有帮助?

                                        # 141 ?  
datax = [1,2,3,4,5,6,7,8,9,10,11,12,13,141,5,16,17,18,19,20]
datay = [2,3,4,5,6,7,8,9,10,11,12,13,141,5,16,17,18,19,20,21]

function gradientdescent() 
    m = 0
    b = 0
    learning_rate = 0.00001
    for n in 1:10000
        for i in 1:length(datay)
            x = datax[i]
            y = datay[i]
            guess = m * x + b
            error = y - guess
            dm = 2error * x
            dc = 2error
            m += dm * learning_rate
            b += dc * learning_rate
        end
    end 
    return m, b
end

gradientdescent()

(-0.04, 17.35)

似乎调整学习率至关重要......