随机梯度下降实现 – MATLAB

我正在尝试在MATLAB中实现“随机梯度下降”。我完全按照算法进行操作,但得到的预测/拟合函数的w(系数)非常非常大。我的算法有错误吗?

算法如下: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

回答:

如果使用过大的学习率,SGD很可能会发散。
学习率应该收敛到零。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注