随机梯度下降解线性回归

%用一个样本计算梯度值;然后更新theta值

function theta = Stochastic_GD(x, y, max_iter, loss_thrd, learn_rate)
    [m, n] = size(x);           %取得 x 的行数和列数
    theta = zeros(n, 1);         %创建 n 维全0列向量,存储待辨识参数的初值; 等价于theta = [0;0];
    loss_pre = 10000;
    iter = 0;

    while iter < max_iter

[x y] = RandomSort(x, y);

        for i = 1:m
error = y(i)-calcu_hx(theta, x, i);              %计算第 i 个样本产生的偏差;
            for j = 1:n
                theta(j) = theta(j) + learn_rate * error * x(i, j);
            end
        end
                      
        loss = 0.0;
        for i = 1:m
        loss = loss + (y(i)-calcu_hx(theta, x, i))^2;  %计算损失函数
        end
            
        if abs(loss_pre - loss) <= loss_thrd
        break;
        end
        
        fprintf('loop = %d,\tloss = %0.6f,\ttheta(1) = %0.6f,\ttheta(2) = %f\n',iter, loss, theta(1), theta(2));
        
        loss_pre = loss;
        iter = iter + 1;
    end
end

%对x1,x2进行随机排序后保存到y1,y2中,模拟随机产生样本
function [y1 y2] = RandomSort(x1, x2)
y1 = x1;
    y2 = x2;
    [m, n] = size(x1); 
    r = randperm(m);
    for i=1:m
        y1(i, 2) = x1(r(i), 2);
        y2(i) = x2(r(i));
    end
end

%计算第 index 样本产生的估计值
function hx = calcu_hx(theta, x, index)
    hx = 0.0;
    for i = 1:length(theta)
        hx = hx + theta(i)*x(index, i);
    end

end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值