随机梯度下降实现 - MATLAB

时间:2011-02-25 12:41:35

标签: matlab artificial-intelligence machine-learning regression mathematical-optimization

我正在尝试在MATLAB中实现“Stochastic gradient descent”。我完全按照算法,但我得到了一个非常非常大的w(coffients)预测/拟合功能。我在算法中有错误吗?

算法: enter image description here

x = 0:0.1:2*pi      // X-axis
    n = size(x,2);      
    r = -0.2+(0.4).*rand(n,1);  //generating random noise to be added to the sin(x) function

    t=zeros(1,n);
    y=zeros(1,n);



    for i=1:n
        t(i)=sin(x(i))+r(i);          // adding the noise
        y(i)=sin(x(i));               // the function without noise
    end

    f = round(1+rand(20,1)*n);        //generating random indexes

    h = x(f);                         //choosing random x points
    k = t(f);                         //chossing random y points

    m=size(h,2);                     // length of the h vector

    scatter(h,k,'Red');              // drawing the training points (with noise)
    %scatter(x,t,2);
    hold on;
    plot(x,sin(x));                 // plotting the Sin function


    w = [0.3 1 0.5];                    // starting point of w
    a=0.05;                         // learning rate "alpha"

// ---------------- ALGORITHM ---------------------//
    for i=1:20
        v = [1 h(i) h(i).^2];                      // X vector
        e = ((w*v') - k(i)).*v;            // prediction - observation
        w = w - a*e;                       // updating w
    end

    hold on;

    l = 0:1:6;
    g = w(1)+w(2)*l+w(3)*(l.^2);
    plot(l,g,'Yellow');                      // drawing the prediction function

2 个答案:

答案 0 :(得分:7)

如果学习率过高,新元可能会出现分歧 学习率应该收敛为零。

答案 1 :(得分:3)

通常情况下,如果w结果值太大,则会过度拟合。我没有仔细查看你的代码。但我认为,你的代码中缺少的是一个适当的正则化术语,它可以防止训练过度拟合。另外,在这里:

e = ((w*v') - k(i)).*v;

这里的v不是预测值的梯度,不是吗?根据算法,你应该替换它。让我们看看这样做之后会是怎样的。