%用一个样本计算梯度值;然后更新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:merror = 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