您的位置:首页 > 其它

随机梯度下降解线性回归

2013-10-26 15:36 92 查看
%用一个样本计算梯度值;然后更新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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: